Repository: Cloud-CV/EvalAI

Issue:
Bug in backend logic to show remaining submissions on submit tab



Discussion:

Resolving patch:
diff --git a/apps/jobs/views.py b/apps/jobs/views.py
--- a/apps/jobs/views.py
+++ b/apps/jobs/views.py
@@ -254,6 +254,14 @@ def leaderboard(request, challenge_phase_split_id):
 @authentication_classes((ExpiringTokenAuthentication,))
 def get_remaining_submissions(request, challenge_phase_pk, challenge_pk):
 
+    '''
+    Returns the number of remaining submissions that a participant can
+    do per day and in total to a particular challenge phase of a
+    challenge.
+    '''
+
+    # significance of get_challenge_model() here to check
+    # if the challenge exists or not
     get_challenge_model(challenge_pk)
 
     challenge_phase = get_challenge_phase_model(challenge_phase_pk)
@@ -266,26 +274,32 @@ def get_remaining_submissions(request, challenge_phase_pk, challenge_pk):
         response_data = {'error': 'You haven\'t participated in the challenge'}
         return Response(response_data, status=status.HTTP_403_FORBIDDEN)
 
-    max_submission_per_day = challenge_phase.max_submissions_per_day
+    max_submissions_per_day_count = challenge_phase.max_submissions_per_day
 
-    max_submission = challenge_phase.max_submissions
+    max_submissions_count = challenge_phase.max_submissions
 
-    submissions_done_today_count = Submission.objects.filter(
+    submissions_done = Submission.objects.filter(
         challenge_phase__challenge=challenge_pk,
         challenge_phase=challenge_phase_pk,
-        participant_team=participant_team_pk,
-        submitted_at__gte=timezone.now().date()).count()
+        participant_team=participant_team_pk)
 
-    failed_submissions_count = Submission.objects.filter(
-        challenge_phase__challenge=challenge_pk,
-        challenge_phase=challenge_phase_pk,
-        participant_team=participant_team_pk,
-        status=Submission.FAILED,
-        submitted_at__gte=timezone.now().date()).count()
+    failed_submissions = submissions_done.filter(
+        status=Submission.FAILED)
+
+    submissions_done_today = submissions_done.filter(
+        submitted_at__gte=timezone.now().date())
 
-    # Checks if today's successfull submission is greater than or equal to max submission per day.
-    if ((submissions_done_today_count - failed_submissions_count) >= max_submission_per_day
-            or (max_submission_per_day == 0)):
+    failed_submissions_done_today = submissions_done_today.filter(
+        status=Submission.FAILED)
+
+    submissions_done_count = submissions_done.count()
+    failed_submissions_count = failed_submissions.count()
+    submissions_done_today_count = submissions_done_today.count()
+    failed_submissions_done_today_count = failed_submissions_done_today.count()
+
+    # Checks if #today's successful submission is greater than or equal to max submission per day
+    if ((submissions_done_today_count - failed_submissions_done_today_count) >= max_submissions_per_day_count
+            or (max_submissions_per_day_count == 0)):
         # Get the UTC time of the instant when the above condition is true.
         date_time_now = timezone.now()
         # Calculate the next day's date.
@@ -303,13 +317,14 @@ def get_remaining_submissions(request, challenge_phase_pk, challenge_pk):
         return Response(response_data, status=status.HTTP_200_OK)
     else:
         # Calculate the remaining submissions for today.
-        remaining_submissions_today_count = (max_submission_per_day -
+        remaining_submissions_today_count = (max_submissions_per_day_count -
                                              (submissions_done_today_count -
-                                              failed_submissions_count)
+                                              failed_submissions_done_today_count)
                                              )
+
         # calculate the remaining submissions from total submissions.
-        remaining_submission_count = max_submission - \
-            (submissions_done_today_count - failed_submissions_count)
+        remaining_submission_count = max_submissions_count - \
+            (submissions_done_count - failed_submissions_count)
         # Return the above calculated data.
         response_data = {'remaining_submissions_today_count': remaining_submissions_today_count,
                          'remaining_submissions': remaining_submission_count


Test patch:
diff --git a/tests/unit/jobs/test_views.py b/tests/unit/jobs/test_views.py
--- a/tests/unit/jobs/test_views.py
+++ b/tests/unit/jobs/test_views.py
@@ -80,6 +80,8 @@ def setUp(self):
                 name='Challenge Phase',
                 description='Description for Challenge Phase',
                 leaderboard_public=False,
+                max_submissions_per_day=10,
+                max_submissions=100,
                 is_public=True,
                 start_date=timezone.now() - timedelta(days=2),
                 end_date=timezone.now() + timedelta(days=1),
@@ -447,13 +449,30 @@ def test_get_remaining_submission_when_participant_team_hasnt_participated_in_ch
         self.assertEqual(response.data, expected)
         self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
 
-    def test_get_remaining_submission(self):
+    def test_get_remaining_submission_when_submission_made_three_days_back(self):
         self.url = reverse_lazy('jobs:get_remaining_submissions',
                                 kwargs={'challenge_phase_pk': self.challenge_phase.pk,
                                         'challenge_pk': self.challenge.pk})
         expected = {
-            'remaining_submissions_today_count': 99998,
-            'remaining_submissions': 99998
+            'remaining_submissions_today_count': 9,
+            'remaining_submissions': 98
+        }
+
+        self.challenge.participant_teams.add(self.participant_team)
+        self.challenge.save()
+        self.submission1.submitted_at = timezone.now() - timedelta(days=3)
+        self.submission1.save()
+        response = self.client.get(self.url, {})
+        self.assertEqual(response.data, expected)
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+    def test_get_remaining_submission_when__submission_made_today(self):
+        self.url = reverse_lazy('jobs:get_remaining_submissions',
+                                kwargs={'challenge_phase_pk': self.challenge_phase.pk,
+                                        'challenge_pk': self.challenge.pk})
+        expected = {
+            'remaining_submissions_today_count': 8,
+            'remaining_submissions': 98
         }
 
         self.challenge.participant_teams.add(self.participant_team)