mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-16 02:16:16 +00:00
leaderboard ranking
This commit is contained in:
@ -7,12 +7,16 @@ from rest_framework.viewsets import GenericViewSet
|
||||
from rest_framework.decorators import action
|
||||
from django.contrib.auth import get_user_model
|
||||
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 import F, Window
|
||||
from django.db.models.functions.window import Rank
|
||||
|
||||
from account.utils import custom_response
|
||||
from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED, EXPIRED
|
||||
from guardian.models import JuniorTask
|
||||
from junior.models import JuniorPoints
|
||||
from web_admin.serializers.analytics_serializer import LeaderboardSerializer
|
||||
|
||||
USER = get_user_model()
|
||||
|
||||
@ -59,7 +63,11 @@ class AnalyticsViewSet(GenericViewSet):
|
||||
|
||||
@action(methods=['get'], url_name='new-signups', url_path='new-signups', detail=False)
|
||||
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()
|
||||
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)
|
||||
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()
|
||||
start_date = end_date - datetime.timedelta(days=6)
|
||||
|
||||
@ -96,3 +108,21 @@ class AnalyticsViewSet(GenericViewSet):
|
||||
}
|
||||
|
||||
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)
|
||||
|
Reference in New Issue
Block a user