kers/events/views.py

45 lines
1.5 KiB
Python
Raw Normal View History

2020-07-25 14:30:53 +02:00
from django.http import HttpResponse, HttpResponseRedirect
2020-07-22 04:08:30 +02:00
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
2020-08-04 03:32:02 +02:00
from django.utils import timezone
2020-07-21 22:24:45 +02:00
2020-08-04 03:32:02 +02:00
from .models import Event, EventRegistration, TimeSlot
2020-07-22 04:02:13 +02:00
2020-07-21 22:24:45 +02:00
def index(request):
2020-07-22 04:08:30 +02:00
events = Event.objects.filter(date__gte=timezone.now().date()).order_by("date")[:20]
events_data = [
2020-08-04 03:32:02 +02:00
{"event": event, "timeslots": [{"timeslot": timeslot,
"my_registration": timeslot.registration_of(request.user)}
for timeslot in TimeSlot.objects.filter(event_id=event.id)]}
for event in events
]
return render(request, "events/index.html", {"events": events_data})
2020-07-22 04:02:13 +02:00
2020-08-04 03:32:02 +02:00
def register(request, timeslot_id):
2020-07-25 14:30:53 +02:00
if request.method != "POST":
return HttpResponse(status_code=405)
if not request.user.has_ugent_info:
raise ValueError("User has missing UGent info missing")
2020-08-04 03:32:02 +02:00
timeslot = get_object_or_404(TimeSlot, id=timeslot_id)
2020-07-25 14:30:53 +02:00
2020-08-04 03:32:02 +02:00
timeslot.eventregistration_set.create(
2020-07-25 14:30:53 +02:00
state=EventRegistration.INTERESTED,
2020-08-04 03:32:02 +02:00
time_slot=timeslot,
2020-07-25 14:30:53 +02:00
user=request.user,
)
2020-08-04 03:32:02 +02:00
return HttpResponseRedirect(reverse("events:index") + f"#{timeslot.id}")
2020-07-25 14:30:53 +02:00
2020-08-04 03:32:02 +02:00
def deregister(request, timeslot_id):
2020-07-25 14:30:53 +02:00
if request.method != "POST":
return HttpResponse(status_code=405)
2020-08-04 03:32:02 +02:00
registration = get_object_or_404(EventRegistration, time_slot=timeslot_id, user=request.user)
2020-07-25 14:30:53 +02:00
registration.delete()
2020-08-04 03:32:02 +02:00
return HttpResponseRedirect(reverse("events:index") + f"#{timeslot_id}")