95 lines
2.4 KiB
Python
95 lines
2.4 KiB
Python
# views.py
|
|
import logging
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
from django.http import HttpRequest
|
|
from django.urls import reverse
|
|
from django.contrib import messages
|
|
|
|
from events.utils.cookieutils import get_rsvp_from_cookie, set_rsvp_cookie
|
|
|
|
from ..models import (
|
|
Event,
|
|
Activity,
|
|
RSVP,
|
|
)
|
|
|
|
# Logger
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def invite_redirect(request: HttpRequest, slug: str):
|
|
"""
|
|
Redirect from the unique invite page
|
|
to the rsvp response page which is linked to the response
|
|
"""
|
|
|
|
rsvp = get_object_or_404(RSVP, invite=slug)
|
|
response = redirect(reverse("events:event", args=[rsvp.event.slug]))
|
|
|
|
# Opened for the first time
|
|
# TODO what if email not sent out?
|
|
if rsvp.status == RSVP.Status.UNOPENED:
|
|
rsvp.status = RSVP.Status.NOT_STARTED
|
|
rsvp.save()
|
|
|
|
# Only set the cookie the first time, after this they need to use the invite link
|
|
set_rsvp_cookie(response, rsvp.event, rsvp)
|
|
else:
|
|
messages.warning(
|
|
request,
|
|
"You can only use the invite once, please use the link in the email.",
|
|
)
|
|
|
|
logger.info("Invite `%s` opened for Event `%s`", slug, rsvp.event.title)
|
|
|
|
return response
|
|
|
|
|
|
def event_page(request: HttpRequest, slug):
|
|
event = get_object_or_404(Event, slug=slug)
|
|
|
|
activities = (
|
|
Activity.objects.filter(event=event)
|
|
.select_related("group")
|
|
.order_by("start_time")
|
|
)
|
|
|
|
groups = {}
|
|
ungrouped = []
|
|
|
|
for activity in activities:
|
|
|
|
if activity.group:
|
|
groups.setdefault(activity.group, []).append(activity)
|
|
else:
|
|
ungrouped.append(activity)
|
|
|
|
context = {
|
|
"event": event,
|
|
"groups": groups,
|
|
"ungrouped_activities": ungrouped,
|
|
}
|
|
|
|
response = False
|
|
|
|
rsvp = get_rsvp_from_cookie(request, slug)
|
|
if isinstance(rsvp, RSVP):
|
|
|
|
joining = rsvp.status == RSVP.Status.YES_JOINING
|
|
not_coming = rsvp.status == RSVP.Status.NOT_COMING
|
|
in_progress = rsvp.status == RSVP.Status.INCOMPLETE
|
|
not_started = rsvp.status in (RSVP.Status.NOT_STARTED, RSVP.Status.UNOPENED)
|
|
|
|
response = {
|
|
"rsvp": rsvp,
|
|
"joining": joining,
|
|
"not_coming": not_coming,
|
|
"in_progress": in_progress,
|
|
"not_started": not_started,
|
|
}
|
|
|
|
context["response"] = response
|
|
|
|
return render(request, "event/event.html", context)
|