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: calendar: prevent altering past, add tooltips #94

Merged
merged 2 commits into from
Nov 29, 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
11 changes: 8 additions & 3 deletions aanmelden/src/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import datetime
from datetime import timedelta
from functools import lru_cache

import requests
from django.conf import settings
from django.db import IntegrityError
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