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
|
save.alters_data = True
|
||||||
|
|
||||||
def can_vote(self, user):
|
def can_vote(self, request):
|
||||||
|
user = request.user
|
||||||
if not user.is_authenticated:
|
if not user.is_authenticated:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# If the user is in contest, nothing should be shown.
|
# If the user is in contest, nothing should be shown.
|
||||||
if user.profile.current_contest:
|
if request.in_contest_mode:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# If the user is not allowed to vote
|
# 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['meta_description'] = self.object.summary or metadata[0]
|
||||||
context['og_image'] = self.object.og_image or metadata[1]
|
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']:
|
if context['can_vote']:
|
||||||
try:
|
try:
|
||||||
context['vote'] = ProblemPointsVote.objects.get(voter=user.profile, problem=self.object)
|
context['vote'] = ProblemPointsVote.objects.get(voter=user.profile, problem=self.object)
|
||||||
|
@ -226,14 +226,11 @@ class ProblemDetail(ProblemMixin, SolvedProblemMixin, DetailView):
|
||||||
else:
|
else:
|
||||||
context['vote'] = None
|
context['vote'] = None
|
||||||
|
|
||||||
all_votes = list(self.object.problem_points_votes.order_by('points').values_list('points', flat=True))
|
if user.is_superuser:
|
||||||
|
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:
|
|
||||||
context['all_votes'] = all_votes
|
context['all_votes'] = all_votes
|
||||||
|
|
||||||
|
context['has_votes'] = len(all_votes) > 0
|
||||||
context['max_possible_vote'] = 600
|
context['max_possible_vote'] = 600
|
||||||
context['min_possible_vote'] = 100
|
context['min_possible_vote'] = 100
|
||||||
return context
|
return context
|
||||||
|
@ -259,7 +256,7 @@ class Vote(ProblemMixin, SingleObjectMixin, View):
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
self.object = self.get_object()
|
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')
|
return HttpResponseForbidden('Not allowed to vote on this problem.', content_type='text/plain')
|
||||||
|
|
||||||
form = ProblemPointsVoteForm(request.POST)
|
form = ProblemPointsVoteForm(request.POST)
|
||||||
|
|
|
@ -73,7 +73,7 @@
|
||||||
// Forms are auto disabled to prevent resubmission, but we need to allow resubmission here.
|
// Forms are auto disabled to prevent resubmission, but we need to allow resubmission here.
|
||||||
$('#id_vote_form_submit_button').removeAttr('disabled');
|
$('#id_vote_form_submit_button').removeAttr('disabled');
|
||||||
var current = $.featherlight.current();
|
var current = $.featherlight.current();
|
||||||
current.close();
|
if (current) current.close();
|
||||||
},
|
},
|
||||||
error: function (data) {
|
error: function (data) {
|
||||||
let errors = data.responseJSON;
|
let errors = data.responseJSON;
|
||||||
|
|
|
@ -43,12 +43,12 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
let voteChart = null;
|
let voteChart = null;
|
||||||
let data = {{ all_votes }};
|
let allVotes = {{ all_votes }};
|
||||||
|
|
||||||
function reload_vote_graph() {
|
function reload_vote_graph() {
|
||||||
if (voteChart !== null) voteChart.destroy();
|
if (voteChart !== null) voteChart.destroy();
|
||||||
|
|
||||||
if (data.length === 0) {
|
if (allVotes.length === 0) {
|
||||||
$('.canvas').hide();
|
$('.canvas').hide();
|
||||||
$('.has_votes_footer').hide();
|
$('.has_votes_footer').hide();
|
||||||
$('.no_votes_error').show();
|
$('.no_votes_error').show();
|
||||||
|
@ -57,7 +57,7 @@
|
||||||
$('.has_votes_footer').show();
|
$('.has_votes_footer').show();
|
||||||
$('.no_votes_error').hide();
|
$('.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.
|
// Give the graph some padding on both sides.
|
||||||
let min_points = {{ min_possible_vote }};
|
let min_points = {{ min_possible_vote }};
|
||||||
let max_points = {{ max_possible_vote }};
|
let max_points = {{ max_possible_vote }};
|
||||||
|
@ -73,18 +73,18 @@
|
||||||
let total_votes = 0;
|
let total_votes = 0;
|
||||||
let mean = 0;
|
let mean = 0;
|
||||||
|
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < allVotes.length; i++) {
|
||||||
// Assume the data is valid.
|
// Assume the allVotes is valid.
|
||||||
voteFreq[(data[i] - min_points) / 100]++;
|
voteFreq[(allVotes[i] - min_points) / 100]++;
|
||||||
max_number_of_votes = Math.max(max_number_of_votes, voteFreq[(data[i] - min_points) / 100]);
|
max_number_of_votes = Math.max(max_number_of_votes, voteFreq[(allVotes[i] - min_points) / 100]);
|
||||||
mean += data[i];
|
mean += allVotes[i];
|
||||||
total_votes++;
|
total_votes++;
|
||||||
}
|
}
|
||||||
mean = mean / total_votes;
|
mean = mean / total_votes;
|
||||||
let half = Math.floor(total_votes / 2);
|
let half = Math.floor(total_votes / 2);
|
||||||
let median = data[half];
|
let median = allVotes[half];
|
||||||
if (total_votes % 2 === 0) {
|
if (total_votes % 2 === 0) {
|
||||||
median = (median + data[half - 1]) / 2;
|
median = (median + allVotes[half - 1]) / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
$('.mean_vote').prop('innerText', mean.toFixed(2));
|
$('.mean_vote').prop('innerText', mean.toFixed(2));
|
||||||
|
@ -128,15 +128,15 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateUserVote(prev_voted_points, voted_points) {
|
function updateUserVote(prev_voted_points, voted_points) {
|
||||||
let index = data.indexOf(prev_voted_points);
|
let index = allVotes.indexOf(prev_voted_points);
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
data.splice(index, 1);
|
allVotes.splice(index, 1);
|
||||||
}
|
}
|
||||||
data.push(voted_points);
|
allVotes.push(voted_points);
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteUserVote(prev_voted_points) {
|
function deleteUserVote(prev_voted_points) {
|
||||||
data.splice(data.indexOf(prev_voted_points), 1);
|
allVotes.splice(allVotes.indexOf(prev_voted_points), 1);
|
||||||
}
|
}
|
||||||
$(function() {
|
$(function() {
|
||||||
$('#id_vote_stats_button').featherlight('#id_vote_stats', {
|
$('#id_vote_stats_button').featherlight('#id_vote_stats', {
|
||||||
|
|
Loading…
Reference in a new issue