Skip to content

Commit

Permalink
feat: begeleider features (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverben authored May 25, 2024
1 parent 06144ce commit ccf1eb6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
46 changes: 44 additions & 2 deletions aanmelden/src/api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from datetime import date

from django.conf import settings
from django.contrib.auth.models import User
from django.db.models import Value
from django.db.models.functions import Concat
from django.http import JsonResponse, HttpResponseBadRequest, HttpResponse
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View

from .mixins import ClientCredentialsRequiredMixin, SlotContextMixin, AuthenticatedMixin
from .models import Presence, MacAddress, Slot
from .utils import register, deregister, NotEnoughSlotsException, TooManyDaysException, StripcardLimitReachedException, AlreadySeenException
from .utils import register, deregister, NotEnoughSlotsException, TooManyDaysException, StripcardLimitReachedException, \
AlreadySeenException, mark_seen


class FreeV2(View):
Expand Down Expand Up @@ -90,7 +94,36 @@ def get(self, request, *args, **kwargs):

class Slots(AuthenticatedMixin, View):
def get(self, request):
return JsonResponse({'slots': Slot.get_enabled_slots(request.user)})
slots = Slot.get_enabled_slots(request.user)
if not self.request.user.is_superuser:
return JsonResponse({"slots": slots})

for slot in slots:
slot['presence'] = list(
Presence.objects.filter(date=slot['date'], pod=slot['pod'], user__is_superuser=False)
.values('id', 'seen')
.annotate(name=Concat('user__first_name', Value(' '), 'user__last_name'))
)

members = list(User.objects
.filter(is_active=True)
.values('id')
.annotate(name=Concat('first_name', Value(' '), 'last_name'))
)

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


class MarkSeen(AuthenticatedMixin, View):
def get(self, *args, **kwargs):
if not self.request.user.is_superuser:
return HttpResponse(status=403)

pk = int(kwargs.get('pk'))
seen = kwargs.get('seen')

mark_seen(pk, seen)
return JsonResponse({'ok': True})


class Register(AuthenticatedMixin, SlotContextMixin, View):
Expand All @@ -107,6 +140,15 @@ def get(self, request, *args, **kwargs):
return JsonResponse({"error": None})


class RegisterManual(AuthenticatedMixin, SlotContextMixin, View):
def get(self, request, *args, **kwargs):
if not self.request.user.is_superuser:
return HttpResponse(status=403)

register(self.slot, User.objects.get(id=int(kwargs.get('pk'))))
return JsonResponse({'ok': True})


class DeRegister(AuthenticatedMixin, SlotContextMixin, View):
def get(self, request, *args, **kwargs):
try:
Expand Down
17 changes: 15 additions & 2 deletions aanmelden/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class StripcardLimitReachedException(RegisterException):
pass


def register(slot, user):
if not user.is_superuser:
def register(slot, user, limit=True):
if not user.is_superuser and not limit:
if Presence.slots_available(slot.date, slot.pod) <= 0:
raise NotEnoughSlotsException()

Expand Down Expand Up @@ -64,6 +64,19 @@ def register(slot, user):
return presence


def mark_seen(pk, seen):
try:
presence = Presence.objects.get(pk=pk)
presence.seen = seen == 'true'
presence.seen_by = 'manual'
presence.save()
except Presence.DoesNotExist:
# Presence not found, who cares
pass

asyncio.run(sio.emit("update_report_page"))


class DeRegisterException(Exception):
pass

Expand Down
16 changes: 3 additions & 13 deletions aanmelden/src/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from aanmelden.src.mixins import BegeleiderRequiredMixin, SlotContextMixin
from aanmelden.src.models import Presence, DjoUser, UserInfo, Slot
from aanmelden.src.utils import (register, deregister, NotEnoughSlotsException, TooManyDaysException,
StripcardLimitReachedException, AlreadySeenException)
StripcardLimitReachedException, AlreadySeenException, mark_seen)


@method_decorator(never_cache, name='dispatch')
Expand Down Expand Up @@ -158,23 +158,13 @@ def get_queryset(self):
return Presence.objects.filter(date__in=dates).order_by('-user__is_superuser')


class MarkAsSeen(LoginRequiredMixin, View):
class MarkAsSeen(BegeleiderRequiredMixin, View):

def get(self, request, *args, **kwargs):
pk = int(kwargs.get('pk'))
seen = kwargs.get('seen')
try:
presence = Presence.objects.get(pk=pk)
presence.seen = seen == 'true'
presence.seen_by = 'manual'
presence.save()
except Presence.DoesNotExist:
# Presence not found, who cares
pass

# Tell all clients to update the report page
asyncio.run(sio.emit("update_report_page"))

mark_seen(pk, seen)
return JsonResponse({"ok": True})


Expand Down
2 changes: 2 additions & 0 deletions aanmelden/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
path('api/v1/slots', api.Slots.as_view()),
path('api/v1/register/<str:day>/<str:pod>', api.Register.as_view()),
path('api/v1/deregister/<str:day>/<str:pod>', api.DeRegister.as_view()),
path('api/v1/register_manual/<str:day>/<str:pod>/<str:pk>', api.RegisterManual.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 ccf1eb6

Please sign in to comment.