diff --git a/helios/crypto/electionalgs.py b/helios/crypto/electionalgs.py index 4c2f2239ffcf4c296934dfbefe2760752eb96400..662a81a4a48cfc7fe0b8452bb7584e485af6907c 100644 --- a/helios/crypto/electionalgs.py +++ b/helios/crypto/electionalgs.py @@ -363,6 +363,29 @@ class EncryptedVote(HeliosObject): encrypted_answers = [EncryptedAnswer.fromElectionAndAnswer(election, answer_num, answers[answer_num]) for answer_num in range(len(answers))] return cls(encrypted_answers=encrypted_answers, election_hash=election.hash, election_uuid = election.uuid) + +def one_question_winner(question, result, num_cast_votes): + """ + determining the winner for one question + """ + # sort the answers , keep track of the index + counts = sorted(enumerate(result), key=lambda(x): x[1]) + counts.reverse() + + # if there's a max > 1, we assume that the top MAX win + if question['max'] > 1: + return [c[0] for c in counts[:max]] + + # if max = 1, then depends on absolute or relative + if question['result_type'] == 'absolute': + if counts[0][1] >= (num_cast_votes/2 + 1): + return [counts[0][0]] + else: + return [] + + if question['result_type'] == 'relative': + return [counts[0][0]] + class Election(HeliosObject): FIELDS = ['uuid', 'questions', 'name', 'short_name', 'description', 'voters_hash', 'openreg', @@ -399,10 +422,22 @@ class Election(HeliosObject): else: return "Closed" + @property + def winners(self): + """ + Depending on the type of each question, determine the winners + returns an array of winners for each question, aka an array of arrays. + assumes that if there is a max to the question, that's how many winners there are. + """ + return [one_question_winner(self.questions[i], self.result[i], self.num_cast_votes) for i in range(len(self.questions))] + @property def pretty_result(self): if not self.result: return None + + # get the winners + winners = self.winners raw_result = self.result prettified_result = [] @@ -416,7 +451,7 @@ class Election(HeliosObject): for j in range(len(q['answers'])): a = q['answers'][j] count = raw_result[i][j] - pretty_question.append({'answer': a, 'count': count}) + pretty_question.append({'answer': a, 'count': count, 'winner': (j in winners[i])}) prettified_result.append({'question': q['short_name'], 'answers': pretty_question}) diff --git a/helios/templates/election_view.html b/helios/templates/election_view.html index c4e56f554f26c076dcee1250cd949e3a4f51d759..aa0563e01a28121db61a5734e921053d3d155057 100644 --- a/helios/templates/election_view.html +++ b/helios/templates/election_view.html @@ -141,7 +141,7 @@ all voters will be notified that the tally is ready. <b><span style="font-size:0.8em;">Question #{{forloop.counter}}</span><br />{{question.question}}</b><br /> <table class="pretty" style="width: auto;"> {% for answer in question.answers %} -<tr><td style="padding-right:80px;">{{answer.answer}}</td><td align="right">{{answer.count}}</td></tr> +<tr><td style="padding-right:80px;{% if answer.winner %}font-weight:bold;{% endif %}">{{answer.answer}}</td><td align="right" style="{% if answer.winner %}font-weight:bold;{% endif %}">{{answer.count}}</td></tr> {% endfor %} </table> {% endfor %}