Skip to content

Commit

Permalink
feat: add future registration api (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverben authored Sep 10, 2024
1 parent eed913f commit bbd8150
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
68 changes: 64 additions & 4 deletions aanmelden/src/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from datetime import date
from datetime import datetime
from json import loads

from django.conf import settings
from django.db.models import Value, F
from django.db.models.functions import Concat
from django.http import JsonResponse, HttpResponseBadRequest, HttpResponse
from django.utils.dateparse import parse_date
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
Expand All @@ -13,10 +16,13 @@
SlotContextMixin,
AuthenticatedMixin,
)
from aanmelden.src.models import Presence, MacAddress, Slot, DjoUser
from aanmelden.src.models import DjoUser
from aanmelden.src.models import Presence, MacAddress, Slot, DAY_NUMBERS
from aanmelden.src.utils import (
register,
register_future,
deregister,
deregister_future,
NotEnoughSlotsException,
TooManyDaysException,
StripcardLimitReachedException,
Expand Down Expand Up @@ -139,7 +145,15 @@ def get(self, request):
)
)

return JsonResponse({"slots": slots, "members": members})
registered_dates = list(
request.user.presence_set.filter(date__gte=datetime.today())
.order_by("date")
.values_list("date", flat=True)
)

return JsonResponse(
{"slots": slots, "members": members, "registered_dates": registered_dates}
)


class MarkSeen(AuthenticatedMixin, View):
Expand All @@ -156,8 +170,13 @@ def get(self, *args, **kwargs):

class Register(AuthenticatedMixin, SlotContextMixin, View):
def get(self, request, *args, **kwargs):
future_date = kwargs.get("date")
try:
register(self.slot, request.user, request.user.is_superuser)
if future_date:
register_future(parse_date(future_date), self.slot, request.user)
else:
register(self.slot, request.user, request.user.is_superuser)

except NotEnoughSlotsException:
return JsonResponse({"error": "Not enough slots available"}, status=400)
except TooManyDaysException:
Expand All @@ -184,9 +203,50 @@ def get(self, request, *args, **kwargs):

class DeRegister(AuthenticatedMixin, SlotContextMixin, View):
def get(self, request, *args, **kwargs):
future_date = kwargs.get("date")
try:
deregister(self.slot, request.user)
if request.user.is_superuser and future_date:
deregister_future(parse_date(future_date), self.slot, request.user)
else:
deregister(self.slot, request.user)

except AlreadySeenException:
return JsonResponse({"error": "Je bent al aanwezig"})

return JsonResponse({"error": None})


@method_decorator(csrf_exempt, name="dispatch")
class FutureUpdate(AuthenticatedMixin, View):
def patch(self, request, *args, **kwargs):
if not self.request.user.is_superuser:
return HttpResponse(status=403)

body = loads(request.body.decode("utf8"))
if "add" in body:
for add in body["add"]:
add_date = parse_date(add)
register_future(
add_date,
Slot.objects.filter(
name=list(DAY_NUMBERS.keys())[
list(DAY_NUMBERS.values()).index(add_date.weekday())
]
).first(),
request.user,
)

if "remove" in body:
for remove in body["remove"]:
remove_date = parse_date(remove)
deregister_future(
remove_date,
Slot.objects.filter(
name=list(DAY_NUMBERS.keys())[
list(DAY_NUMBERS.values()).index(remove_date.weekday())
]
).first(),
request.user,
)

return JsonResponse({"error": None})
3 changes: 3 additions & 0 deletions aanmelden/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@
),
path("api/v1/slots", api.Slots.as_view()),
path("api/v1/register/<str:day>/<str:pod>", api.Register.as_view()),
path("api/v1/register/<str:day>/<str:pod>/<str:date>", api.Register.as_view()),
path("api/v1/deregister/<str:day>/<str:pod>", api.DeRegister.as_view()),
path("api/v1/deregister/<str:day>/<str:pod>/<str:date>", api.DeRegister.as_view()),
path(
"api/v1/register_manual/<str:day>/<str:pod>/<str:pk>",
api.RegisterManual.as_view(),
),
path("api/v1/future", api.FutureUpdate.as_view()),
path("api/v1/seen/<int:pk>/<str:seen>", api.MarkSeen.as_view()),
re_path(r"oauth/.*", views.LoginResponseView.as_view()),
path("", views.LoginView.as_view(), name="login"),
Expand Down

0 comments on commit bbd8150

Please sign in to comment.