Skip to content

Commit

Permalink
[Backend] Add submissions metrics by PK (#4353)
Browse files Browse the repository at this point in the history
  • Loading branch information
gchhablani authored Apr 25, 2024
1 parent 68c1c3a commit 6535fe9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apps/challenges/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
views.get_all_challenges_submission_metrics,
name="get_all_challenges_submission_metrics",
),
url(
r"^challenge/get_submission_metrics_by_pk/(?P<pk>[0-9]+)/$",
views.get_challenge_submission_metrics_by_pk,
name="get_challenge_submission_metrics_by_pk",
),
url(
r"^challenges/participated/(?P<challenge_time>[A-Za-z]+)/$",
views.get_all_participated_challenges,
Expand Down
24 changes: 24 additions & 0 deletions apps/challenges/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,30 @@ def get_all_challenges_submission_metrics(request):
return Response(submission_metrics, status=status.HTTP_200_OK)


@api_view(["GET"])
@throttle_classes([AnonRateThrottle])
def get_challenge_submission_metrics_by_pk(request, pk):
"""
Returns the submission metrics for a given challenge by primary key and their phases
"""
if not is_user_a_staff(request.user):
response_data = {"error": "Sorry, you are not authorized to make this request"}
return Response(response_data, status=status.HTTP_403_FORBIDDEN)
challenge = get_challenge_model(pk)
challenge_phases = ChallengePhase.objects.filter(challenge=challenge)
submission_metrics = {}

submission_statuses = [status[0] for status in Submission.STATUS_OPTIONS]

# Fetch challenge phases for the challenge
challenge_phases = ChallengePhase.objects.filter(challenge=challenge)
for submission_status in submission_statuses:
count = Submission.objects.filter(challenge_phase__in=challenge_phases, status=submission_status).count()
submission_metrics[submission_status] = count

return Response(submission_metrics, status=status.HTTP_200_OK)


@api_view(["GET"])
@throttle_classes([AnonRateThrottle])
def get_featured_challenges(request):
Expand Down

0 comments on commit 6535fe9

Please sign in to comment.