Skip to content

Commit

Permalink
discord leaderboard minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
adnankattekaden committed Dec 6, 2023
1 parent 10c7abf commit 39c31e0
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions api/dashboard/discord_moderator/discord_mod_views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from collections import defaultdict

from django.db.models import Q
from rest_framework.views import APIView
from utils.permission import CustomizePermission, JWTUtils, role_required

from db.task import KarmaActivityLog
from utils.permission import CustomizePermission
from utils.response import CustomResponse
from utils.types import RoleType
from utils.utils import CommonUtils
from .serializer import KarmaActivityLogSerializer
from db.task import KarmaActivityLog
from django.utils import timezone
from utils.utils import DateTimeUtils
from django.db.models import Count, Q


class TaskList(APIView):
Expand All @@ -25,40 +24,43 @@ class PendingTasks(APIView):
def get(self, request):
date = request.query_params.get("date")
if date:
tasks = KarmaActivityLog.objects.filter(created_at = date)
peerpending = tasks.filter(peer_approved = False).count()
appraiserpending = tasks.filter(appraiser_approved = False).count()
data = {'peer_pending':peerpending,'appraise_pending':appraiserpending}
return CustomResponse(response = data).get_success_response()

peerpending = KarmaActivityLog.objects.filter(peer_approved = False).count()
appraiserpending = KarmaActivityLog.objects.filter(appraiser_approved = False).count()
data = {'peer_pending':peerpending,'appraise_pending':appraiserpending}
return CustomResponse(response = data).get_success_response()

tasks = KarmaActivityLog.objects.filter(created_at=date)
peerpending = tasks.filter(peer_approved=False).count()
appraiserpending = tasks.filter(appraiser_approved=False).count()
data = {'peer_pending': peerpending, 'appraise_pending': appraiserpending}
return CustomResponse(response=data).get_success_response()

peerpending = KarmaActivityLog.objects.filter(peer_approved=False).count()
appraiserpending = KarmaActivityLog.objects.filter(appraiser_approved=False).count()
data = {'peer_pending': peerpending, 'appraise_pending': appraiserpending}
return CustomResponse(response=data).get_success_response()


class LeaderBoard(APIView):
authentication_classes = [CustomizePermission]

def get(self, request):
choice = request.query_params.get("option")
if choice == "peer":
data = {}
logs_with_peer_approval = KarmaActivityLog.objects.filter(Q(peer_approved_by__isnull=False) & ~Q(peer_approved_by = ''))
for obj in logs_with_peer_approval:
if data.get(obj.peer_approved_by.fullname) == None:
data[obj.peer_approved_by.fullname] = {"count":0,"muid":obj.peer_approved_by.muid }
data[obj.peer_approved_by.fullname]['count']+=1
return CustomResponse(response=data).get_success_response()

elif choice == "appraiser":
data = {}
logs_with_appraiser_approval = KarmaActivityLog.objects.filter(Q(appraiser_approved_by__isnull = False) & ~Q(appraiser_approved_by = ''))
for obj in logs_with_appraiser_approval:
if data.get(obj.appraiser_approved_by.fullname) == None:
data[obj.appraiser_approved_by.fullname] = {"count":0,"muid":obj.peer_approved_by.muid}
data[obj.appraiser_approved_by.fullname]['count']+=1
return CustomResponse(response=data).get_success_response()

else:
return CustomResponse(response="Bad Request").get_success_response()


if choice not in ["peer", "appraiser"]:
return CustomResponse(response="Bad Request").get_failure_response()

approval_field = f"{choice}_approved_by"
logs_with_approval = KarmaActivityLog.objects.filter(
Q(**{f"{approval_field}__isnull": False}) & ~Q(**{approval_field: ''})
)

data = defaultdict(lambda: {"count": 0, "muid": None})

for obj in logs_with_approval:
approver_name = getattr(obj, approval_field).fullname
if data[approver_name]["muid"] is None:
data[approver_name]["muid"] = getattr(obj, approval_field).muid
data[approver_name]["count"] += 1

response_data = [
{"name": name, "count": info["count"], "muid": info["muid"]}
for name, info in data.items()
]

return CustomResponse(response=response_data).get_success_response()

0 comments on commit 39c31e0

Please sign in to comment.