From 59e713f3ffa1bf341186c1e2561fb61acba850b2 Mon Sep 17 00:00:00 2001 From: Wiebe Beeftink Date: Fri, 29 Nov 2024 20:45:45 +0100 Subject: [PATCH 1/2] feat: calendar: prevent altering past, add tooltips --- aanmelden/src/utils.py | 15 ++++++++++----- aanmelden/src/views.py | 6 +++++- aanmelden/static/js/calendar.js | 31 ++++++++++++++++++++++--------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/aanmelden/src/utils.py b/aanmelden/src/utils.py index 6106ae1..635a0e9 100644 --- a/aanmelden/src/utils.py +++ b/aanmelden/src/utils.py @@ -1,10 +1,10 @@ import asyncio -from datetime import timedelta -from functools import lru_cache - import requests +import datetime +from datetime import timedelta from django.conf import settings from django.db import IntegrityError +from functools import lru_cache from jwt import PyJWKClient from aanmelden.sockets import sio @@ -80,16 +80,21 @@ def register_future(date, slot, user): presence.pod = slot.pod presence.user = user + # if it's in the past, don't register + if date < datetime.date.today(): + raise JochDetectedException + try: presence.save() except IntegrityError: # Already registered -> ignore pass - # only update if register date is in current week + # get start and end of registered week date_start = slot.date - timedelta(days=slot.date.weekday()) date_end = date_start + timedelta(days=6) + # only update if register date is in current week if date_start <= date <= date_end: asyncio.run(sio.emit("update_report_page")) asyncio.run(sio.emit("update_main_page")) @@ -160,7 +165,7 @@ def get_jwks_client(): return PyJWKClient(uri=get_openid_configuration()["jwks_uri"]) -def get_access_token(request) -> (str, None): +def get_access_token(request) -> tuple[str, None]: token = request.GET.get("access_token", "").strip() if token == "": parts = request.headers.get("authorization", "").split() diff --git a/aanmelden/src/views.py b/aanmelden/src/views.py index 7b6d46f..b4edf71 100644 --- a/aanmelden/src/views.py +++ b/aanmelden/src/views.py @@ -27,6 +27,7 @@ TooManyDaysException, StripcardLimitReachedException, AlreadySeenException, + JochDetectedException, mark_seen, ) @@ -140,7 +141,10 @@ class RegisterFuture( ): def get(self, request, *args, **kwargs): date = parse_date(kwargs.get("date")) - register_future(date, self.slot, request.user) + try: + register_future(date, self.slot, request.user) + except JochDetectedException: + pass # waarschijnlijk een joch die probeert te klooien return HttpResponseRedirect(reverse("calendar")) diff --git a/aanmelden/static/js/calendar.js b/aanmelden/static/js/calendar.js index 6ecfd4a..c070ea6 100644 --- a/aanmelden/static/js/calendar.js +++ b/aanmelden/static/js/calendar.js @@ -51,11 +51,19 @@ const loadCalendar = () => { const dateEntry = entries.find(entry => compareDates(date, entry.date)) const dateSlot = slots.find(slot => slot.date.getDay() === date.getDay()) + + const isToday = compareDates(date, today) + const canRegister = dateSlot && (date >= today || isToday) const td = document.createElement('td') td.className = 'text-center position-relative' td.textContent = dateNum + // if date is today + if (isToday) { + td.classList.add('text-decoration-underline') + } + // if registration is found if (dateEntry) { const icon = document.createElement('iconify-icon') @@ -66,30 +74,35 @@ const loadCalendar = () => { td.append(icon) td.classList.add('text-bg-info') - } else if(dateSlot) { + } else if(canRegister) { td.classList.add('bg-secondary-subtle') } // make clickable if can register - if(dateSlot) { + if(canRegister) { const dateFormatted = formatDate(date) td.role = 'button' td.ariaLabel = `registreren voor ${dateFormatted}` - + + td.dataset.bsTitle = date.toLocaleDateString('nl-NL', { + weekday: 'short', + day: 'numeric', + month: 'short', + year: 'numeric', + }) + td.dataset.bsToggle = 'tooltip' + td.dataset.bsPlacement = 'right' + new bootstrap.Tooltip(td) + const route = dateEntry ? 'deregister' : 'register' const dateParam = encodeURIComponent(dateFormatted) const dayParam = encodeURIComponent(dateSlot.name) const podParam = encodeURIComponent(dateSlot.pod) const url = `/${route}/future/${dateParam}/${dayParam}/${podParam}` - + td.addEventListener('click', () => location.href = url) } - - // if date is today - if (compareDates(date, today)) { - td.classList.add('text-decoration-underline') - } // gray out if it isn't the current month if (date.getMonth() === month) { From a1e83a0b9cf200bdffcb52d6a9beb0e14e2c7a6a Mon Sep 17 00:00:00 2001 From: Wiebe Beeftink Date: Fri, 29 Nov 2024 20:49:25 +0100 Subject: [PATCH 2/2] chore: fix pylint gezeik --- aanmelden/src/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aanmelden/src/utils.py b/aanmelden/src/utils.py index 635a0e9..7c1e5f9 100644 --- a/aanmelden/src/utils.py +++ b/aanmelden/src/utils.py @@ -1,10 +1,10 @@ import asyncio -import requests import datetime from datetime import timedelta +from functools import lru_cache +import requests from django.conf import settings from django.db import IntegrityError -from functools import lru_cache from jwt import PyJWKClient from aanmelden.sockets import sio