pretalx-musicrate/pretalx_musicrate/views.py

200 lines
5.9 KiB
Python
Raw Normal View History

2023-12-15 02:52:49 +01:00
from hmac import compare_digest
2023-12-15 17:12:12 +01:00
from operator import itemgetter
2023-12-15 02:52:49 +01:00
2023-12-13 23:58:00 +01:00
from django.contrib import messages
2023-12-15 17:12:12 +01:00
from django.db.models import F, Max, Min, Window
from django.db.models.functions import RowNumber
from django.http import JsonResponse
2023-12-15 02:52:49 +01:00
from django.shortcuts import redirect
2023-12-15 02:48:31 +01:00
from django.urls import reverse
2023-12-15 17:12:12 +01:00
from django.utils.functional import cached_property
2023-12-13 23:58:00 +01:00
from django.utils.translation import gettext_lazy as _
from django.views.generic import FormView, TemplateView, View
2023-12-15 17:12:12 +01:00
from django.views.generic.detail import SingleObjectMixin
from django_context_decorator import context
2023-12-15 02:48:31 +01:00
from pretalx.common.mixins.views import EventPermissionRequired
2023-12-13 23:58:00 +01:00
from .forms import MusicrateSettingsForm
2023-12-15 02:52:49 +01:00
class JoinView(TemplateView):
template_name = "pretalx_musicrate/join.html"
def validate_token(self, token):
if compare_digest(
token.encode("utf-8"),
self.request.event.pretalx_musicrate_settings.join_token.encode("utf-8"),
):
return True
messages.error(self.request, _("Invalid token"))
return False
def get_context_data(self, token_valid=False, **kwargs):
context = super().get_context_data(**kwargs)
context["token_valid"] = token_valid
return context
def get(self, request, *args, token, **kwargs):
token_valid = self.validate_token(token)
return super().get(request, *args, token_valid=token_valid, **kwargs)
def post(self, request, *args, token, **kwargs):
token_valid = self.validate_token(token)
if token_valid:
return redirect(request.path)
return self.render_to_response(
self.get_context_data(token_valid=token_valid, **kwargs)
)
2023-12-15 02:48:31 +01:00
class MusicrateSettingsView(EventPermissionRequired, FormView):
2023-12-13 23:58:00 +01:00
permission_required = "orga.change_settings"
template_name = "pretalx_musicrate/settings.html"
form_class = MusicrateSettingsForm
def get_success_url(self):
return self.request.path
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["event"] = self.request.event
return kwargs
def form_valid(self, form):
form.save()
2023-12-14 00:57:15 +01:00
messages.success(
self.request, _("The pretalx-musicrate settings were updated.")
2023-12-14 00:57:15 +01:00
)
2023-12-13 23:58:00 +01:00
return super().form_valid(form)
2023-12-15 02:48:31 +01:00
class QRCodeView(EventPermissionRequired, TemplateView):
permission_required = "orga.view_submissions"
template_name = "pretalx_musicrate/qrcode.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["contents"] = self.request.build_absolute_uri(
reverse(
"plugins:pretalx_musicrate:join",
kwargs={
"event": self.request.event.slug,
"token": self.request.event.pretalx_musicrate_settings.join_token,
},
)
)
return context
2023-12-15 17:12:12 +01:00
class SubmissionMixin(SingleObjectMixin):
slug_field = "code"
slug_url_kwarg = "code"
def get_queryset(self):
return self.request.event.submissions.prefetch_related("answers").filter(
submission_type__in=self.request.event.pretalx_musicrate_settings.submission_types.all()
)
@context
@cached_property
def submission(self):
return self.get_object()
@context
@cached_property
def genre(self):
return (
self.submission.answers.filter(
question=self.request.event.pretalx_musicrate_settings.genre_question
)
.values_list("answer", flat=True)
.first()
)
@context
@cached_property
def origin(self):
return (
self.submission.answers.filter(
question=self.request.event.pretalx_musicrate_settings.origin_question
)
.values_list("answer", flat=True)
.first()
)
def get_url_kwargs(self, **kwargs):
return self.request.resolver_match.kwargs | kwargs
@context
@cached_property
def prev(self):
prev = (
self.get_queryset()
.annotate(prev=Max("created"))
.filter(created__lt=self.submission.created, created=F("prev"))
.first()
)
if prev is None:
return None
return reverse(
self.request.resolver_match.view_name,
kwargs=self.get_url_kwargs(code=prev.code),
)
@context
@cached_property
def index(self):
return next(
map(
itemgetter(1),
filter(
lambda s: s[0] == self.submission.pk,
self.get_queryset()
.annotate(index=Window(expression=RowNumber(), order_by="created"))
.values_list("pk", "index"),
),
)
)
@context
@cached_property
def count(self):
return self.get_queryset().count()
@context
@cached_property
def next(self):
next = (
self.get_queryset()
.annotate(next=Min("created"))
.filter(created__gt=self.submission.created, created=F("next"))
.first()
)
if next is None:
return None
return reverse(
self.request.resolver_match.view_name,
kwargs=self.get_url_kwargs(code=next.code),
)
class PresenterView(EventPermissionRequired, SubmissionMixin, TemplateView):
permission_required = "orga.view_submissions"
template_name = "pretalx_musicrate/present.html"
@context
@property
def can_continue(self):
return True
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return super().get(request, *args, **kwargs)
class MayAdvanceView(EventPermissionRequired, View):
permission_required = "orga.view_submissions"
def get(self, request, *args, **kwargs):
return JsonResponse(True, safe=False)