Skip to content

Commit

Permalink
Merge pull request #2165 from gtech-mulearn/dev-server
Browse files Browse the repository at this point in the history
Dev server
  • Loading branch information
jelanmathewjames authored Jul 13, 2024
2 parents bba312b + 14faadc commit bd4ff11
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
8 changes: 4 additions & 4 deletions api/dashboard/profile/profile_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from db.user import Role, Socials, User, UserRoleLink, UserSettings
from utils.permission import CustomizePermission, JWTUtils
from utils.response import CustomResponse
from utils.types import WebHookActions, WebHookCategory
from utils.types import WebHookActions, WebHookCategory, TFPTasksHashtags
from utils.utils import DiscordWebhooks

from . import profile_serializer
Expand Down Expand Up @@ -426,11 +426,11 @@ class BadgesAPI(APIView):
def get(self, request, muid):
try:
user = User.objects.get(muid=muid)
hastags = ["#tfp2.0-scratch"]
hastags = TFPTasksHashtags.get_all_values()
response_data = {"full_name": user.full_name, "completed_tasks":[]}
for tag in hastags:
if KarmaActivityLog.objects.filter(user=user, task__hashtag=tag).exists():
response_data["completed_tasks"] = tag
if log := KarmaActivityLog.objects.filter(user=user, task__hashtag=tag).first():
response_data["completed_tasks"] = log.task__title
return CustomResponse(response=response_data).get_success_response()
except User.DoesNotExist:
return CustomResponse(
Expand Down
15 changes: 13 additions & 2 deletions api/launchpad/launchpad_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework.views import APIView

from .serializers import LaunchpadLeaderBoardSerializer, LaunchpadParticipantsSerializer, LaunchpadUserListSerializer,\
CollegeDataSerializer, LaunchpadUserSerializer, UserProfileUpdateSerializer, LaunchpadUpdateUserSerializer
CollegeDataSerializer, LaunchpadUserSerializer, UserProfileUpdateSerializer, LaunchpadUpdateUserSerializer,LaunchPadRankSerializer
from api.dashboard.profile.profile_serializer import UserProfileSerializer , LinkSocials ,UserLevelSerializer ,UserLogSerializer

from utils.response import CustomResponse
Expand Down Expand Up @@ -558,7 +558,18 @@ def get(self, request, launchpad_id=None):
if response:
return response
serializer = UserProfileSerializer(user, many=False)
return CustomResponse(response=serializer.data).get_success_response()
launchpad_karma = KarmaActivityLog.objects.filter(
user=user,
task__event='launchpad',
appraiser_approved=True,
).aggregate(total_karma=Sum('karma'))['total_karma'] or 0
rank_serializer = LaunchPadRankSerializer(user)
launchpad_rank = rank_serializer.data['launchpad_rank']

data = serializer.data
data['launchpad_karma'] = launchpad_karma
data['launchpad_rank'] = launchpad_rank
return CustomResponse(response=data).get_success_response()

class GetSocialsAPI(BaseAPI):
def get(self, request, launchpad_id=None):
Expand Down
65 changes: 62 additions & 3 deletions api/launchpad/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,76 @@
class LaunchPadIDSerializer(serializers.ModelSerializer):
class Meta:
model = LaunchPad
fields = ('launchpad_id')

fields = ['launchpad_id']
def to_representation(self, instance):
return instance.launchpad_id

class LaunchPadRankSerializer(serializers.ModelSerializer):
launchpad_rank = serializers.SerializerMethodField('get_rank')
class Meta:
model = User
fields = ['launchpad_rank']

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 LaunchpadLeaderBoardSerializer(serializers.ModelSerializer):
rank = serializers.SerializerMethodField()
karma = serializers.IntegerField()
actual_karma = serializers.IntegerField(source="wallet_user.karma", default=None)
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)
launchpad_id = LaunchPadIDSerializer(read_only=True)
launchpad_id = LaunchPadIDSerializer(source='launchpad_user.first', read_only=True)

class Meta:
model = User
Expand Down
7 changes: 7 additions & 0 deletions utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ class LaunchPadRoles(Enum):
@classmethod
def get_all_values(cls):
return [member.value for member in cls]

class TFPTasksHashtags(Enum):
SCRATCH = '#tfp2.0-scratch'

@classmethod
def get_all_values(cls):
return [member.value for member in cls]

DEFAULT_HACKATHON_FORM_FIELDS = {
'name': 'system',
Expand Down

0 comments on commit bd4ff11

Please sign in to comment.