diff --git a/api/common/common_views.py b/api/common/common_views.py index 19b52caa..b7fe8c01 100644 --- a/api/common/common_views.py +++ b/api/common/common_views.py @@ -11,7 +11,7 @@ from utils.types import IntegrationType, OrganizationType from utils.utils import CommonUtils from .serializer import StudentInfoSerializer - +from collections import defaultdict class LcDashboardAPI(APIView): @@ -21,7 +21,9 @@ def get(self, request): learning_circle_count = LearningCircle.objects.filter(created_at__gt=date).count() total_no_enrollment = UserCircleLink.objects.filter(accepted=True, created_at__gt=date).count() circle_count_by_ig = LearningCircle.objects.filter(created_at__gt=date).values( - ig_name=F('ig__name')).annotate(total_circles=Count('id')) + ig_name=F('ig__name')).annotate(total_circles=Count('id'), + total_users=Count('ig__learningcircle__usercirclelink__user', + distinct=True)) unique_user_count = UserCircleLink.objects.filter(accepted=True, created_at__gt=date).values( 'user').distinct().count() else: @@ -125,11 +127,24 @@ def get(self, request): class CollegeWiseLcReport(APIView): def get(self, request): - learning_circles_info = LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value).annotate( - user_count=Count('usercirclelink'), - org_name=F('org__title') - ).values('name', 'user_count', 'org_name') - return CustomResponse(response=learning_circles_info).get_success_response() + learning_circles_info = LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value) \ + .values('org__title', 'name') \ + .annotate(user_count=Count('usercirclelink')) \ + .order_by('org__title') + + org_learning_circles = defaultdict(list) + + for info in learning_circles_info: + org_name = info['org__title'] + org_learning_circles[org_name].append({ + 'name': info['name'], + 'user_count': info['user_count'] + }) + + result_list = [{"org_name": org_name, "learning_circles": circles} for org_name, circles in + org_learning_circles.items()] + + return CustomResponse(response=result_list).get_success_response() class GlobalCountAPI(APIView):