Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Deep-Chill committed Dec 5, 2023
1 parent e6aebc5 commit 27253f4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 39 deletions.
47 changes: 12 additions & 35 deletions pages/api/sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,9 @@ def remove_element(self, request, slug=None, element_rank=None):

class LiveEventAttendanceAPIView(APIView):
'''
Manages attendance tracking for live events
Allows marking a user's attendance to a Live Event.
This API endpoint allows for marking attendance at live events,
represented by the LiveEvent model. It accepts URL parameters to identify
the specific live event and the user attending, and updates the EnumeratedProgress
instance associated with the event and user to reflect their attendance.
The relevant user's EnumeratedProgress viewing duration for the event is updated to
The user's EnumeratedProgress viewing duration for the event is updated to
meet the minimum viewing duration requirement of the associated EnumeratedElement.
**Tags**: attendance, live events
Expand All @@ -307,54 +302,36 @@ def post(self, request, *args, **kwargs):
.. code-block:: http
POST /api/sequences/{sequence_slug}/{rank}/events/{username}/mark-attendance HTTP/1.1
URL Parameters:
- sequence_slug (str): The slug of the sequence associated with the live event.
- username (str): The username of the user attending the live event.
- rank (int): The rank of the enumerated element in the sequence.
Responds with a status indicating whether the attendance was successfully marked or not.
POST /api/sequences/{sequence}/{rank}/{username}/mark-attendance HTTP/1.1
On successful attendance marking:
Responds
.. code-block:: json
{
"detail": "Attendance marked successfully"
}
On failure to mark attendance (e.g., invalid parameters, event not found):
.. code-block:: json
{
"detail": "Attendance not marked"
}
"""

input_serializer = AttendanceInputSerializer(data=self.kwargs)
if not input_serializer.is_valid():
return api_response.Response(
input_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
input_serializer.is_valid(raise_exception=True)

sequence = input_serializer.validated_data['sequence']
user = input_serializer.validated_data['username']
rank = input_serializer.validated_data['rank']

sequence_progress = get_object_or_404(
SequenceProgress, user=user, sequence=sequence)
enumerated_progress = get_object_or_404(
EnumeratedProgress, progress=sequence_progress, rank=rank)
sequence_progress, _ = SequenceProgress.objects.get_or_create(
user=user, sequence=sequence)
enumerated_progress, _ = EnumeratedProgress.objects.get_or_create(
progress=sequence_progress, rank=rank)
enumerated_element = get_object_or_404(
EnumeratedElements, rank=rank, sequence=sequence)
page_element = enumerated_element.page_element
live_event = get_object_or_404(
LiveEvent, sequence=sequence, element=page_element)
live_event = LiveEvent.objects.filter(element=page_element).first()

if page_element.events.first() == live_event and \
enumerated_progress.viewing_duration < enumerated_element.min_viewing_duration:
# We use if live_event to confirm the existence of the LiveEvent object
if live_event and enumerated_progress.viewing_duration <= enumerated_element.min_viewing_duration:
enumerated_progress.viewing_duration = enumerated_element.min_viewing_duration
enumerated_progress.save()
return api_response.Response(
Expand Down
4 changes: 1 addition & 3 deletions pages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,6 @@ class LiveEvent(models.Model):
scheduled_at = models.DateTimeField()
location = models.URLField(_("URL to the calendar event"), max_length=2083)
max_attendees = models.IntegerField(default=0)
sequence = models.ForeignKey('Sequence', on_delete=models.CASCADE,
related_name='sequence_events')
extra = get_extra_field_class()(null=True, blank=True,
help_text=_("Extra meta data (can be stringify JSON)"))

Expand Down Expand Up @@ -457,7 +455,7 @@ class SequenceProgress(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
extra = get_extra_field_class()(null=True, blank=True,
help_text=_("Extra meta data (can be stringify JSON)"))
completion_date = models.DateTimeField(
completion_date = models.DateTimeField(blank=True, null=True,
help_text=_("Time when the user completed the Sequence"))

def __str__(self):
Expand Down
4 changes: 4 additions & 0 deletions pages/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ class Meta:
fields = ('created_at', 'viewing_duration', 'last_ping_time')

class AttendanceInputSerializer(serializers.Serializer):
"""
Serializer to validate input to mark users' attendance
to a LiveEvent(LiveEventAttendanceAPIView)
"""
sequence = serializers.SlugField()
username = serializers.CharField()
rank = serializers.IntegerField()
Expand Down
2 changes: 1 addition & 1 deletion pages/urls/api/sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
SequenceAPIView,
basename='api_sequences')
urlpatterns = [
path('sequences/<slug:sequence>/<int:rank>/events/<username>/mark-attendance',
path('sequences/<slug:sequence>/<int:rank>/<username>/mark-attendance',
LiveEventAttendanceAPIView.as_view(),
name='api_mark_attendance')
] + router.urls

0 comments on commit 27253f4

Please sign in to comment.