Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev server #2193

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions api/launchpad/launchpad_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]
Expand All @@ -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_")

Expand Down
80 changes: 7 additions & 73 deletions api/launchpad/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,19 @@ 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',
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_")

Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading