diff --git a/api/common/common_views.py b/api/common/common_views.py index a6cf6ddc..7fddbf3c 100644 --- a/api/common/common_views.py +++ b/api/common/common_views.py @@ -23,9 +23,41 @@ def get(self, request): date = request.query_params.get("date") if date: learning_circle_count = LearningCircle.objects.filter(created_at__gt=date).count() - total_no_enrollment = UserCircleLink.objects.filter(accepted=True, - user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value, - created_at__gt=date).count() + # total_no_enrollment = UserCircleLink.objects.filter(accepted=True, + # user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value, + # created_at__gt=date).count() + total_no_enrollment = (UserCircleLink.objects.filter(accepted=True, + user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value, + created_at__gt=date).values( + first_name=F("user__first_name"), + last_name=F("user__last_name"), + email=F("user__email"), + muid=F("user__muid"), + circle_name=F("circle__name"), + 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( + "user__karma_activity_log_user__task__karma", + filter=Q( + user__karma_activity_log_user__task__ig=F("circle__ig") + ), + ), + dwms_id=Case( + When( + user__integration_authorization_user__integration__name=IntegrationType.KKEM.value, + then=F( + "user__integration_authorization_user__additional_field" + ), + ), + default=Value(None, output_field=CharField()), + output_field=CharField(), + ), + ) + ).count() user_circle_link_count = UserCircleLink.objects.filter(created_at__gt=date, circle=OuterRef('pk')).values( 'circle_id').annotate( total_users=Count('id')).values('total_users') @@ -51,9 +83,39 @@ def get(self, request): ) else: learning_circle_count = LearningCircle.objects.all().count() - total_no_enrollment = UserCircleLink.objects.filter(accepted=True, - user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value).count() + # total_no_enrollment = UserCircleLink.objects.filter(accepted=True, + # user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value).count() + total_no_enrollment = (UserCircleLink.objects.filter(accepted=True, + user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value).values( + first_name=F("user__first_name"), + last_name=F("user__last_name"), + email=F("user__email"), + muid=F("user__muid"), + circle_name=F("circle__name"), + 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( + "user__karma_activity_log_user__task__karma", + filter=Q( + user__karma_activity_log_user__task__ig=F("circle__ig") + ), + ), + dwms_id=Case( + When( + user__integration_authorization_user__integration__name=IntegrationType.KKEM.value, + then=F( + "user__integration_authorization_user__additional_field" + ), + ), + default=Value(None, output_field=CharField()), + output_field=CharField(), + ), + ) + ).count() user_circle_link_count = UserCircleLink.objects.filter(circle=OuterRef('pk')).values('circle_id').annotate( total_users=Count('id')).values('total_users') @@ -213,40 +275,6 @@ def get(self, request): return CommonUtils.generate_csv(student_info_data, "Learning Circle Report") -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=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, - request, - search_fields=["org_title", "learning_circle_count", "user_count"], - sort_fields={"org_title": "org_title", "learning_circle_count": "learning_circle_count", - "user_count": "user_count"}, - is_pagination=False - ) - - lc_report = CollegeInfoSerializer(paginated_queryset, many=True).data - - return CommonUtils.generate_csv(lc_report, "Learning Circle Report") - - class CollegeWiseLcReport(APIView): def get(self, request): date = request.query_params.get('date') @@ -304,6 +332,40 @@ 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=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, + request, + search_fields=["org_title", "learning_circle_count", "user_count"], + sort_fields={"org_title": "org_title", "learning_circle_count": "learning_circle_count", + "user_count": "user_count"}, + is_pagination=False + ) + + lc_report = CollegeInfoSerializer(paginated_queryset, many=True).data + + return CommonUtils.generate_csv(lc_report, "Learning Circle Report") + + class LearningCircleEnrollment(APIView): def get(self, request): diff --git a/api/dashboard/profile/profile_serializer.py b/api/dashboard/profile/profile_serializer.py index 96120200..28394607 100644 --- a/api/dashboard/profile/profile_serializer.py +++ b/api/dashboard/profile/profile_serializer.py @@ -1,7 +1,6 @@ import uuid from decouple import config as decouple_config -from django.core.files.storage import FileSystemStorage from django.db import transaction from django.db.models import F, Sum, Q from rest_framework import serializers @@ -130,7 +129,7 @@ def get_rank(self, obj): Wallet.objects.filter(karma__gte=user_karma) .exclude( Q(user__user_role_link_user__role__title__in=[ - RoleType.ENABLER.value, RoleType.MENTOR.value]) + RoleType.ENABLER.value, RoleType.MENTOR.value]) ).order_by('-karma') ) @@ -154,14 +153,14 @@ def get_interest_groups(self, obj): if KarmaActivityLog.objects.filter( task__ig=ig_link.ig, user=obj, appraiser_approved=True ) - .aggregate(Sum("karma")) - .get("karma__sum") - is None + .aggregate(Sum("karma")) + .get("karma__sum") + is None else KarmaActivityLog.objects.filter( task__ig=ig_link.ig, user=obj, appraiser_approved=True ) - .aggregate(Sum("karma")) - .get("karma__sum") + .aggregate(Sum("karma")) + .get("karma__sum") ) interest_groups.append( {"id": ig_link.ig.id, "name": ig_link.ig.name, "karma": total_ig_karma} @@ -219,35 +218,32 @@ def get_role(self, obj): return ["Learner"] if len(roles) == 0 else roles def get_rank(self, obj): - roles = self.context.get("roles") + roles = self.get_roles(obj) user_karma = obj.wallet_user.karma if RoleType.MENTOR.value in roles: ranks = Wallet.objects.filter( user__user_role_link_user__verified=True, user__user_role_link_user__role__title=RoleType.MENTOR.value, karma__gte=user_karma, - ).count() + ).order_by('-karma', '-updated_at', 'created_at') elif RoleType.ENABLER.value in roles: ranks = Wallet.objects.filter( user__user_role_link_user__verified=True, user__user_role_link_user__role__title=RoleType.ENABLER.value, karma__gte=user_karma, - ).count() + ).order_by('-karma', '-updated_at', 'created_at') else: ranks = ( - Wallet.objects.filter( - karma__gte=user_karma, user__user_role_link_user__verified=True) + Wallet.objects.filter(karma__gte=user_karma) .exclude( - Q( - user__user_role_link_user__role__title__in=[ - RoleType.ENABLER.value, - RoleType.MENTOR.value, - ] - ) - ) - .count() + Q(user__user_role_link_user__role__title__in=[ + RoleType.ENABLER.value, RoleType.MENTOR.value]) + ).order_by('-karma') ) - return ranks if ranks > 0 else None + + for count, _rank in enumerate(ranks, start=1): + if obj == _rank.user: + return count def get_karma(self, obj): return total_karma.karma if (total_karma := obj.wallet_user) else None @@ -402,7 +398,7 @@ def create_karma_activity_log(task_title, karma_value): dl = WebHookActions.SEPARATOR.value discord_id = User.objects.get(id=user_id).discord_id value = f"{task.hashtag}{dl}{karma_value}{dl}{discord_id}{dl}{karma_log.id}" - + DiscordWebhooks.general_updates( WebHookCategory.KARMA_INFO.value, WebHookActions.UPDATE.value, @@ -411,7 +407,7 @@ def create_karma_activity_log(task_title, karma_value): else: KarmaActivityLog.objects.filter( task_id=task.id, user_id=user_id - ).delete() + ).first().delete() Wallet.objects.filter(user_id=user_id).update( karma=F("karma") + karma_value, updated_by_id=user_id diff --git a/api/url_shortener/url_shortener_view.py b/api/url_shortener/url_shortener_view.py index 85a2e1dd..0cfcb365 100644 --- a/api/url_shortener/url_shortener_view.py +++ b/api/url_shortener/url_shortener_view.py @@ -43,7 +43,7 @@ def post(self, request): [RoleType.ADMIN.value, RoleType.FELLOW.value, RoleType.ASSOCIATE.value] ) def get(self, request): - url_shortener_objects = UrlShortener.objects.all() + url_shortener_objects = UrlShortener.objects.all().order_by('-created_at') paginated_queryset = CommonUtils.get_paginated_queryset( url_shortener_objects,