|
|
|
@ -3,6 +3,7 @@ from rest_framework.response import Response |
|
|
|
from django.shortcuts import get_object_or_404 |
|
|
|
from utils.pagination import NoPagination |
|
|
|
from django.db.models import Q |
|
|
|
from collections import defaultdict |
|
|
|
from utils.pagination import StandardResultsSetPagination |
|
|
|
from ..models import HadisSect, HadisCategory,Hadis |
|
|
|
from apps.bookmark.serializers.bookmark import BookmarkStatusSerializer |
|
|
|
@ -71,6 +72,16 @@ class HadisCategoryTreeView(ListAPIView): |
|
|
|
def list(self, request, *args, **kwargs): |
|
|
|
queryset = self.get_queryset() |
|
|
|
|
|
|
|
hadis_ids_map = defaultdict(list) |
|
|
|
hadis_pairs = ( |
|
|
|
Hadis.objects |
|
|
|
.filter(status=True, category_id__in=[cat.id for cat in queryset]) |
|
|
|
.values_list('category_id', 'id') |
|
|
|
.order_by('category_id', 'id') |
|
|
|
) |
|
|
|
for category_id, hadis_id in hadis_pairs: |
|
|
|
hadis_ids_map[category_id].append(hadis_id) |
|
|
|
|
|
|
|
# 1. Build a mapping of all categories and their children |
|
|
|
category_map = {cat.id: cat for cat in queryset} |
|
|
|
children_map = {} |
|
|
|
@ -135,7 +146,13 @@ class HadisCategoryTreeView(ListAPIView): |
|
|
|
} |
|
|
|
|
|
|
|
# Build tree using mapping |
|
|
|
category_data = self._build_tree_recursive(root, request, children_map, recursive_counts) |
|
|
|
category_data = self._build_tree_recursive( |
|
|
|
root, |
|
|
|
request, |
|
|
|
children_map, |
|
|
|
recursive_counts, |
|
|
|
hadis_ids_map, |
|
|
|
) |
|
|
|
grouped_data[sect_type]['categories'].append(category_data) |
|
|
|
|
|
|
|
# Count total categories |
|
|
|
@ -148,11 +165,12 @@ class HadisCategoryTreeView(ListAPIView): |
|
|
|
|
|
|
|
return Response(response_data) |
|
|
|
|
|
|
|
def _build_tree_recursive(self, category, request, children_map, recursive_counts): |
|
|
|
def _build_tree_recursive(self, category, request, children_map, recursive_counts, hadis_ids_map): |
|
|
|
""" |
|
|
|
Build tree from flat mapping |
|
|
|
""" |
|
|
|
children = children_map.get(category.id, []) |
|
|
|
has_hadis = category.direct_hadis_count > 0 |
|
|
|
|
|
|
|
# Align fields with CategorySerializer |
|
|
|
return { |
|
|
|
@ -165,13 +183,20 @@ class HadisCategoryTreeView(ListAPIView): |
|
|
|
'sect_type': category.sect.sect_type, |
|
|
|
'hadis_count': category.direct_hadis_count, # Direct count |
|
|
|
'children_count': recursive_counts.get(category.id, 0), # Total in sub-tree (matches normal endpoint) |
|
|
|
'has_hadis': category.direct_hadis_count > 0, |
|
|
|
'has_hadis': has_hadis, |
|
|
|
'hadis_ids': hadis_ids_map.get(category.id, []) if has_hadis else [], |
|
|
|
'order': category.order, |
|
|
|
'thumbnail': self._get_thumbnail_url(category, request), |
|
|
|
'xmind_file': self._get_xmind_url(category, request), |
|
|
|
'has_xmind_file': bool(getattr(category, 'xmind_file', None)), |
|
|
|
'children': [ |
|
|
|
self._build_tree_recursive(child, request, children_map, recursive_counts) |
|
|
|
self._build_tree_recursive( |
|
|
|
child, |
|
|
|
request, |
|
|
|
children_map, |
|
|
|
recursive_counts, |
|
|
|
hadis_ids_map, |
|
|
|
) |
|
|
|
for child in children |
|
|
|
] |
|
|
|
} |
|
|
|
|