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.
128 lines
4.5 KiB
128 lines
4.5 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 HadisCategorySectListView(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,
|
|
}
|
|
|
|
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 all HadisCategory tree structure grouped by sect and source_type
|
|
Returns all categories in a tree structure
|
|
"""
|
|
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):
|
|
return HadisCategory.objects.filter(
|
|
parent__isnull=True,
|
|
sect__is_active=True
|
|
).order_by('sect__order', 'order')
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
queryset = self.get_queryset()
|
|
|
|
grouped_data = {}
|
|
|
|
serializer_instance = HadisCategoryTreeSerializer(context={'request': request})
|
|
|
|
# گروهبندی بر اساس sect
|
|
for category in queryset:
|
|
sect_id = str(category.sect.id)
|
|
|
|
if sect_id not in grouped_data:
|
|
# اضافه کردن اطلاعات sect
|
|
grouped_data[sect_id] = {
|
|
'sect_info': {
|
|
'id': category.sect.id,
|
|
'sect_type': category.sect.sect_type,
|
|
'title': category.sect.title,
|
|
'description': category.sect.description
|
|
},
|
|
'source_types': [],
|
|
'categories': {}
|
|
}
|
|
|
|
# اضافه کردن source_type به لیست
|
|
if category.source_type not in grouped_data[sect_id]['source_types']:
|
|
grouped_data[sect_id]['source_types'].append(category.source_type)
|
|
|
|
# گروهبندی categories بر اساس source_type
|
|
if category.source_type not in grouped_data[sect_id]['categories']:
|
|
grouped_data[sect_id]['categories'][category.source_type] = []
|
|
|
|
category_data = serializer_instance.to_dict(category)
|
|
grouped_data[sect_id]['categories'][category.source_type].append(category_data)
|
|
|
|
def count_children(children_list):
|
|
count = 0
|
|
for item in children_list:
|
|
count += 1
|
|
if 'children' in item and item['children']:
|
|
count += count_children(item['children'])
|
|
return count
|
|
|
|
total_count = 0
|
|
for sect_data in grouped_data.values():
|
|
for source_categories in sect_data['categories'].values():
|
|
for item in source_categories:
|
|
total_count += 1
|
|
if 'children' in item and item['children']:
|
|
total_count += count_children(item['children'])
|
|
|
|
response_data = {
|
|
'count': total_count,
|
|
'results': grouped_data
|
|
}
|
|
|
|
return Response(response_data)
|