From 8c55f4dbc9f9ad323da4217c97e602fca2b077c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?= <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de> Date: Fri, 29 Oct 2021 12:38:28 +0200 Subject: [PATCH] Introduce API Post Endpoint for indication of interest in AK This endpoint checks for validity of the request and increments the AK interest counter if currently allowed --- AKModel/urls.py | 12 +++++++----- AKSubmission/api.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 AKSubmission/api.py diff --git a/AKModel/urls.py b/AKModel/urls.py index 64527851..2071acc9 100644 --- a/AKModel/urls.py +++ b/AKModel/urls.py @@ -26,11 +26,13 @@ if apps.is_installed("AKScheduling"): api_router.register('scheduling-constraint-violations', ConstraintViolationsViewSet, basename='scheduling-constraint-violations') - extra_paths = [ - path('api/scheduling-events/', EventsView.as_view(), name='scheduling-events'), - path('api/scheduling-room-availabilities/', RoomAvailabilitiesView.as_view(), - name='scheduling-room-availabilities'), - ] + extra_paths.append(path('api/scheduling-events/', EventsView.as_view(), name='scheduling-events')) + extra_paths.append(path('api/scheduling-room-availabilities/', RoomAvailabilitiesView.as_view(), + name='scheduling-room-availabilities')) +if apps.is_installed("AKSubmission"): + from AKSubmission.api import increment_interest_counter + + extra_paths.append(path('api/ak/<pk>/indicate-interest/', increment_interest_counter, name='submission-ak-indicate-interest')) event_specific_paths = [ path('api/', include(api_router.urls), name='api'), diff --git a/AKSubmission/api.py b/AKSubmission/api.py new file mode 100644 index 00000000..d11de2d0 --- /dev/null +++ b/AKSubmission/api.py @@ -0,0 +1,38 @@ +from rest_framework import status +from rest_framework.decorators import api_view +from rest_framework.response import Response +from django.utils.datetime_safe import datetime + +from AKModel.models import AK + + +def ak_interest_indication_active(event, current_timestamp): + """ + Check whether indication of interest is currently allowed for a given event + + :param event: event to check for + :type event: Event + :param current_timestamp: current timestamp + :type current_timestamp: datetime + :return: True if indication is allowed, False if not + :rtype: Bool + """ + return event.active and (event.interest_start is None or (event.interest_start <= current_timestamp and ( + event.interest_end is None or current_timestamp <= event.interest_end))) + + +@api_view(['POST']) +def increment_interest_counter(request, event_slug, pk, **kwargs): + """ + Increment interest counter for AK + """ + ak = AK.objects.get(pk=pk) + if ak: + # Check whether interest indication is currently allowed + current_timestamp = datetime.now().astimezone(ak.event.timezone) + if ak_interest_indication_active(ak.event, current_timestamp): + ak.interest_counter += 1 + ak.save() + return Response(status=status.HTTP_200_OK) + return Response(status=status.HTTP_403_FORBIDDEN) + return Response(status=status.HTTP_404_NOT_FOUND) -- GitLab