diff --git a/pretalx_musicrate/views.py b/pretalx_musicrate/views.py index 64b0020..e76234f 100644 --- a/pretalx_musicrate/views.py +++ b/pretalx_musicrate/views.py @@ -31,6 +31,25 @@ youtube_re = re.compile( ) +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 + + return ( + submission + or submissions.filter( + submission_type__in=settings.submission_types.all(), + state=SubmissionStates.SUBMITTED, + ) + .order_by("created") + .first() + ) + + class JoinView(TemplateView): template_name = "pretalx_musicrate/join.html" @@ -57,15 +76,10 @@ class JoinView(TemplateView): def get(self, request, *args, token, **kwargs): token_valid = self.validate_token(token) if self.juror is not None: - submission = ( - self.juror.last_submission - or self.request.event.pretalx_musicrate_settings.last_submission - or self.request.event.submissions.filter( - submission_type__in=self.request.event.pretalx_musicrate_settings.submission_types.all(), - state=SubmissionStates.SUBMITTED, - ) - .order_by("created") - .first() + 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( @@ -84,15 +98,10 @@ class JoinView(TemplateView): event=self.request.event, last_submission=self.request.event.pretalx_musicrate_settings.last_submission, ) - submission = ( - self.juror.last_submission - or self.request.event.pretalx_musicrate_settings.last_submission - or self.request.event.submissions.filter( - submission_type__in=self.request.event.pretalx_musicrate_settings.submission_types.all(), - state=SubmissionStates.SUBMITTED, - ) - .order_by("created") - .first() + 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( @@ -142,14 +151,9 @@ class QRCodeView(EventPermissionRequired, TemplateView): }, ) ) - context["last_submission"] = ( - self.request.event.pretalx_musicrate_settings.last_submission - or self.request.event.submissions.filter( - submission_type__in=self.request.event.pretalx_musicrate_settings.submission_types.all(), - state=SubmissionStates.SUBMITTED, - ) - .order_by("created") - .first() + context["last_submission"] = get_last_submission( + self.request.event.pretalx_musicrate_settings, + self.request.event.submissions, ) return context @@ -288,17 +292,22 @@ class RatingView(FormView, SubmissionMixin): 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 ( - (settings := self.request.event.pretalx_musicrate_settings).last_submission - is not None - and self.object.created > settings.last_submission.created + 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=settings.last_submission.code, + code=last_submission.code, ) + return super().get(request, *args, **kwargs) @@ -357,16 +366,16 @@ class PresenterView(EventPermissionRequired, SubmissionMixin, TemplateView): def get(self, request, *args, **kwargs): self.object = self.get_object() - if ( - (settings := self.request.event.pretalx_musicrate_settings).last_submission - is None - or self.object.created > settings.last_submission.created - ): + + 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