Update problem vote
This commit is contained in:
parent
77d7244ad2
commit
cb7e4559e4
4 changed files with 24 additions and 26 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -43,12 +43,12 @@
|
|||
|
||||
<script>
|
||||
let voteChart = null;
|
||||
let data = {{ all_votes }};
|
||||
let allVotes = {{ all_votes }};
|
||||
|
||||
function reload_vote_graph() {
|
||||
if (voteChart !== null) voteChart.destroy();
|
||||
|
||||
if (data.length === 0) {
|
||||
if (allVotes.length === 0) {
|
||||
$('.canvas').hide();
|
||||
$('.has_votes_footer').hide();
|
||||
$('.no_votes_error').show();
|
||||
|
@ -57,7 +57,7 @@
|
|||
$('.has_votes_footer').show();
|
||||
$('.no_votes_error').hide();
|
||||
|
||||
data.sort(function(a, b){return a - b});
|
||||
allVotes.sort(function(a, b){return a - b});
|
||||
// Give the graph some padding on both sides.
|
||||
let min_points = {{ min_possible_vote }};
|
||||
let max_points = {{ max_possible_vote }};
|
||||
|
@ -73,18 +73,18 @@
|
|||
let total_votes = 0;
|
||||
let mean = 0;
|
||||
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
// Assume the data is valid.
|
||||
voteFreq[(data[i] - min_points) / 100]++;
|
||||
max_number_of_votes = Math.max(max_number_of_votes, voteFreq[(data[i] - min_points) / 100]);
|
||||
mean += data[i];
|
||||
for (let i = 0; i < allVotes.length; i++) {
|
||||
// Assume the allVotes is valid.
|
||||
voteFreq[(allVotes[i] - min_points) / 100]++;
|
||||
max_number_of_votes = Math.max(max_number_of_votes, voteFreq[(allVotes[i] - min_points) / 100]);
|
||||
mean += allVotes[i];
|
||||
total_votes++;
|
||||
}
|
||||
mean = mean / total_votes;
|
||||
let half = Math.floor(total_votes / 2);
|
||||
let median = data[half];
|
||||
let median = allVotes[half];
|
||||
if (total_votes % 2 === 0) {
|
||||
median = (median + data[half - 1]) / 2;
|
||||
median = (median + allVotes[half - 1]) / 2;
|
||||
}
|
||||
|
||||
$('.mean_vote').prop('innerText', mean.toFixed(2));
|
||||
|
@ -128,15 +128,15 @@
|
|||
}
|
||||
|
||||
function updateUserVote(prev_voted_points, voted_points) {
|
||||
let index = data.indexOf(prev_voted_points);
|
||||
let index = allVotes.indexOf(prev_voted_points);
|
||||
if (index > -1) {
|
||||
data.splice(index, 1);
|
||||
allVotes.splice(index, 1);
|
||||
}
|
||||
data.push(voted_points);
|
||||
allVotes.push(voted_points);
|
||||
}
|
||||
|
||||
function deleteUserVote(prev_voted_points) {
|
||||
data.splice(data.indexOf(prev_voted_points), 1);
|
||||
allVotes.splice(allVotes.indexOf(prev_voted_points), 1);
|
||||
}
|
||||
$(function() {
|
||||
$('#id_vote_stats_button').featherlight('#id_vote_stats', {
|
||||
|
|
Loading…
Reference in a new issue