Browse Source

hadis ids added to category tree

master
Mohsen Taba 1 month ago
parent
commit
12a6d59464
  1. 35
      apps/hadis/views/category.py

35
apps/hadis/views/category.py

@ -3,6 +3,7 @@ from rest_framework.response import Response
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from utils.pagination import NoPagination from utils.pagination import NoPagination
from django.db.models import Q from django.db.models import Q
from collections import defaultdict
from utils.pagination import StandardResultsSetPagination from utils.pagination import StandardResultsSetPagination
from ..models import HadisSect, HadisCategory,Hadis from ..models import HadisSect, HadisCategory,Hadis
from apps.bookmark.serializers.bookmark import BookmarkStatusSerializer from apps.bookmark.serializers.bookmark import BookmarkStatusSerializer
@ -70,6 +71,16 @@ class HadisCategoryTreeView(ListAPIView):
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
queryset = self.get_queryset() 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 # 1. Build a mapping of all categories and their children
category_map = {cat.id: cat for cat in queryset} category_map = {cat.id: cat for cat in queryset}
@ -135,7 +146,13 @@ class HadisCategoryTreeView(ListAPIView):
} }
# Build tree using mapping # 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) grouped_data[sect_type]['categories'].append(category_data)
# Count total categories # Count total categories
@ -148,11 +165,12 @@ class HadisCategoryTreeView(ListAPIView):
return Response(response_data) 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 Build tree from flat mapping
""" """
children = children_map.get(category.id, []) children = children_map.get(category.id, [])
has_hadis = category.direct_hadis_count > 0
# Align fields with CategorySerializer # Align fields with CategorySerializer
return { return {
@ -165,13 +183,20 @@ class HadisCategoryTreeView(ListAPIView):
'sect_type': category.sect.sect_type, 'sect_type': category.sect.sect_type,
'hadis_count': category.direct_hadis_count, # Direct count 'hadis_count': category.direct_hadis_count, # Direct count
'children_count': recursive_counts.get(category.id, 0), # Total in sub-tree (matches normal endpoint) '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, 'order': category.order,
'thumbnail': self._get_thumbnail_url(category, request), 'thumbnail': self._get_thumbnail_url(category, request),
'xmind_file': self._get_xmind_url(category, request), 'xmind_file': self._get_xmind_url(category, request),
'has_xmind_file': bool(getattr(category, 'xmind_file', None)), 'has_xmind_file': bool(getattr(category, 'xmind_file', None)),
'children': [ '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 for child in children
] ]
} }
@ -505,4 +530,4 @@ class HadisCategoryXMindView(APIView):
} }
} }
return Response(data)
return Response(data)
Loading…
Cancel
Save