From eff48c31a7420d67d2b34c12c4be5ba7e43c0cbb Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Mon, 12 Oct 2026 13:01:19 +0330 Subject: [PATCH] feat(hadis): allow cross-type parent categories and add topic/leaf filters --- apps/hadis/models/category.py | 7 ------- apps/hadis/views_admin.py | 38 +++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/apps/hadis/models/category.py b/apps/hadis/models/category.py index cb84941..c3eaca5 100644 --- a/apps/hadis/models/category.py +++ b/apps/hadis/models/category.py @@ -93,13 +93,6 @@ class HadisCategory(LowercaseSlugMixin, MPTTModel): f'Parent sect: {self.parent.sect.sect_type}, ' f'Your sect: {self.sect.sect_type}') ) - if self.parent and self.parent.parent is not None: - if self.source_type != self.parent.source_type: - raise ValidationError( - _('Child category must have the same source_type as its parent when parent is not a root category. ' - f'Parent source_type: {self.parent.source_type}, ' - f'Your source_type: {self.source_type}') - ) if self.parent and self.parent.hadis_set.exists(): raise ValidationError( _('Cannot set parent to a category that already contains hadiths/content items.') diff --git a/apps/hadis/views_admin.py b/apps/hadis/views_admin.py index ae4f7c1..c1a54ce 100644 --- a/apps/hadis/views_admin.py +++ b/apps/hadis/views_admin.py @@ -100,6 +100,24 @@ class AdminHadisCategoryViewSet(ReadOnlyModelViewSet): queryset = queryset.filter( Q(title__icontains=search) | Q(slug__icontains=search) ) + has_children = self.request.query_params.get("has_children") + if has_children is not None and has_children != "all": + if str(has_children).lower() in ["false", "0"]: + queryset = queryset.filter(annotated_has_children=False) + elif str(has_children).lower() in ["true", "1"]: + queryset = queryset.filter(annotated_has_children=True) + is_leaf = self.request.query_params.get("is_leaf") + if is_leaf is not None and is_leaf != "all": + if str(is_leaf).lower() in ["true", "1"]: + queryset = queryset.filter(annotated_has_children=False) + elif str(is_leaf).lower() in ["false", "0"]: + queryset = queryset.filter(annotated_has_children=True) + is_topic = self.request.query_params.get("is_topic") + if is_topic is not None and is_topic != "all": + if str(is_topic).lower() in ["true", "1"]: + queryset = queryset.filter(annotated_has_children=False) + elif str(is_topic).lower() in ["false", "0"]: + queryset = queryset.filter(annotated_has_children=True) return queryset.order_by("order", "id") @@ -164,12 +182,24 @@ class DovodiHadisCategoryViewSet(ModelViewSet): queryset = queryset.filter(annotated_has_hadith=False) elif str(has_hadith).lower() in ["true", "1"]: queryset = queryset.filter(annotated_has_hadith=True) + has_children = self.request.query_params.get("has_children") + if has_children is not None and has_children != "all": + if str(has_children).lower() in ["false", "0"]: + queryset = queryset.filter(annotated_has_children=False) + elif str(has_children).lower() in ["true", "1"]: + queryset = queryset.filter(annotated_has_children=True) + is_leaf = self.request.query_params.get("is_leaf") + if is_leaf is not None and is_leaf != "all": + if str(is_leaf).lower() in ["true", "1"]: + queryset = queryset.filter(annotated_has_children=False) + elif str(is_leaf).lower() in ["false", "0"]: + queryset = queryset.filter(annotated_has_children=True) is_topic = self.request.query_params.get("is_topic") if is_topic is not None and is_topic != "all": - if str(is_topic).lower() in ["false", "0"]: - queryset = queryset.filter(annotated_has_hadith=False) - elif str(is_topic).lower() in ["true", "1"]: - queryset = queryset.filter(annotated_has_hadith=True) + if str(is_topic).lower() in ["true", "1"]: + queryset = queryset.filter(annotated_has_children=False) + elif str(is_topic).lower() in ["false", "0"]: + queryset = queryset.filter(annotated_has_children=True) return queryset.order_by("-id")