Skip to content

Commit

Permalink
feat: calendar: prevent altering past, add tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
wipeautcrafter committed Nov 29, 2024
1 parent 810ab39 commit 59e713f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
15 changes: 10 additions & 5 deletions aanmelden/src/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion aanmelden/src/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TooManyDaysException,
StripcardLimitReachedException,
AlreadySeenException,
JochDetectedException,
mark_seen,
)

Expand Down Expand Up @@ -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"))


Expand Down
31 changes: 22 additions & 9 deletions aanmelden/static/js/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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) {
Expand Down

0 comments on commit 59e713f

Please sign in to comment.