We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Bug in last submission datetime analysis API
**IndexError:** list index out of range

**Culprit:**
/api/analytics/challenge/{challenge_pk}/challenge_phase/{challenge_phase_pk}/last_submission_datetime_analysis/


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `apps/analytics/views.py`
Content:
```
1 from datetime import timedelta
2 
3 from django.utils import timezone
4 
5 from rest_framework import permissions, status
6 from rest_framework.decorators import (api_view,
7                                        authentication_classes,
8                                        permission_classes,
9                                        throttle_classes,)
10 from rest_framework.response import Response
11 from rest_framework_expiring_authtoken.authentication import (ExpiringTokenAuthentication,)
12 from rest_framework.throttling import UserRateThrottle
13 
14 from accounts.permissions import HasVerifiedEmail
15 
16 from challenges.permissions import IsChallengeCreator
17 from challenges.utils import get_challenge_model, get_challenge_phase_model
18 from jobs.models import Submission
19 from jobs.serializers import (LastSubmissionDateTime,
20                               LastSubmissionDateTimeSerializer,
21                               SubmissionCount,
22                               SubmissionCountSerializer,
23                               )
24 from participants.models import Participant
25 from participants.serializers import (ParticipantCount,
26                                       ParticipantCountSerializer,
27                                       ParticipantTeamCount,
28                                       ParticipantTeamCountSerializer,
29                                       )
30 from .serializers import (ChallengePhaseSubmissionCount,
31                           ChallengePhaseSubmissionCountSerializer,
32                           LastSubmissionTimestamp,
33                           LastSubmissionTimestampSerializer,
34                           )
35 
36 
37 @throttle_classes([UserRateThrottle])
38 @api_view(['GET', ])
39 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))
40 @authentication_classes((ExpiringTokenAuthentication,))
41 def get_participant_team_count(request, challenge_pk):
42     """
43         Returns the number of participant teams in a challenge
44     """
45     challenge = get_challenge_model(challenge_pk)
46     participant_team_count = challenge.participant_teams.count()
47     participant_team_count = ParticipantTeamCount(participant_team_count)
48     serializer = ParticipantTeamCountSerializer(participant_team_count)
49     return Response(serializer.data, status=status.HTTP_200_OK)
50 
51 
52 @throttle_classes([UserRateThrottle])
53 @api_view(['GET', ])
54 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))
55 @authentication_classes((ExpiringTokenAuthentication,))
56 def get_participant_count(request, challenge_pk):
57     """
58         Returns the number of participants in a challenge
59     """
60     challenge = get_challenge_model(challenge_pk)
61     participant_teams = challenge.participant_teams.all()
62     participant_count = Participant.objects.filter(team__in=participant_teams).count()
63     participant_count = ParticipantCount(participant_count)
64     serializer = ParticipantCountSerializer(participant_count)
65     return Response(serializer.data, status=status.HTTP_200_OK)
66 
67 
68 @throttle_classes([UserRateThrottle])
69 @api_view(['GET', ])
70 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))
71 @authentication_classes((ExpiringTokenAuthentication,))
72 def get_submission_count(request, challenge_pk, duration):
73     """
74         Returns submission count for a challenge according to the duration
75         Valid values for duration are all, daily, weekly and monthly.
76     """
77     # make sure that a valid url is requested.
78     if duration.lower() not in ('all', 'daily', 'weekly', 'monthly'):
79         response_data = {'error': 'Wrong URL pattern!'}
80         return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)
81 
82     challenge = get_challenge_model(challenge_pk)
83 
84     challenge_phase_ids = challenge.challengephase_set.all().values_list('id', flat=True)
85 
86     q_params = {'challenge_phase__id__in': challenge_phase_ids}
87     since_date = None
88     if duration.lower() == 'daily':
89         since_date = timezone.now().date()
90 
91     elif duration.lower() == 'weekly':
92         since_date = (timezone.now() - timedelta(days=7)).date()
93 
94     elif duration.lower() == 'monthly':
95         since_date = (timezone.now() - timedelta(days=30)).date()
96     # for `all` we dont need any condition in `q_params`
97     if since_date:
98         q_params['submitted_at__gte'] = since_date
99 
100     submission_count = Submission.objects.filter(**q_params).count()
101     submission_count = SubmissionCount(submission_count)
102     serializer = SubmissionCountSerializer(submission_count)
103     return Response(serializer.data, status=status.HTTP_200_OK)
104 
105 
106 @throttle_classes([UserRateThrottle])
107 @api_view(['GET', ])
108 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))
109 @authentication_classes((ExpiringTokenAuthentication,))
110 def get_challenge_phase_submission_analysis(request, challenge_pk, challenge_phase_pk):
111     """
112     API to fetch
113     1. The submissions count for challenge phase.
114     2. The participated team count for challenge phase.
115     """
116     challenge = get_challenge_model(challenge_pk)
117 
118     challenge_phase = get_challenge_phase_model(challenge_phase_pk)
119 
120     submissions = Submission.objects.filter(
121         challenge_phase=challenge_phase, challenge_phase__challenge=challenge)
122     submission_count = submissions.count()
123     participant_team_count = submissions.values_list(
124         'participant_team', flat=True).distinct().count()
125 
126     challenge_phase_submission_count = ChallengePhaseSubmissionCount(
127         submission_count, participant_team_count, challenge_phase.pk)
128     try:
129         serializer = ChallengePhaseSubmissionCountSerializer(challenge_phase_submission_count)
130         response_data = serializer.data
131         return Response(response_data, status=status.HTTP_200_OK)
132     except:
133         response_data = {'error': "Bad request. Please try again later!"}
134         return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
135 
136 
137 @throttle_classes([UserRateThrottle])
138 @api_view(['GET', ])
139 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))
140 @authentication_classes((ExpiringTokenAuthentication,))
141 def get_last_submission_time(request, challenge_pk, challenge_phase_pk, submission_by):
142     """
143         Returns the last submission time for a particular challenge phase
144     """
145     challenge = get_challenge_model(challenge_pk)
146 
147     challenge_phase = get_challenge_phase_model(challenge_phase_pk)
148 
149     # To get the last submission time by a user in a challenge phase.
150     if submission_by == 'user':
151         last_submitted_at = Submission.objects.filter(created_by=request.user.pk,
152                                                       challenge_phase=challenge_phase,
153                                                       challenge_phase__challenge=challenge)
154         last_submitted_at = last_submitted_at.order_by('-submitted_at')[0].created_at
155         last_submitted_at = LastSubmissionDateTime(last_submitted_at)
156         serializer = LastSubmissionDateTimeSerializer(last_submitted_at)
157         return Response(serializer.data, status=status.HTTP_200_OK)
158 
159     else:
160         response_data = {'error': 'Page not found!'}
161         return Response(response_data, status=status.HTTP_404_NOT_FOUND)
162 
163 
164 @throttle_classes([UserRateThrottle])
165 @api_view(['GET', ])
166 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))
167 @authentication_classes((ExpiringTokenAuthentication,))
168 def get_last_submission_datetime_analysis(request, challenge_pk, challenge_phase_pk):
169     """
170     API to fetch
171     1. To get the last submission time in a challenge phase.
172     2. To get the last submission time in a challenge.
173     """
174 
175     challenge = get_challenge_model(challenge_pk)
176 
177     challenge_phase = get_challenge_phase_model(challenge_phase_pk)
178 
179     submissions = Submission.objects.filter(
180         challenge_phase__challenge=challenge)
181 
182     last_submission_timestamp_in_challenge = submissions.order_by(
183         '-submitted_at')[0].created_at
184 
185     last_submission_timestamp_in_challenge_phase = submissions.filter(
186         challenge_phase=challenge_phase).order_by('-submitted_at')[0].created_at
187 
188     last_submission_timestamp = LastSubmissionTimestamp(
189         last_submission_timestamp_in_challenge, last_submission_timestamp_in_challenge_phase, challenge_phase.pk)
190 
191     try:
192         serializer = LastSubmissionTimestampSerializer(last_submission_timestamp)
193         response_data = serializer.data
194         return Response(response_data, status=status.HTTP_200_OK)
195     except:
196         response_data = {'error': 'Bad request. Please try again later!'}
197         return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
198
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/apps/analytics/views.py b/apps/analytics/views.py
--- a/apps/analytics/views.py
+++ b/apps/analytics/views.py
@@ -179,11 +179,22 @@
     submissions = Submission.objects.filter(
         challenge_phase__challenge=challenge)
 
+    if not submissions:
+        response_data = {
+            'message': 'You dont have any submissions in this challenge!'
+        }
+        return Response(response_data, status.HTTP_200_OK)
+
     last_submission_timestamp_in_challenge = submissions.order_by(
         '-submitted_at')[0].created_at
 
-    last_submission_timestamp_in_challenge_phase = submissions.filter(
-        challenge_phase=challenge_phase).order_by('-submitted_at')[0].created_at
+    submissions_in_a_phase = submissions.filter(
+        challenge_phase=challenge_phase)
+
+    if not submissions_in_a_phase:
+        last_submission_timestamp_in_challenge_phase = 'You dont have any submissions in this challenge phase!'
+    else:
+        last_submission_timestamp_in_challenge_phase = submissions_in_a_phase.order_by('-submitted_at')[0].created_at
 
     last_submission_timestamp = LastSubmissionTimestamp(
         last_submission_timestamp_in_challenge, last_submission_timestamp_in_challenge_phase, challenge_phase.pk)

{"golden_diff": "diff --git a/apps/analytics/views.py b/apps/analytics/views.py\n--- a/apps/analytics/views.py\n+++ b/apps/analytics/views.py\n@@ -179,11 +179,22 @@\n     submissions = Submission.objects.filter(\n         challenge_phase__challenge=challenge)\n \n+    if not submissions:\n+        response_data = {\n+            'message': 'You dont have any submissions in this challenge!'\n+        }\n+        return Response(response_data, status.HTTP_200_OK)\n+\n     last_submission_timestamp_in_challenge = submissions.order_by(\n         '-submitted_at')[0].created_at\n \n-    last_submission_timestamp_in_challenge_phase = submissions.filter(\n-        challenge_phase=challenge_phase).order_by('-submitted_at')[0].created_at\n+    submissions_in_a_phase = submissions.filter(\n+        challenge_phase=challenge_phase)\n+\n+    if not submissions_in_a_phase:\n+        last_submission_timestamp_in_challenge_phase = 'You dont have any submissions in this challenge phase!'\n+    else:\n+        last_submission_timestamp_in_challenge_phase = submissions_in_a_phase.order_by('-submitted_at')[0].created_at\n \n     last_submission_timestamp = LastSubmissionTimestamp(\n         last_submission_timestamp_in_challenge, last_submission_timestamp_in_challenge_phase, challenge_phase.pk)\n", "issue": "Bug in last submission datetime analysis API\n**IndexError:** list index out of range\r\n\r\n**Culprit:**\r\n/api/analytics/challenge/{challenge_pk}/challenge_phase/{challenge_phase_pk}/last_submission_datetime_analysis/\r\n\n", "before_files": [{"content": "from datetime import timedelta\n\nfrom django.utils import timezone\n\nfrom rest_framework import permissions, status\nfrom rest_framework.decorators import (api_view,\n                                       authentication_classes,\n                                       permission_classes,\n                                       throttle_classes,)\nfrom rest_framework.response import Response\nfrom rest_framework_expiring_authtoken.authentication import (ExpiringTokenAuthentication,)\nfrom rest_framework.throttling import UserRateThrottle\n\nfrom accounts.permissions import HasVerifiedEmail\n\nfrom challenges.permissions import IsChallengeCreator\nfrom challenges.utils import get_challenge_model, get_challenge_phase_model\nfrom jobs.models import Submission\nfrom jobs.serializers import (LastSubmissionDateTime,\n                              LastSubmissionDateTimeSerializer,\n                              SubmissionCount,\n                              SubmissionCountSerializer,\n                              )\nfrom participants.models import Participant\nfrom participants.serializers import (ParticipantCount,\n                                      ParticipantCountSerializer,\n                                      ParticipantTeamCount,\n                                      ParticipantTeamCountSerializer,\n                                      )\nfrom .serializers import (ChallengePhaseSubmissionCount,\n                          ChallengePhaseSubmissionCountSerializer,\n                          LastSubmissionTimestamp,\n                          LastSubmissionTimestampSerializer,\n                          )\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_participant_team_count(request, challenge_pk):\n    \"\"\"\n        Returns the number of participant teams in a challenge\n    \"\"\"\n    challenge = get_challenge_model(challenge_pk)\n    participant_team_count = challenge.participant_teams.count()\n    participant_team_count = ParticipantTeamCount(participant_team_count)\n    serializer = ParticipantTeamCountSerializer(participant_team_count)\n    return Response(serializer.data, status=status.HTTP_200_OK)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_participant_count(request, challenge_pk):\n    \"\"\"\n        Returns the number of participants in a challenge\n    \"\"\"\n    challenge = get_challenge_model(challenge_pk)\n    participant_teams = challenge.participant_teams.all()\n    participant_count = Participant.objects.filter(team__in=participant_teams).count()\n    participant_count = ParticipantCount(participant_count)\n    serializer = ParticipantCountSerializer(participant_count)\n    return Response(serializer.data, status=status.HTTP_200_OK)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_submission_count(request, challenge_pk, duration):\n    \"\"\"\n        Returns submission count for a challenge according to the duration\n        Valid values for duration are all, daily, weekly and monthly.\n    \"\"\"\n    # make sure that a valid url is requested.\n    if duration.lower() not in ('all', 'daily', 'weekly', 'monthly'):\n        response_data = {'error': 'Wrong URL pattern!'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    challenge = get_challenge_model(challenge_pk)\n\n    challenge_phase_ids = challenge.challengephase_set.all().values_list('id', flat=True)\n\n    q_params = {'challenge_phase__id__in': challenge_phase_ids}\n    since_date = None\n    if duration.lower() == 'daily':\n        since_date = timezone.now().date()\n\n    elif duration.lower() == 'weekly':\n        since_date = (timezone.now() - timedelta(days=7)).date()\n\n    elif duration.lower() == 'monthly':\n        since_date = (timezone.now() - timedelta(days=30)).date()\n    # for `all` we dont need any condition in `q_params`\n    if since_date:\n        q_params['submitted_at__gte'] = since_date\n\n    submission_count = Submission.objects.filter(**q_params).count()\n    submission_count = SubmissionCount(submission_count)\n    serializer = SubmissionCountSerializer(submission_count)\n    return Response(serializer.data, status=status.HTTP_200_OK)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_challenge_phase_submission_analysis(request, challenge_pk, challenge_phase_pk):\n    \"\"\"\n    API to fetch\n    1. The submissions count for challenge phase.\n    2. The participated team count for challenge phase.\n    \"\"\"\n    challenge = get_challenge_model(challenge_pk)\n\n    challenge_phase = get_challenge_phase_model(challenge_phase_pk)\n\n    submissions = Submission.objects.filter(\n        challenge_phase=challenge_phase, challenge_phase__challenge=challenge)\n    submission_count = submissions.count()\n    participant_team_count = submissions.values_list(\n        'participant_team', flat=True).distinct().count()\n\n    challenge_phase_submission_count = ChallengePhaseSubmissionCount(\n        submission_count, participant_team_count, challenge_phase.pk)\n    try:\n        serializer = ChallengePhaseSubmissionCountSerializer(challenge_phase_submission_count)\n        response_data = serializer.data\n        return Response(response_data, status=status.HTTP_200_OK)\n    except:\n        response_data = {'error': \"Bad request. Please try again later!\"}\n        return Response(response_data, status=status.HTTP_400_BAD_REQUEST)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_last_submission_time(request, challenge_pk, challenge_phase_pk, submission_by):\n    \"\"\"\n        Returns the last submission time for a particular challenge phase\n    \"\"\"\n    challenge = get_challenge_model(challenge_pk)\n\n    challenge_phase = get_challenge_phase_model(challenge_phase_pk)\n\n    # To get the last submission time by a user in a challenge phase.\n    if submission_by == 'user':\n        last_submitted_at = Submission.objects.filter(created_by=request.user.pk,\n                                                      challenge_phase=challenge_phase,\n                                                      challenge_phase__challenge=challenge)\n        last_submitted_at = last_submitted_at.order_by('-submitted_at')[0].created_at\n        last_submitted_at = LastSubmissionDateTime(last_submitted_at)\n        serializer = LastSubmissionDateTimeSerializer(last_submitted_at)\n        return Response(serializer.data, status=status.HTTP_200_OK)\n\n    else:\n        response_data = {'error': 'Page not found!'}\n        return Response(response_data, status=status.HTTP_404_NOT_FOUND)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_last_submission_datetime_analysis(request, challenge_pk, challenge_phase_pk):\n    \"\"\"\n    API to fetch\n    1. To get the last submission time in a challenge phase.\n    2. To get the last submission time in a challenge.\n    \"\"\"\n\n    challenge = get_challenge_model(challenge_pk)\n\n    challenge_phase = get_challenge_phase_model(challenge_phase_pk)\n\n    submissions = Submission.objects.filter(\n        challenge_phase__challenge=challenge)\n\n    last_submission_timestamp_in_challenge = submissions.order_by(\n        '-submitted_at')[0].created_at\n\n    last_submission_timestamp_in_challenge_phase = submissions.filter(\n        challenge_phase=challenge_phase).order_by('-submitted_at')[0].created_at\n\n    last_submission_timestamp = LastSubmissionTimestamp(\n        last_submission_timestamp_in_challenge, last_submission_timestamp_in_challenge_phase, challenge_phase.pk)\n\n    try:\n        serializer = LastSubmissionTimestampSerializer(last_submission_timestamp)\n        response_data = serializer.data\n        return Response(response_data, status=status.HTTP_200_OK)\n    except:\n        response_data = {'error': 'Bad request. Please try again later!'}\n        return Response(response_data, status=status.HTTP_400_BAD_REQUEST)\n", "path": "apps/analytics/views.py"}], "after_files": [{"content": "from datetime import timedelta\n\nfrom django.utils import timezone\n\nfrom rest_framework import permissions, status\nfrom rest_framework.decorators import (api_view,\n                                       authentication_classes,\n                                       permission_classes,\n                                       throttle_classes,)\nfrom rest_framework.response import Response\nfrom rest_framework_expiring_authtoken.authentication import (ExpiringTokenAuthentication,)\nfrom rest_framework.throttling import UserRateThrottle\n\nfrom accounts.permissions import HasVerifiedEmail\n\nfrom challenges.permissions import IsChallengeCreator\nfrom challenges.utils import get_challenge_model, get_challenge_phase_model\nfrom jobs.models import Submission\nfrom jobs.serializers import (LastSubmissionDateTime,\n                              LastSubmissionDateTimeSerializer,\n                              SubmissionCount,\n                              SubmissionCountSerializer,\n                              )\nfrom participants.models import Participant\nfrom participants.serializers import (ParticipantCount,\n                                      ParticipantCountSerializer,\n                                      ParticipantTeamCount,\n                                      ParticipantTeamCountSerializer,\n                                      )\nfrom .serializers import (ChallengePhaseSubmissionCount,\n                          ChallengePhaseSubmissionCountSerializer,\n                          LastSubmissionTimestamp,\n                          LastSubmissionTimestampSerializer,\n                          )\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_participant_team_count(request, challenge_pk):\n    \"\"\"\n        Returns the number of participant teams in a challenge\n    \"\"\"\n    challenge = get_challenge_model(challenge_pk)\n    participant_team_count = challenge.participant_teams.count()\n    participant_team_count = ParticipantTeamCount(participant_team_count)\n    serializer = ParticipantTeamCountSerializer(participant_team_count)\n    return Response(serializer.data, status=status.HTTP_200_OK)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_participant_count(request, challenge_pk):\n    \"\"\"\n        Returns the number of participants in a challenge\n    \"\"\"\n    challenge = get_challenge_model(challenge_pk)\n    participant_teams = challenge.participant_teams.all()\n    participant_count = Participant.objects.filter(team__in=participant_teams).count()\n    participant_count = ParticipantCount(participant_count)\n    serializer = ParticipantCountSerializer(participant_count)\n    return Response(serializer.data, status=status.HTTP_200_OK)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_submission_count(request, challenge_pk, duration):\n    \"\"\"\n        Returns submission count for a challenge according to the duration\n        Valid values for duration are all, daily, weekly and monthly.\n    \"\"\"\n    # make sure that a valid url is requested.\n    if duration.lower() not in ('all', 'daily', 'weekly', 'monthly'):\n        response_data = {'error': 'Wrong URL pattern!'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    challenge = get_challenge_model(challenge_pk)\n\n    challenge_phase_ids = challenge.challengephase_set.all().values_list('id', flat=True)\n\n    q_params = {'challenge_phase__id__in': challenge_phase_ids}\n    since_date = None\n    if duration.lower() == 'daily':\n        since_date = timezone.now().date()\n\n    elif duration.lower() == 'weekly':\n        since_date = (timezone.now() - timedelta(days=7)).date()\n\n    elif duration.lower() == 'monthly':\n        since_date = (timezone.now() - timedelta(days=30)).date()\n    # for `all` we dont need any condition in `q_params`\n    if since_date:\n        q_params['submitted_at__gte'] = since_date\n\n    submission_count = Submission.objects.filter(**q_params).count()\n    submission_count = SubmissionCount(submission_count)\n    serializer = SubmissionCountSerializer(submission_count)\n    return Response(serializer.data, status=status.HTTP_200_OK)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_challenge_phase_submission_analysis(request, challenge_pk, challenge_phase_pk):\n    \"\"\"\n    API to fetch\n    1. The submissions count for challenge phase.\n    2. The participated team count for challenge phase.\n    \"\"\"\n    challenge = get_challenge_model(challenge_pk)\n\n    challenge_phase = get_challenge_phase_model(challenge_phase_pk)\n\n    submissions = Submission.objects.filter(\n        challenge_phase=challenge_phase, challenge_phase__challenge=challenge)\n    submission_count = submissions.count()\n    participant_team_count = submissions.values_list(\n        'participant_team', flat=True).distinct().count()\n\n    challenge_phase_submission_count = ChallengePhaseSubmissionCount(\n        submission_count, participant_team_count, challenge_phase.pk)\n    try:\n        serializer = ChallengePhaseSubmissionCountSerializer(challenge_phase_submission_count)\n        response_data = serializer.data\n        return Response(response_data, status=status.HTTP_200_OK)\n    except:\n        response_data = {'error': \"Bad request. Please try again later!\"}\n        return Response(response_data, status=status.HTTP_400_BAD_REQUEST)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_last_submission_time(request, challenge_pk, challenge_phase_pk, submission_by):\n    \"\"\"\n        Returns the last submission time for a particular challenge phase\n    \"\"\"\n    challenge = get_challenge_model(challenge_pk)\n\n    challenge_phase = get_challenge_phase_model(challenge_phase_pk)\n\n    # To get the last submission time by a user in a challenge phase.\n    if submission_by == 'user':\n        last_submitted_at = Submission.objects.filter(created_by=request.user.pk,\n                                                      challenge_phase=challenge_phase,\n                                                      challenge_phase__challenge=challenge)\n        last_submitted_at = last_submitted_at.order_by('-submitted_at')[0].created_at\n        last_submitted_at = LastSubmissionDateTime(last_submitted_at)\n        serializer = LastSubmissionDateTimeSerializer(last_submitted_at)\n        return Response(serializer.data, status=status.HTTP_200_OK)\n\n    else:\n        response_data = {'error': 'Page not found!'}\n        return Response(response_data, status=status.HTTP_404_NOT_FOUND)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail, IsChallengeCreator))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef get_last_submission_datetime_analysis(request, challenge_pk, challenge_phase_pk):\n    \"\"\"\n    API to fetch\n    1. To get the last submission time in a challenge phase.\n    2. To get the last submission time in a challenge.\n    \"\"\"\n\n    challenge = get_challenge_model(challenge_pk)\n\n    challenge_phase = get_challenge_phase_model(challenge_phase_pk)\n\n    submissions = Submission.objects.filter(\n        challenge_phase__challenge=challenge)\n\n    if not submissions:\n        response_data = {\n            'message': 'You dont have any submissions in this challenge!'\n        }\n        return Response(response_data, status.HTTP_200_OK)\n\n    last_submission_timestamp_in_challenge = submissions.order_by(\n        '-submitted_at')[0].created_at\n\n    submissions_in_a_phase = submissions.filter(\n        challenge_phase=challenge_phase)\n\n    if not submissions_in_a_phase:\n        last_submission_timestamp_in_challenge_phase = 'You dont have any submissions in this challenge phase!'\n    else:\n        last_submission_timestamp_in_challenge_phase = submissions_in_a_phase.order_by('-submitted_at')[0].created_at\n\n    last_submission_timestamp = LastSubmissionTimestamp(\n        last_submission_timestamp_in_challenge, last_submission_timestamp_in_challenge_phase, challenge_phase.pk)\n\n    try:\n        serializer = LastSubmissionTimestampSerializer(last_submission_timestamp)\n        response_data = serializer.data\n        return Response(response_data, status=status.HTTP_200_OK)\n    except:\n        response_data = {'error': 'Bad request. Please try again later!'}\n        return Response(response_data, status=status.HTTP_400_BAD_REQUEST)\n", "path": "apps/analytics/views.py"}]}