Skip to content

Commit

Permalink
Candidates can safely withdraw from election that has already entered…
Browse files Browse the repository at this point in the history
… its voting phase.
  • Loading branch information
helgihg committed Jun 18, 2018
1 parent b850b98 commit 6e3f895
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
20 changes: 8 additions & 12 deletions core/static/js/wasa2il.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,18 @@ function election_render(election) {
$(".voting").show();
}

// FIXME: The second term here makes much of the code below obsolete.
// This is deliberate; we would like to allow users to withdraw
// their candidacy at any time, but we need a few more things before
// that is safe and reasonable:
// 1. E-mail notifications to people who have voted for the candidate
// 2. A grace period so people can update their votes
// 3. Double-checking the ballot counting logic to ensure this does
// not break anything at that end, as it will create a gap in
// the user's ballot sequence.
if (election_ui_update_is_safe()) {
if (election_state == 'concluded' || election_state == 'voting') {
if (election_state == 'concluded') {
$("#election_button_withdraw").hide();
$("#election_button_announce").hide();
}
else if (election_object.user_is_candidate) {
$("#election_button_withdraw").show();
else if (election_state == 'voting') {
if (election_object.user_is_candidate) {
$("#election_button_withdraw").show();
}
else {
$("#election_button_withdraw").hide();
}
$("#election_button_announce").hide();
}
else {
Expand Down
21 changes: 21 additions & 0 deletions election/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ def process(self):
if self.is_processed:
raise Election.AlreadyProcessedException('Election %s has already been processed!' % self)

# "Flatten" the values of votes in an election. A candidate may be
# removed from an election when voting has already started. When that
# happens, ballots with that candidate may have a gap in their values,
# for example [0, 1, 2, 4] , because the person with value 3 was
# removed from the election. Here the ballot is "flattened" so that
# gaps are eliminated and the values are made sequential, i.e.
# [0, 1, 2, 3] and not [0, 1, 2, 4].
votes = self.electionvote_set.order_by('user_id', 'value')
last_user_id = 0
for vote in votes:
# Reset correct value every time we start processing a new user.
if last_user_id != vote.user_id:
correct_value = 0

if vote.value != correct_value:
vote.value = correct_value
vote.save()

correct_value += 1
last_user_id = vote.user_id

if self.candidate_set.count() == 0:
# If there are no candidates, there's no need to calculate
# anything. We're pretty confident in these being the results.
Expand Down

0 comments on commit 6e3f895

Please sign in to comment.