Browse Source

feat(hadis): allow cross-type parent categories and add topic/leaf filters

master
Mohsen Taba 3 weeks ago
parent
commit
eff48c31a7
  1. 7
      apps/hadis/models/category.py
  2. 38
      apps/hadis/views_admin.py

7
apps/hadis/models/category.py

@ -93,13 +93,6 @@ class HadisCategory(LowercaseSlugMixin, MPTTModel):
f'Parent sect: {self.parent.sect.sect_type}, ' f'Parent sect: {self.parent.sect.sect_type}, '
f'Your sect: {self.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(): if self.parent and self.parent.hadis_set.exists():
raise ValidationError( raise ValidationError(
_('Cannot set parent to a category that already contains hadiths/content items.') _('Cannot set parent to a category that already contains hadiths/content items.')

38
apps/hadis/views_admin.py

@ -100,6 +100,24 @@ class AdminHadisCategoryViewSet(ReadOnlyModelViewSet):
queryset = queryset.filter( queryset = queryset.filter(
Q(title__icontains=search) | Q(slug__icontains=search) 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") return queryset.order_by("order", "id")
@ -164,12 +182,24 @@ class DovodiHadisCategoryViewSet(ModelViewSet):
queryset = queryset.filter(annotated_has_hadith=False) queryset = queryset.filter(annotated_has_hadith=False)
elif str(has_hadith).lower() in ["true", "1"]: elif str(has_hadith).lower() in ["true", "1"]:
queryset = queryset.filter(annotated_has_hadith=True) 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") is_topic = self.request.query_params.get("is_topic")
if is_topic is not None and is_topic != "all": 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") return queryset.order_by("-id")

Loading…
Cancel
Save