diff --git a/api/launchpad/launchpad_views.py b/api/launchpad/launchpad_views.py index ab6148e3..95ec1a4d 100644 --- a/api/launchpad/launchpad_views.py +++ b/api/launchpad/launchpad_views.py @@ -64,6 +64,10 @@ def get(self, request): 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, @@ -89,8 +93,25 @@ def get(self, request): completed_tasks=Count('task', distinct=True) ).filter(completed_tasks=launchpad_tasks.count()) + allowed_org_types = ["College", "School", "Company"] + completed_users = completed_tasks_counts.values('user') + latest_org_link = UserOrganizationLink.objects.filter( + user=OuterRef('id'), + org__org_type__in=allowed_org_types + ).order_by('-created_at').values('org__title')[:1] + + latest_district = UserOrganizationLink.objects.filter( + user=OuterRef('id'), + org__org_type__in=allowed_org_types + ).order_by('-created_at').values('org__district__name')[:1] + + latest_state = UserOrganizationLink.objects.filter( + user=OuterRef('id'), + org__org_type__in=allowed_org_types + ).order_by('-created_at').values('org__district__zone__state__name')[:1] + wallet_subquery = Wallet.objects.filter( user=OuterRef('id') ).values('karma')[:1] @@ -101,6 +122,9 @@ def get(self, request): id__in=completed_users ).annotate( karma=Subquery(wallet_subquery,output_field=IntegerField()), + org=Subquery(latest_org_link), + district_name=Subquery(latest_district), + state=Subquery(latest_state), time_=Max("karma_activity_log_user__created_at"), ).order_by("-karma", "time_") diff --git a/api/launchpad/serializers.py b/api/launchpad/serializers.py index 1966b246..2784664c 100644 --- a/api/launchpad/serializers.py +++ b/api/launchpad/serializers.py @@ -32,7 +32,6 @@ def get_rank(self, obj): ).values('user').annotate( total_karma=Sum('karma') ).values('total_karma') - allowed_org_types = ["College", "School", "Company"] intro_task_completed_users = KarmaActivityLog.objects.filter( task__event='launchpad', @@ -40,30 +39,12 @@ def get_rank(self, obj): task__hashtag='#lp24-introduction', ).values('user') - latest_org_link = UserOrganizationLink.objects.filter( - user=OuterRef('id'), - org__org_type__in=allowed_org_types - ).order_by('-created_at').values('org__title')[:1] - - latest_district = UserOrganizationLink.objects.filter( - user=OuterRef('id'), - org__org_type__in=allowed_org_types - ).order_by('-created_at').values('org__district__name')[:1] - - latest_state = UserOrganizationLink.objects.filter( - user=OuterRef('id'), - org__org_type__in=allowed_org_types - ).order_by('-created_at').values('org__district__zone__state__name')[:1] - users = User.objects.filter( karma_activity_log_user__task__event="launchpad", karma_activity_log_user__appraiser_approved=True, id__in=intro_task_completed_users ).annotate( karma=Subquery(total_karma_subquery, output_field=IntegerField()), - org=Subquery(latest_org_link), - district_name=Subquery(latest_district), - state=Subquery(latest_state), time_=Max("karma_activity_log_user__created_at"), ).order_by("-karma", "time_") @@ -77,7 +58,7 @@ def get_rank(self, obj): return rank class LaunchpadLeaderBoardSerializer(serializers.ModelSerializer): - rank = serializers.SerializerMethodField() + rank = serializers.IntegerField() karma = serializers.IntegerField() actual_karma = serializers.IntegerField(source="wallet_user.karma", default=None) org = serializers.CharField(allow_null=True, allow_blank=True) @@ -88,68 +69,21 @@ class LaunchpadLeaderBoardSerializer(serializers.ModelSerializer): class Meta: model = User fields = ("rank", "full_name", "actual_karma", "karma", "org", "district_name", "state","launchpad_id") - - def get_rank(self, obj): - total_karma_subquery = KarmaActivityLog.objects.filter( - user=OuterRef('id'), - task__event='launchpad', - appraiser_approved=True, - ).values('user').annotate( - total_karma=Sum('karma') - ).values('total_karma') - allowed_org_types = ["College", "School", "Company"] - - intro_task_completed_users = KarmaActivityLog.objects.filter( - task__event='launchpad', - appraiser_approved=True, - task__hashtag='#lp24-introduction', - ).values('user') - - latest_org_link = UserOrganizationLink.objects.filter( - user=OuterRef('id'), - org__org_type__in=allowed_org_types - ).order_by('-created_at').values('org__title')[:1] - - latest_district = UserOrganizationLink.objects.filter( - user=OuterRef('id'), - org__org_type__in=allowed_org_types - ).order_by('-created_at').values('org__district__name')[:1] - - latest_state = UserOrganizationLink.objects.filter( - user=OuterRef('id'), - org__org_type__in=allowed_org_types - ).order_by('-created_at').values('org__district__zone__state__name')[:1] - - users = User.objects.filter( - karma_activity_log_user__task__event="launchpad", - karma_activity_log_user__appraiser_approved=True, - id__in=intro_task_completed_users - ).annotate( - karma=Subquery(total_karma_subquery, output_field=IntegerField()), - org=Subquery(latest_org_link), - district_name=Subquery(latest_district), - state=Subquery(latest_state), - time_=Max("karma_activity_log_user__created_at"), - ).order_by("-karma", "time_") - # high complexity - rank = 0 - for data in users: - rank += 1 - if data.id == obj.id: - break - - return rank + class TaskCompletedLeaderBoardSerializer(serializers.ModelSerializer): rank = serializers.IntegerField() karma = serializers.IntegerField() is_public = serializers.BooleanField(source='user_settings_user.is_public',default=False) + org = serializers.CharField(allow_null=True, allow_blank=True) + district_name = serializers.CharField(allow_null=True, allow_blank=True) + state = serializers.CharField(allow_null=True, allow_blank=True) class Meta: model = User - fields = ("muid", "is_public", "rank", "full_name", "karma") - + fields = ("muid", "is_public", "rank", "full_name", "karma", "org", "district_name", "state", "profile_pic") + class LaunchpadParticipantsSerializer(serializers.ModelSerializer): org = serializers.CharField(allow_null=True, allow_blank=True)