diff --git a/api/common/common_views.py b/api/common/common_views.py index 7fddbf3c..7944a5b9 100644 --- a/api/common/common_views.py +++ b/api/common/common_views.py @@ -279,43 +279,37 @@ class CollegeWiseLcReport(APIView): def get(self, request): date = request.query_params.get('date') if date: - learning_circle_count_subquery = ( + lc_query = ( LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value, created_at__date=date) - .values(org_title=F("org__title")) - .annotate(learning_circle_count=Count("id")) - .filter(org_title=OuterRef("org_title")) - .values("learning_circle_count") - [:1] - ) - - learning_circles_info = ( - LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value, created_at__date=date) - .values(org_title=F("org__title")) - .annotate( - learning_circle_count=Subquery(learning_circle_count_subquery), - user_count=Count("user_circle_link_circle__user") - ) - .order_by("org_title") + .select_related("org") + .prefetch_related("user_circle_link_circle__user") ) + learning_circle_count_subquery = (lc_query.values(org_title=F("org__title")) + .annotate(learning_circle_count=Count("id")) + .filter(org_title=OuterRef("org_title")) + .values("learning_circle_count") + [:1]) + learning_circles_info = (lc_query.values(org_title=F("org__title")) + .annotate( + learning_circle_count=Subquery(learning_circle_count_subquery), + user_count=Count("user_circle_link_circle__user"), + ).order_by("org_title")) else: - learning_circle_count_subquery = ( + lc_query = ( LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value) - .values(org_title=F("org__title")) - .annotate(learning_circle_count=Count("id")) - .filter(org_title=OuterRef("org_title")) - .values("learning_circle_count") - [:1] - ) - - learning_circles_info = ( - LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value) - .values(org_title=F("org__title")) - .annotate( - learning_circle_count=Subquery(learning_circle_count_subquery), - user_count=Count("user_circle_link_circle__user"), - ) - .order_by("org_title") + .select_related("org") + .prefetch_related("user_circle_link_circle__user") ) + learning_circle_count_subquery = (lc_query.values(org_title=F("org__title")) + .annotate(learning_circle_count=Count("id")) + .filter(org_title=OuterRef("org_title")) + .values("learning_circle_count") + [:1]) + learning_circles_info = (lc_query.values(org_title=F("org__title")) + .annotate( + learning_circle_count=Subquery(learning_circle_count_subquery), + user_count=Count("user_circle_link_circle__user"), + ).order_by("org_title")) paginated_queryset = CommonUtils.get_paginated_queryset( learning_circles_info, @@ -379,7 +373,6 @@ def get(self, request): district=F("user__user_organization_link_user__org__district__name"), circle_ig=F("circle__ig__name"), organisation=F("user__user_organization_link_user__org__title"), - ) .annotate( karma_earned=Sum( diff --git a/api/common/urls.py b/api/common/urls.py index df276c79..3d68618b 100644 --- a/api/common/urls.py +++ b/api/common/urls.py @@ -10,7 +10,9 @@ path('lc-report/csv/', common_views.LcReportDownloadAPI.as_view()), path('lc-enrollment/', common_views.LearningCircleEnrollment.as_view()), path('lc-enrollment/csv/', common_views.LearningCircleEnrollmentCSV.as_view()), + path('global-count/', common_views.GlobalCountAPI.as_view()), + path('gta-sandshore/', common_views.GTASANDSHOREAPI.as_view()), path('profile-pic//', common_views.UserProfilePicAPI.as_view()), path('list-ig/', common_views.ListIGAPI.as_view()),