kers/events/views.py
2020-07-25 14:31:03 +02:00

52 lines
1.5 KiB
Python

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from django.urls import reverse
import datetime
from .models import Event, EventRegistration, CustomUser
from events.tasks import assign_reservations
def index(request):
events = Event.objects.filter(date__gte=timezone.now().date()).order_by("date")[:20]
events_data = [
{ "event": event, "my_registration": event.registration_of(request.user) }
for event in events
]
return render(request, "events/index.html", {"events": events_data})
def view_score_stuff(request):
return HttpResponse(
assign_reservations()
)
def register(request, event_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")
event = get_object_or_404(Event, id=event_id)
event.eventregistration_set.create(
state=EventRegistration.INTERESTED,
event=event,
user=request.user,
)
return HttpResponseRedirect(reverse("events:index") + f"#{event.id}")
def deregister(request, event_id):
if request.method != "POST":
return HttpResponse(status_code=405)
registration = get_object_or_404(EventRegistration, event=event_id, user=request.user)
registration.delete()
return HttpResponseRedirect(reverse("events:index") + f"#{event_id}")