diff --git a/api/common/common_views.py b/api/common/common_views.py index c2a84fd1..9a4f387f 100644 --- a/api/common/common_views.py +++ b/api/common/common_views.py @@ -215,11 +215,21 @@ def get(self, request): class CollegeWiseLcReportCSV(APIView): def get(self, request): + learning_circle_count_subquery = ( + 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=Count("id"), user_count=Count("user_circle_link_circle") + learning_circle_count=Subquery(learning_circle_count_subquery), + user_count=Count("user_circle_link_circle__user"), ) .order_by("org_title") ) @@ -241,20 +251,40 @@ class CollegeWiseLcReport(APIView): def get(self, request): date = request.query_params.get('date') if date: + learning_circle_count_subquery = ( + 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=Count("id"), user_count=Count("user_circle_link_user") + 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 = ( + 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=Count("id"), user_count=Count("user_circle_link_user") + learning_circle_count=Subquery(learning_circle_count_subquery), + user_count=Count("user_circle_link_circle__user"), ) .order_by("org_title") ) diff --git a/api/dashboard/discord_moderator/discord_mod_views.py b/api/dashboard/discord_moderator/discord_mod_views.py index 59656149..8384858e 100644 --- a/api/dashboard/discord_moderator/discord_mod_views.py +++ b/api/dashboard/discord_moderator/discord_mod_views.py @@ -7,6 +7,7 @@ from db.task import KarmaActivityLog from django.utils import timezone from utils.utils import DateTimeUtils +from django.db.models import Count, Q class TaskList(APIView): @@ -27,10 +28,37 @@ def get(self, request): tasks = KarmaActivityLog.objects.filter(created_at = date) peerpending = tasks.filter(peer_approved = False).count() appraiserpending = tasks.filter(appraiser_approved = False).count() - data = {'peer-pending':peerpending,'appraiser-pending':appraiserpending} + data = {'peer_pending':peerpending,'appraise_pending':appraiserpending} return CustomResponse(response = data).get_success_response() peerpending = KarmaActivityLog.objects.filter(peer_approved = False).count() appraiserpending = KarmaActivityLog.objects.filter(appraiser_approved = False).count() - data = {'peer-pending':peerpending,'appraiser-pending':appraiserpending} - return CustomResponse(response = data).get_success_response() \ No newline at end of file + data = {'peer_pending':peerpending,'appraise_pending':appraiserpending} + return CustomResponse(response = data).get_success_response() + +class LeaderBoard(APIView): + authentication_classes = [CustomizePermission] + + def get(self, request): + choice = request.query_params.get("option") + if choice == "peer": + data = {} + logs_with_peer_approval = KarmaActivityLog.objects.filter(Q(peer_approved_by__isnull=False) & ~Q(peer_approved_by = '')) + for obj in logs_with_peer_approval: + if data.get(obj.peer_approved_by.fullname) == None: + data[obj.peer_approved_by.fullname] = {"count":0,"muid":obj.peer_approved_by.muid } + data[obj.peer_approved_by.fullname]['count']+=1 + return CustomResponse(response=data).get_success_response() + + elif choice == "appraiser": + data = {} + logs_with_appraiser_approval = KarmaActivityLog.objects.filter(Q(appraiser_approved_by__isnull = False) & ~Q(appraiser_approved_by = '')) + for obj in logs_with_appraiser_approval: + if data.get(obj.appraiser_approved_by.fullname) == None: + data[obj.appraiser_approved_by.fullname] = {"count":0,"muid":obj.peer_approved_by.muid} + data[obj.appraiser_approved_by.fullname]['count']+=1 + return CustomResponse(response=data).get_success_response() + + else: + return CustomResponse(response="Bad Request").get_success_response() + diff --git a/api/dashboard/discord_moderator/serializer.py b/api/dashboard/discord_moderator/serializer.py index c90433f3..9de91d38 100644 --- a/api/dashboard/discord_moderator/serializer.py +++ b/api/dashboard/discord_moderator/serializer.py @@ -1,6 +1,6 @@ from rest_framework import serializers from db.task import KarmaActivityLog - +from db.user import User class KarmaActivityLogSerializer(serializers.ModelSerializer): fullname = serializers.CharField(source = 'user.fullname') @@ -22,4 +22,3 @@ def get_status(self,obj): return "Pending" - diff --git a/api/dashboard/discord_moderator/urls.py b/api/dashboard/discord_moderator/urls.py index ae21d267..d3ba80a6 100644 --- a/api/dashboard/discord_moderator/urls.py +++ b/api/dashboard/discord_moderator/urls.py @@ -5,5 +5,6 @@ urlpatterns = [ path('tasklist/', discord_mod_views.TaskList.as_view()), path('pendingcounts/', discord_mod_views.PendingTasks.as_view()), + path('leaderboard/', discord_mod_views.LeaderBoard.as_view()), ]