mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-17 02:45:08 +00:00
leaderboard ranking
This commit is contained in:
Binary file not shown.
@ -1,3 +1,53 @@
|
|||||||
"""
|
"""
|
||||||
web_admin analytics serializer file
|
web_admin analytics serializer file
|
||||||
"""
|
"""
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from junior.models import JuniorPoints, Junior
|
||||||
|
from web_admin.serializers.user_management_serializer import JuniorSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class JuniorLeaderboardSerializer(serializers.ModelSerializer):
|
||||||
|
name = serializers.SerializerMethodField()
|
||||||
|
first_name = serializers.SerializerMethodField()
|
||||||
|
last_name = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""
|
||||||
|
meta class
|
||||||
|
"""
|
||||||
|
model = Junior
|
||||||
|
fields = ('id', 'name', 'first_name', 'last_name', 'is_active', 'image')
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_name(obj):
|
||||||
|
"""
|
||||||
|
:param obj: junior object
|
||||||
|
:return: full name
|
||||||
|
"""
|
||||||
|
return f"{obj.auth.first_name} {obj.auth.last_name}" if obj.auth.last_name else obj.auth.first_name
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_first_name(obj):
|
||||||
|
"""
|
||||||
|
:param obj: junior object
|
||||||
|
:return: first name
|
||||||
|
"""
|
||||||
|
return obj.auth.first_name
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_last_name(obj):
|
||||||
|
"""
|
||||||
|
:param obj: junior object
|
||||||
|
:return: last name
|
||||||
|
"""
|
||||||
|
return obj.auth.last_name
|
||||||
|
|
||||||
|
|
||||||
|
class LeaderboardSerializer(serializers.ModelSerializer):
|
||||||
|
junior = JuniorLeaderboardSerializer()
|
||||||
|
rank = serializers.IntegerField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = JuniorPoints
|
||||||
|
fields = ('total_points', 'rank', 'junior')
|
||||||
|
@ -7,12 +7,16 @@ from rest_framework.viewsets import GenericViewSet
|
|||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.db.models import Count
|
from django.db.models import Count, OuterRef, Subquery, Sum
|
||||||
from django.db.models.functions import TruncDate
|
from django.db.models.functions import TruncDate
|
||||||
|
from django.db.models import F, Window
|
||||||
|
from django.db.models.functions.window import Rank
|
||||||
|
|
||||||
from account.utils import custom_response
|
from account.utils import custom_response
|
||||||
from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED, EXPIRED
|
from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED, EXPIRED
|
||||||
from guardian.models import JuniorTask
|
from guardian.models import JuniorTask
|
||||||
|
from junior.models import JuniorPoints
|
||||||
|
from web_admin.serializers.analytics_serializer import LeaderboardSerializer
|
||||||
|
|
||||||
USER = get_user_model()
|
USER = get_user_model()
|
||||||
|
|
||||||
@ -59,7 +63,11 @@ class AnalyticsViewSet(GenericViewSet):
|
|||||||
|
|
||||||
@action(methods=['get'], url_name='new-signups', url_path='new-signups', detail=False)
|
@action(methods=['get'], url_name='new-signups', url_path='new-signups', detail=False)
|
||||||
def new_signups(self, request, *args, **kwargs):
|
def new_signups(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
api method to get new signups
|
||||||
|
:param request: query params {start_date and end_date}, date format (yyyy-mm-dd)
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
end_date = datetime.date.today()
|
end_date = datetime.date.today()
|
||||||
start_date = end_date - datetime.timedelta(days=6)
|
start_date = end_date - datetime.timedelta(days=6)
|
||||||
|
|
||||||
@ -76,7 +84,11 @@ class AnalyticsViewSet(GenericViewSet):
|
|||||||
|
|
||||||
@action(methods=['get'], url_name='assign-tasks', url_path='assign-tasks', detail=False)
|
@action(methods=['get'], url_name='assign-tasks', url_path='assign-tasks', detail=False)
|
||||||
def assign_tasks_report(self, request, *args, **kwargs):
|
def assign_tasks_report(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
api method to get assign tasks
|
||||||
|
:param request: query params {start_date and end_date}, date format (yyyy-mm-dd)
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
end_date = datetime.date.today()
|
end_date = datetime.date.today()
|
||||||
start_date = end_date - datetime.timedelta(days=6)
|
start_date = end_date - datetime.timedelta(days=6)
|
||||||
|
|
||||||
@ -96,3 +108,21 @@ class AnalyticsViewSet(GenericViewSet):
|
|||||||
}
|
}
|
||||||
|
|
||||||
return custom_response(None, data)
|
return custom_response(None, data)
|
||||||
|
|
||||||
|
@action(methods=['get'], url_name='junior-leaderboard', url_path='junior-leaderboard', detail=False,
|
||||||
|
serializer_class=LeaderboardSerializer)
|
||||||
|
def junior_leaderboard(self, request):
|
||||||
|
"""
|
||||||
|
|
||||||
|
:param request:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
# queryset = JuniorPoints.objects.all().order_by('-total_points', 'junior__created_at')
|
||||||
|
queryset = JuniorPoints.objects.prefetch_related('junior', 'junior__auth').annotate(rank=Window(
|
||||||
|
expression=Rank(),
|
||||||
|
order_by=[F('total_points').desc(), 'junior__created_at']
|
||||||
|
)).order_by('-total_points', 'junior__created_at')
|
||||||
|
paginator = self.pagination_class()
|
||||||
|
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||||
|
serializer = self.serializer_class(paginated_queryset, many=True)
|
||||||
|
return custom_response(None, serializer.data)
|
||||||
|
@ -34,7 +34,7 @@ class UserManagementViewSet(GenericViewSet, mixins.ListModelMixin,
|
|||||||
is_superuser=False).prefetch_related('guardian_profile',
|
is_superuser=False).prefetch_related('guardian_profile',
|
||||||
'junior_profile'
|
'junior_profile'
|
||||||
).exclude(junior_profile__isnull=True,
|
).exclude(junior_profile__isnull=True,
|
||||||
guardian_profile__isnull=True).order_by('date_joined')
|
guardian_profile__isnull=True).order_by('-date_joined')
|
||||||
filter_backends = (SearchFilter,)
|
filter_backends = (SearchFilter,)
|
||||||
search_fields = ['first_name', 'last_name']
|
search_fields = ['first_name', 'last_name']
|
||||||
http_method_names = ['get', 'post', 'patch']
|
http_method_names = ['get', 'post', 'patch']
|
||||||
|
@ -177,6 +177,31 @@ AUTH_PASSWORD_VALIDATORS = [
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
LOGGING = {
|
||||||
|
"version": 1,
|
||||||
|
"filters": {
|
||||||
|
"require_debug_true": {
|
||||||
|
"()": "django.utils.log.RequireDebugTrue"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"handlers": {
|
||||||
|
"console": {
|
||||||
|
"level": "DEBUG",
|
||||||
|
"filters": [
|
||||||
|
"require_debug_true"
|
||||||
|
],
|
||||||
|
"class": "logging.StreamHandler"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"loggers": {
|
||||||
|
"django.db.backends": {
|
||||||
|
"level": "DEBUG",
|
||||||
|
"handlers": [
|
||||||
|
"console"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Internationalization
|
# Internationalization
|
||||||
# https://docs.djangoproject.com/en/3.0/topics/i18n/
|
# https://docs.djangoproject.com/en/3.0/topics/i18n/
|
||||||
|
Reference in New Issue
Block a user