From 12a6d594645f109a96f810cab784a3219d0944a5 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Mon, 8 Jun 2026 11:50:53 +0330 Subject: [PATCH] hadis ids added to category tree --- apps/hadis/views/category.py | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/apps/hadis/views/category.py b/apps/hadis/views/category.py index b6b4015..4fa26e9 100644 --- a/apps/hadis/views/category.py +++ b/apps/hadis/views/category.py @@ -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 @@ -70,6 +71,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} @@ -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 ] } @@ -505,4 +530,4 @@ class HadisCategoryXMindView(APIView): } } - return Response(data) \ No newline at end of file + return Response(data)