from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render, get_object_or_404 from django.urls import reverse from django.utils import timezone from .models import Event, EventRegistration, TimeSlot def index(request): events = Event.objects.filter(date__gte=timezone.now().date()).order_by("date")[:20] events_data = [ {"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}) def register(request, timeslot_id): if request.method != "POST": return HttpResponse(status_code=405) if not request.user.has_ugent_info: raise ValueError("User has missing UGent info missing") timeslot = get_object_or_404(TimeSlot, id=timeslot_id) timeslot.eventregistration_set.create( state=EventRegistration.INTERESTED, time_slot=timeslot, user=request.user, ) return HttpResponseRedirect(reverse("events:index") + f"#{timeslot.id}") def deregister(request, timeslot_id): if request.method != "POST": return HttpResponse(status_code=405) registration = get_object_or_404(EventRegistration, time_slot=timeslot_id, user=request.user) registration.delete() return HttpResponseRedirect(reverse("events:index") + f"#{timeslot_id}")