Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: begeleider features for api #60

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading