diff --git a/AKModel/urls.py b/AKModel/urls.py index 64527851d7e613eb19a3fa155403f33d66b4be4e..2071acc9edc56b506eb307f42654e9da1ba82c63 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 0000000000000000000000000000000000000000..d11de2d06c6ce641c9b4877cb23aad5175cb80d9 --- /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)