import csv import re from hmac import compare_digest from urllib.parse import parse_qs, urlparse from django.contrib import messages from django.db.models import Case, F, FilteredRelation, Q, Value, When from django.http import HttpResponse, JsonResponse from django.shortcuts import get_object_or_404, redirect from django.urls import reverse from django.utils.functional import cached_property from django.utils.html import Urlizer from django.utils.translation import gettext_lazy as _, ngettext from django.views.generic import FormView, TemplateView, View from django.views.generic.detail import SingleObjectMixin from django_context_decorator import context from pretalx.common.views.mixins import EventPermissionRequired from pretalx.orga.views.submission import BaseSubmissionList, SubmissionList from pretalx.submission.models import Submission, SubmissionStates from .forms import ( AssigneeForm, EnhancedSubmissionFilterForm, MusicrateSettingsForm, RatingForm, ) from .models import Juror, Rating youtube_re = re.compile( r"(?:https?://)?(youtu\.be/|(?:(?:m|www)\.)?youtube\.com/watch\?)", re.IGNORECASE ) def get_last_submission(settings, submissions, submission=None): if submission is not None and submission.state != SubmissionStates.SUBMITTED: submission = None submission = submission or settings.last_submission if submission is not None and submission.state != SubmissionStates.SUBMITTED: submission = None submissions = submissions.filter( submission_type__in=settings.submission_types.all(), state=SubmissionStates.SUBMITTED, ) if submission is not None and not submissions.filter(pk=submission.pk).exists(): submission = None return submission or submissions.order_by("created").first() class JoinView(TemplateView): template_name = "pretalx_musicrate/join.html" def validate_token(self, token): try: self.juror = Juror.objects.get(token=token) return True except Juror.DoesNotExist: self.juror = None 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["juror"] = self.juror context["token_valid"] = token_valid return context def get(self, request, *args, token, **kwargs): token_valid = self.validate_token(token) if self.juror is not None: submission = get_last_submission( self.request.event.pretalx_musicrate_settings, self.request.event.submissions, self.juror.last_submission, ) if submission is not None: return redirect( "plugins:pretalx_musicrate:rating", event=self.request.event.slug, token=self.juror.token, code=submission.code, ) 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: if self.juror is None: self.juror = Juror.objects.create( event=self.request.event, last_submission=self.request.event.pretalx_musicrate_settings.last_submission, ) submission = get_last_submission( self.request.event.pretalx_musicrate_settings, self.request.event.submissions, self.juror.last_submission, ) if submission is not None: return redirect( "plugins:pretalx_musicrate:rating", event=self.request.event.slug, token=self.juror.token, code=submission.code, ) return self.render_to_response( self.get_context_data(token_valid=token_valid, **kwargs) ) class MusicrateSettingsView(EventPermissionRequired, FormView): 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() messages.success( self.request, _("The pretalx-musicrate settings were updated.") ) return super().form_valid(form) 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, }, ) ) context["last_submission"] = get_last_submission( self.request.event.pretalx_musicrate_settings, self.request.event.submissions, ) return context 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(), state=SubmissionStates.SUBMITTED, ) @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 @cached_property def index_queryset(self): return ( self.get_queryset() .prefetch_related(None) .only("pk", "code") .order_by("created") ) @context @cached_property def prev(self): if self.index == 1: return None return reverse( self.request.resolver_match.view_name, kwargs=self.get_url_kwargs( code=self.index_queryset[self.index - 1 - 1].code ), ) @context @cached_property def index(self): return next( map( lambda s: s[0] + 1, filter( lambda s: s[1].pk == self.submission.pk, enumerate(self.index_queryset), ), ) ) @context @cached_property def count(self): return self.index_queryset.count() @context @cached_property def next(self): if self.index == self.count: return None return reverse( self.request.resolver_match.view_name, kwargs=self.get_url_kwargs( code=self.index_queryset[self.index - 1 + 1].code ), ) class RatingView(FormView, SubmissionMixin): template_name = "pretalx_musicrate/rating.html" form_class = RatingForm @cached_property def juror(self): return get_object_or_404( Juror, token=self.request.resolver_match.kwargs["token"] ) @context @cached_property def can_continue(self): return self.submission.ratings.filter(juror=self.juror).exists() def get_form(self, form_class=None): if form_class is None: form_class = self.get_form_class() try: instance = Rating.objects.get(submission=self.submission, juror=self.juror) except Rating.DoesNotExist: instance = None return form_class(instance=instance, **self.get_form_kwargs()) def get_success_url(self): return self.request.path def form_valid(self, form): obj = form.save(commit=False) obj.submission = self.submission obj.juror = self.juror obj.save() try: self.juror.last_submission = self.submission self.juror.save() except Exception: pass messages.success(self.request, _("Saved!")) return super().form_valid(form) def get(self, request, *args, **kwargs): self.object = self.get_object() last_submission = get_last_submission( self.request.event.pretalx_musicrate_settings, self.request.event.submissions, ) if ( last_submission is not None and self.object.created > last_submission.created ): return redirect( self.request.resolver_match.view_name, event=self.request.event.slug, token=self.juror.token, code=last_submission.code, ) return super().get(request, *args, **kwargs) class PresenterView(EventPermissionRequired, SubmissionMixin, TemplateView): permission_required = "orga.view_submissions" template_name = "pretalx_musicrate/present.html" @context @cached_property def links(self): class Extractor: def __init__(self): self.urls = [] def format(self, href, **kwargs): self.urls.append(href) return href extractor = Extractor() urlizer = Urlizer() urlizer.url_template = extractor urlizer(self.submission.abstract) urlizer(self.submission.description) urlizer(self.submission.notes) urlizer(self.submission.internal_notes) for answer in self.submission.answers.filter( question__in=self.request.event.pretalx_musicrate_settings.link_questions.all() ).values_list("answer", flat=True): urlizer(answer) links = [] for url in extractor.urls: if (m := youtube_re.search(url)) is not None: links.append( ( "youtube", ( url.removeprefix("http") .removeprefix("s") .removeprefix("://") .removeprefix("youtu.be/")[:11] if m[1] == "youtu.be/" else "".join( parse_qs(urlparse(url).query).get("v", "dQw4w9WgXcQ") ) ), ) ) else: links.append(("other", url)) return links @context @property def can_continue(self): return True def get(self, request, *args, **kwargs): self.object = self.get_object() settings = self.request.event.pretalx_musicrate_settings last_submission = get_last_submission(settings, self.request.event.submissions) if last_submission is None or self.object.created > last_submission.created: try: settings.last_submission = self.object settings.save() except Exception: pass response = super().get(request, *args, **kwargs) response._csp_update = {"frame-src": "https://www.youtube-nocookie.com"} return response class MayAdvanceView(EventPermissionRequired, SubmissionMixin, View): permission_required = "orga.view_submissions" def get(self, request, *args, **kwargs): num_ratings = self.submission.ratings.count() num_jurors = self.request.event.jurors.count() return JsonResponse( { "mayAdvance": num_ratings >= round( num_jurors * self.request.event.pretalx_musicrate_settings.advance_threshold ) and num_jurors > 2, "statusText": ngettext( "%(num_ratings)d of %(num_jurors)d has rated this submission", "%(num_ratings)d of %(num_jurors)d have rated this submission", num_ratings, ) % {"num_jurors": num_jurors, "num_ratings": num_ratings}, } ) class ExportView(EventPermissionRequired, View): permission_required = "orga.view_submissions" def get(self, request, *args, **kwargs): response = HttpResponse( content_type="text/csv", headers={ "Content-Disposition": f'attachment; filename="{request.event.slug}.csv"' }, ) writer = csv.writer(response) genre_question = request.event.pretalx_musicrate_settings.genre_question origin_question = request.event.pretalx_musicrate_settings.origin_question jurors = request.event.jurors.order_by("token") for submission in ( request.event.submissions.prefetch_related("answers") .select_related("submission_type") .filter( submission_type__in=request.event.pretalx_musicrate_settings.submission_types.all() ) .only("title", "submission_type__name") .order_by("created") ): submission_info = [submission.title, submission.submission_type.name] if genre_question is not None: submission_info.append( submission.answers.filter(question=genre_question) .values_list("answer", flat=True) .first() or "" ) if origin_question is not None: submission_info.append( submission.answers.filter(question=origin_question) .values_list("answer", flat=True) .first() or "" ) writer.writerow( [ *submission_info, *jurors.annotate( filtered_ratings=FilteredRelation( "ratings", condition=Q(ratings__submission=submission) ), rating=Case( When(filtered_ratings__rating=None, then=Value("")), When(filtered_ratings__rating="", then=Value("E")), default="filtered_ratings__rating", ), ).values_list("rating", flat=True), ] ) return response class AssigneeView(EventPermissionRequired, FormView, SingleObjectMixin): form_class = AssigneeForm model = Submission permission_required = "orga.change_submissions" slug_field = "code" slug_url_kwarg = "code" template_name = "pretalx_musicrate/assignee.html" def get_form_kwargs(self): kwargs = super().get_form_kwargs() kwargs["submission"] = self.object return kwargs def get_success_url(self): return self.request.path def form_valid(self, form): form.save() messages.success(self.request, _("Saved!")) return super().form_valid(form) def get(self, *args, **kwargs): self.object = self.get_object() return super().get(*args, **kwargs) def post(self, *args, **kwargs): self.object = self.get_object() return super().post(*args, **kwargs) class EnhancedSubmissionList(SubmissionList): sortable_fields = ("code", "score__value", "title", "state", "assignee") template_name = "pretalx_musicrate/enhanced_list.html" def get_filter_form(self): return EnhancedSubmissionFilterForm( data=self.request.GET, event=self.request.event, usable_states=self.usable_states, limit_tracks=self.limit_tracks, search_fields=self.get_default_filters(), ) def _get_base_queryset(self, for_review=False): qs = ( super(BaseSubmissionList, self) .get_queryset(for_review=for_review) .prefetch_related("assignee") .order_by("-id") ) if not self.filter_form.is_valid(): return qs return self.filter_form.filter_queryset(qs) class ScoreExportView(EventPermissionRequired, View): permission_required = "orga.view_submissions" def get(self, request, *args, **kwargs): response = HttpResponse( content_type="text/csv", headers={ "Content-Disposition": f'attachment; filename="{request.event.slug}.csv"' }, ) csv.writer(response).writerows( request.event.submissions.select_related("score", "submission_type") .filter( submission_type__in=request.event.pretalx_musicrate_settings.submission_types.all() ) .exclude(state__in=(SubmissionStates.CANCELED, SubmissionStates.WITHDRAWN)) .order_by(F("score__value").desc(nulls_last=True)) .values_list("score__value", "title") ) return response