diff --git a/api/launchpad/launchpad_views.py b/api/launchpad/launchpad_views.py index 379bb2d5..ab6148e3 100644 --- a/api/launchpad/launchpad_views.py +++ b/api/launchpad/launchpad_views.py @@ -5,7 +5,8 @@ from rest_framework.views import APIView from .serializers import LaunchpadLeaderBoardSerializer, LaunchpadParticipantsSerializer, LaunchpadUserListSerializer,\ - CollegeDataSerializer, LaunchpadUserSerializer, UserProfileUpdateSerializer, LaunchpadUpdateUserSerializer,LaunchPadRankSerializer + CollegeDataSerializer, LaunchpadUserSerializer, UserProfileUpdateSerializer, LaunchpadUpdateUserSerializer,LaunchPadRankSerializer,\ + TaskCompletedLeaderBoardSerializer from api.dashboard.profile.profile_serializer import UserProfileSerializer , LinkSocials ,UserLevelSerializer ,UserLogSerializer from utils.response import CustomResponse @@ -14,7 +15,7 @@ from utils.permission import JWTUtils from db.user import User, UserRoleLink , Role , Socials from db.organization import UserOrganizationLink, Organization -from db.task import KarmaActivityLog , Level +from db.task import KarmaActivityLog, Level, TaskList, Wallet from db.launchpad import LaunchPadUsers, LaunchPadUserCollegeLink , LaunchPad @@ -76,6 +77,50 @@ def get(self, request): data=serializer.data, pagination=paginated_queryset.get("pagination") ) +class TaskCompletedLeaderboard(APIView): + def get(self, request): + + launchpad_tasks = TaskList.objects.filter(event='launchpad').values('id') + + completed_tasks_counts = KarmaActivityLog.objects.filter( + task__event='launchpad', + appraiser_approved=True, + ).values('user').annotate( + completed_tasks=Count('task', distinct=True) + ).filter(completed_tasks=launchpad_tasks.count()) + + completed_users = completed_tasks_counts.values('user') + + wallet_subquery = Wallet.objects.filter( + user=OuterRef('id') + ).values('karma')[:1] + + users = User.objects.filter( + karma_activity_log_user__task__event="launchpad", + karma_activity_log_user__appraiser_approved=True, + id__in=completed_users + ).annotate( + karma=Subquery(wallet_subquery,output_field=IntegerField()), + time_=Max("karma_activity_log_user__created_at"), + ).order_by("-karma", "time_") + + rank_list = list(users) + for index, user in enumerate(rank_list): + user.rank = index + 1 + + paginated_queryset = CommonUtils.get_paginated_queryset( + users, + request, + ["full_name", "karma", "org", "district_name", "state"] + ) + + serializer = TaskCompletedLeaderBoardSerializer( + paginated_queryset.get("queryset"), + many=True + ) + return CustomResponse().paginated_response( + data=serializer.data, pagination=paginated_queryset.get("pagination") + ) class ListParticipantsAPI(APIView): def get(self, request): diff --git a/api/launchpad/serializers.py b/api/launchpad/serializers.py index 45ef931d..5c6083ef 100644 --- a/api/launchpad/serializers.py +++ b/api/launchpad/serializers.py @@ -141,6 +141,14 @@ def get_rank(self, obj): return rank +class TaskCompletedLeaderBoardSerializer(serializers.ModelSerializer): + rank = serializers.IntegerField() + karma = serializers.IntegerField() + + class Meta: + model = User + fields = ("muid", "rank", "full_name", "karma") + class LaunchpadParticipantsSerializer(serializers.ModelSerializer): org = serializers.CharField(allow_null=True, allow_blank=True) diff --git a/api/launchpad/urls.py b/api/launchpad/urls.py index 836fcd19..f59377a8 100644 --- a/api/launchpad/urls.py +++ b/api/launchpad/urls.py @@ -4,6 +4,7 @@ urlpatterns = [ path('leaderboard/', launchpad_views.Leaderboard.as_view()), + path('task-completed-leaderboard/', launchpad_views.TaskCompletedLeaderboard.as_view()), path('list-participants/', launchpad_views.ListParticipantsAPI.as_view()), path('launchpad-details/', launchpad_views.LaunchpadDetailsCount.as_view()), path('college-data/', launchpad_views.CollegeData.as_view()),