You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

119 lines
3.9 KiB

from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
from utils.pagination import NoPagination
from ..models import HadisSect, HadisCategory
from ..serializers import HadisCategorySectListSerializer, HadisCategoryTreeSerializer
from ..docs import hadis_sect_list_swagger, hadis_category_tree_swagger
class HadisSectListView(ListAPIView):
"""
API view to list all HadisSects grouped by sect_type (shia/sunni)
"""
queryset = HadisSect.objects.filter(is_active=True).order_by('order')
serializer_class = HadisCategorySectListSerializer
pagination_class = NoPagination
@hadis_sect_list_swagger
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def list(self, request, *args, **kwargs):
queryset = self.get_queryset()
response = super().list(request, *args, **kwargs)
lang = request.LANGUAGE_CODE
# Group sects by type
grouped_data = {
'shia': [],
'sunni': []
}
for sect in queryset:
sect_data = {
'id': sect.id,
'title': sect.title,
'seo_field': None
}
if sect.sect_type == HadisSect.SectType.SHIA:
grouped_data['shia'].append(sect_data)
elif sect.sect_type == HadisSect.SectType.SUNNI:
grouped_data['sunni'].append(sect_data)
# Create response with count and results
response_data = {
'count': queryset.count(),
'results': grouped_data
}
return Response(response_data)
class HadisCategoryTreeView(ListAPIView):
"""
API view to get HadisCategory tree structure by sect_id
Returns categories grouped by source_type (quran/hadith)
"""
serializer_class = HadisCategoryTreeSerializer
pagination_class = NoPagination
@hadis_category_tree_swagger
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def get_queryset(self):
sect_id = self.kwargs.get('sect_id')
sect = get_object_or_404(HadisSect, id=sect_id, is_active=True)
# Get root categories (no parent) for this sect
return HadisCategory.objects.filter(
sect=sect,
parent__isnull=True
).order_by('order')
def list(self, request, *args, **kwargs):
queryset = self.get_queryset()
# Group categories by source_type
grouped_data = {
'quran': [],
'hadith': []
}
# Create serializer instance for to_dict method
serializer_instance = HadisCategoryTreeSerializer(context={'request': request})
for category in queryset:
category_data = serializer_instance.to_dict(category)
if category.source_type == HadisCategory.SourceType.QURAN:
grouped_data['quran'].append(category_data)
elif category.source_type == HadisCategory.SourceType.HADITH:
grouped_data['hadith'].append(category_data)
# Calculate total count including all descendants recursively
def count_objects_recursive(data_list):
"""Count all objects including children recursively"""
count = 0
for item in data_list:
count += 1 # Count current item
if 'children' in item and item['children']:
count += count_objects_recursive(item['children']) # Count children recursively
return count
# Calculate total count from grouped data
total_count = (
count_objects_recursive(grouped_data['quran']) +
count_objects_recursive(grouped_data['hadith'])
)
# Create response with count and results
response_data = {
'count': total_count,
'results': grouped_data
}
return Response(response_data)