From cb7e4559e4103453dc0a9b50074e8e38e54f3968 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Fri, 11 Mar 2022 22:34:32 -0600 Subject: [PATCH] Update problem vote --- judge/models/problem.py | 5 +++-- judge/views/problem.py | 15 ++++++--------- templates/problem/voting-form.html | 2 +- templates/problem/voting-stats.html | 28 ++++++++++++++-------------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/judge/models/problem.py b/judge/models/problem.py index 28dd102..f9e10ec 100644 --- a/judge/models/problem.py +++ b/judge/models/problem.py @@ -375,12 +375,13 @@ class Problem(models.Model): save.alters_data = True - def can_vote(self, user): + def can_vote(self, request): + user = request.user if not user.is_authenticated: return False # If the user is in contest, nothing should be shown. - if user.profile.current_contest: + if request.in_contest_mode: return False # If the user is not allowed to vote diff --git a/judge/views/problem.py b/judge/views/problem.py index 78d538f..65ab3bf 100644 --- a/judge/views/problem.py +++ b/judge/views/problem.py @@ -217,7 +217,7 @@ class ProblemDetail(ProblemMixin, SolvedProblemMixin, DetailView): context['meta_description'] = self.object.summary or metadata[0] context['og_image'] = self.object.og_image or metadata[1] - context['can_vote'] = self.object.can_vote(user) + context['can_vote'] = self.object.can_vote(self.request) if context['can_vote']: try: context['vote'] = ProblemPointsVote.objects.get(voter=user.profile, problem=self.object) @@ -226,14 +226,11 @@ class ProblemDetail(ProblemMixin, SolvedProblemMixin, DetailView): else: context['vote'] = None - all_votes = list(self.object.problem_points_votes.order_by('points').values_list('points', flat=True)) - - context['has_votes'] = len(all_votes) > 0 - - # If the user is not currently in contest. - if not user.is_authenticated or user.profile.current_contest is None: + if user.is_superuser: + all_votes = list(self.object.problem_points_votes.order_by('points').values_list('points', flat=True)) context['all_votes'] = all_votes - + + context['has_votes'] = len(all_votes) > 0 context['max_possible_vote'] = 600 context['min_possible_vote'] = 100 return context @@ -259,7 +256,7 @@ class Vote(ProblemMixin, SingleObjectMixin, View): def post(self, request, *args, **kwargs): self.object = self.get_object() - if not self.object.can_vote(request.user): # Not allowed to vote for some reason. + if not self.object.can_vote(request): # Not allowed to vote for some reason. return HttpResponseForbidden('Not allowed to vote on this problem.', content_type='text/plain') form = ProblemPointsVoteForm(request.POST) diff --git a/templates/problem/voting-form.html b/templates/problem/voting-form.html index 735b781..0dc2fd3 100644 --- a/templates/problem/voting-form.html +++ b/templates/problem/voting-form.html @@ -73,7 +73,7 @@ // Forms are auto disabled to prevent resubmission, but we need to allow resubmission here. $('#id_vote_form_submit_button').removeAttr('disabled'); var current = $.featherlight.current(); - current.close(); + if (current) current.close(); }, error: function (data) { let errors = data.responseJSON; diff --git a/templates/problem/voting-stats.html b/templates/problem/voting-stats.html index 57acd41..c37896e 100644 --- a/templates/problem/voting-stats.html +++ b/templates/problem/voting-stats.html @@ -43,12 +43,12 @@