|
|
@ -266,6 +266,57 @@ class HadisCategoryTreeNormalView(ListAPIView): |
|
|
Q(active_hadis_count__gt=0) | Q(children_total_count__gt=0) | Q(source_type=HadisCategory.SourceType.QURAN) |
|
|
Q(active_hadis_count__gt=0) | Q(children_total_count__gt=0) | Q(source_type=HadisCategory.SourceType.QURAN) |
|
|
).order_by('sect__order', 'order') |
|
|
).order_by('sect__order', 'order') |
|
|
|
|
|
|
|
|
|
|
|
def apply_category_ordering_and_counts(queryset, request): |
|
|
|
|
|
from django.db.models import Subquery, OuterRef, Count, IntegerField |
|
|
|
|
|
from django.db.models.functions import Coalesce |
|
|
|
|
|
from django.db.models.expressions import RawSQL |
|
|
|
|
|
from django.utils.translation import get_language |
|
|
|
|
|
|
|
|
|
|
|
descendant_hadis_subquery = Subquery( |
|
|
|
|
|
Hadis.objects.filter( |
|
|
|
|
|
category__tree_id=OuterRef('tree_id'), |
|
|
|
|
|
category__lft__gte=OuterRef('lft'), |
|
|
|
|
|
category__rght__lte=OuterRef('rght'), |
|
|
|
|
|
status=True |
|
|
|
|
|
).values('category__tree_id').annotate(cnt=Count('id')).values('cnt')[:1], |
|
|
|
|
|
output_field=IntegerField() |
|
|
|
|
|
) |
|
|
|
|
|
queryset = queryset.annotate(total_content_count=Coalesce(descendant_hadis_subquery, 0)) |
|
|
|
|
|
|
|
|
|
|
|
ordering_param = ( |
|
|
|
|
|
request.query_params.get('ordering') or |
|
|
|
|
|
request.query_params.get('sort') or |
|
|
|
|
|
'' |
|
|
|
|
|
).strip().lower() |
|
|
|
|
|
|
|
|
|
|
|
if ordering_param in ['content', 'most_content', '-total_content_count', '-content']: |
|
|
|
|
|
return queryset.order_by('-total_content_count', 'order', 'id') |
|
|
|
|
|
elif ordering_param in ['alphabetical', 'title', 'slug', 'a-z']: |
|
|
|
|
|
lang = ( |
|
|
|
|
|
request.query_params.get('language_code') or |
|
|
|
|
|
request.query_params.get('lang') or |
|
|
|
|
|
getattr(request, 'LANGUAGE_CODE', None) |
|
|
|
|
|
) |
|
|
|
|
|
if not lang and hasattr(request, 'headers'): |
|
|
|
|
|
lang = request.headers.get('Accept-Language') |
|
|
|
|
|
if lang: |
|
|
|
|
|
lang = lang.split(',')[0].split(';')[0].split('-')[0].strip().lower() |
|
|
|
|
|
if not lang: |
|
|
|
|
|
lang = get_language() or 'en' |
|
|
|
|
|
|
|
|
|
|
|
queryset = queryset.annotate( |
|
|
|
|
|
loc_title=RawSQL(""" |
|
|
|
|
|
COALESCE( |
|
|
|
|
|
NULLIF((SELECT COALESCE(elem->>'title', elem->>'text') FROM jsonb_array_elements(CASE WHEN jsonb_typeof(hadis_hadiscategory.title::jsonb) = 'array' THEN hadis_hadiscategory.title::jsonb ELSE '[]'::jsonb END) elem WHERE elem->>'language_code' = %s LIMIT 1), ''), |
|
|
|
|
|
NULLIF((SELECT COALESCE(elem->>'title', elem->>'text') FROM jsonb_array_elements(CASE WHEN jsonb_typeof(hadis_hadiscategory.title::jsonb) = 'array' THEN hadis_hadiscategory.title::jsonb ELSE '[]'::jsonb END) elem WHERE elem->>'language_code' = 'en' LIMIT 1), ''), |
|
|
|
|
|
hadis_hadiscategory.slug |
|
|
|
|
|
) |
|
|
|
|
|
""", [lang]) |
|
|
|
|
|
) |
|
|
|
|
|
return queryset.order_by('loc_title', 'slug', 'id') |
|
|
|
|
|
|
|
|
|
|
|
return queryset.order_by('order', 'id') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HadisCategorySelectBySectView(ListAPIView): |
|
|
class HadisCategorySelectBySectView(ListAPIView): |
|
|
""" |
|
|
""" |
|
|
@ -274,7 +325,6 @@ class HadisCategorySelectBySectView(ListAPIView): |
|
|
""" |
|
|
""" |
|
|
serializer_class = HadisCategorySelectSerializer |
|
|
serializer_class = HadisCategorySelectSerializer |
|
|
pagination_class = StandardResultsSetPagination |
|
|
pagination_class = StandardResultsSetPagination |
|
|
|
|
|
|
|
|
@categories_tree_by_sect_swagger |
|
|
@categories_tree_by_sect_swagger |
|
|
def get(self, request, *args, **kwargs): |
|
|
def get(self, request, *args, **kwargs): |
|
|
return self.list(request, *args, **kwargs) |
|
|
return self.list(request, *args, **kwargs) |
|
|
@ -323,7 +373,7 @@ class HadisCategorySelectBySectView(ListAPIView): |
|
|
|
|
|
|
|
|
from django.db.models import Count, Q |
|
|
from django.db.models import Count, Q |
|
|
# Return children of this category that have either active hadiths, children, or are quran |
|
|
# Return children of this category that have either active hadiths, children, or are quran |
|
|
return HadisCategory.objects.filter( |
|
|
|
|
|
|
|
|
queryset = HadisCategory.objects.filter( |
|
|
parent=parent_category, |
|
|
parent=parent_category, |
|
|
sect__sect_type=sect_type, |
|
|
sect__sect_type=sect_type, |
|
|
sect__is_active=True |
|
|
sect__is_active=True |
|
|
@ -332,7 +382,8 @@ class HadisCategorySelectBySectView(ListAPIView): |
|
|
children_total_count=Count('children') |
|
|
children_total_count=Count('children') |
|
|
).filter( |
|
|
).filter( |
|
|
Q(active_hadis_count__gt=0) | Q(children_total_count__gt=0) | Q(source_type=HadisCategory.SourceType.QURAN) |
|
|
Q(active_hadis_count__gt=0) | Q(children_total_count__gt=0) | Q(source_type=HadisCategory.SourceType.QURAN) |
|
|
).order_by('order') |
|
|
|
|
|
|
|
|
) |
|
|
|
|
|
return apply_category_ordering_and_counts(queryset, self.request) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HadisCategorySelectBySectSourceView(ListAPIView): |
|
|
class HadisCategorySelectBySectSourceView(ListAPIView): |
|
|
@ -391,7 +442,7 @@ class HadisCategorySelectBySectSourceView(ListAPIView): |
|
|
|
|
|
|
|
|
from django.db.models import Count, Q |
|
|
from django.db.models import Count, Q |
|
|
# Return children of this category, filtered by source_type and excluding empty leaves |
|
|
# Return children of this category, filtered by source_type and excluding empty leaves |
|
|
return HadisCategory.objects.filter( |
|
|
|
|
|
|
|
|
queryset = HadisCategory.objects.filter( |
|
|
parent=parent_category, |
|
|
parent=parent_category, |
|
|
sect__sect_type=sect_type, |
|
|
sect__sect_type=sect_type, |
|
|
sect__is_active=True, |
|
|
sect__is_active=True, |
|
|
@ -401,7 +452,8 @@ class HadisCategorySelectBySectSourceView(ListAPIView): |
|
|
children_total_count=Count('children') |
|
|
children_total_count=Count('children') |
|
|
).filter( |
|
|
).filter( |
|
|
Q(active_hadis_count__gt=0) | Q(children_total_count__gt=0) | Q(source_type=HadisCategory.SourceType.QURAN) |
|
|
Q(active_hadis_count__gt=0) | Q(children_total_count__gt=0) | Q(source_type=HadisCategory.SourceType.QURAN) |
|
|
).order_by('order') |
|
|
|
|
|
|
|
|
) |
|
|
|
|
|
return apply_category_ordering_and_counts(queryset, self.request) |
|
|
|
|
|
|
|
|
class CategoriesView(ListAPIView): |
|
|
class CategoriesView(ListAPIView): |
|
|
""" |
|
|
""" |
|
|
@ -415,14 +467,21 @@ class CategoriesView(ListAPIView): |
|
|
|
|
|
|
|
|
def get_queryset(self): |
|
|
def get_queryset(self): |
|
|
from django.db.models import Count, Q |
|
|
from django.db.models import Count, Q |
|
|
return HadisCategory.objects.filter( |
|
|
|
|
|
|
|
|
queryset = HadisCategory.objects.filter( |
|
|
sect__is_active=True |
|
|
sect__is_active=True |
|
|
).annotate( |
|
|
).annotate( |
|
|
active_hadis_count=Count('hadis', filter=Q(hadis__status=True)), |
|
|
active_hadis_count=Count('hadis', filter=Q(hadis__status=True)), |
|
|
children_total_count=Count('children') |
|
|
children_total_count=Count('children') |
|
|
).filter( |
|
|
).filter( |
|
|
Q(active_hadis_count__gt=0) | Q(children_total_count__gt=0) | Q(source_type=HadisCategory.SourceType.QURAN) |
|
|
Q(active_hadis_count__gt=0) | Q(children_total_count__gt=0) | Q(source_type=HadisCategory.SourceType.QURAN) |
|
|
).order_by('order', 'id') |
|
|
|
|
|
|
|
|
) |
|
|
|
|
|
is_root = self.request.query_params.get('is_root', '').lower() |
|
|
|
|
|
if is_root in ['true', '1']: |
|
|
|
|
|
queryset = queryset.filter(parent__isnull=True) |
|
|
|
|
|
elif is_root in ['false', '0']: |
|
|
|
|
|
queryset = queryset.filter(parent__isnull=False) |
|
|
|
|
|
|
|
|
|
|
|
return apply_category_ordering_and_counts(queryset, self.request) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -451,7 +510,7 @@ class CategoriesBySectView(ListAPIView): |
|
|
elif is_root in ['false', '0']: |
|
|
elif is_root in ['false', '0']: |
|
|
queryset = queryset.filter(parent__isnull=False) |
|
|
queryset = queryset.filter(parent__isnull=False) |
|
|
|
|
|
|
|
|
return queryset.order_by('order', 'id') |
|
|
|
|
|
|
|
|
return apply_category_ordering_and_counts(queryset, self.request) |
|
|
|
|
|
|
|
|
@categories_by_sect_swagger |
|
|
@categories_by_sect_swagger |
|
|
def get(self, request, *args, **kwargs): |
|
|
def get(self, request, *args, **kwargs): |
|
|
|