Implement vote tallying!

This commit is contained in:
RunasSudo 2017-09-24 13:32:46 +10:00
parent 20eb60e737
commit c7d7c611b6
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 23 additions and 5 deletions

View File

@ -25,6 +25,9 @@ class EncryptedAnswer(EmbeddedObject):
class NullEncryptedAnswer(EncryptedAnswer): class NullEncryptedAnswer(EncryptedAnswer):
answer = EmbeddedObjectField() answer = EmbeddedObjectField()
def decrypt(self):
return self.answer
class Ballot(EmbeddedObject): class Ballot(EmbeddedObject):
_id = UUIDField() _id = UUIDField()
@ -36,15 +39,23 @@ class Voter(EmbeddedObject):
class Question(EmbeddedObject): class Question(EmbeddedObject):
prompt = StringField() prompt = StringField()
def compute_result(self):
...
class Result(EmbeddedObject): class Result(EmbeddedObject):
pass pass
class ApprovalQuestion(Question): class ApprovalQuestion(Question):
choices = ListField(StringField()) choices = ListField(StringField())
def compute_result(self):
result = ApprovalResult(choices=([0] * len(self.choices)))
for voter in self.recurse_parents(Election).voters:
for ballot in voter.ballots:
# TODO: Separate decryption phase
encrypted_answer = ballot.encrypted_answers[self._instance[1]] # _instance[1] is the question number
answer = encrypted_answer.decrypt()
for choice in answer.choices:
result.choices[choice] += 1
return result
class ApprovalAnswer(Answer): class ApprovalAnswer(Answer):
choices = ListField(IntField()) choices = ListField(IntField())

View File

@ -48,7 +48,7 @@ class ElectionTestCase(TestCase):
voter = Voter() voter = Voter()
election.voters.append(voter) election.voters.append(voter)
# Check _instance # Check _instance
self.assertEqual(voter._instance, (election.voters, None)) self.assertEqual(voter._instance, (election.voters, i))
question = ApprovalQuestion(prompt='President', choices=['John Smith', 'Joe Bloggs', 'John Q. Public']) question = ApprovalQuestion(prompt='President', choices=['John Smith', 'Joe Bloggs', 'John Q. Public'])
election.questions.append(question) election.questions.append(question)
@ -83,5 +83,12 @@ class ElectionTestCase(TestCase):
election.save() election.save()
# Compute result # Compute result
election.results = [None, None]
for i in range(2): for i in range(2):
result = election.questions[i].compute_result() result = election.questions[i].compute_result()
election.results[i] = result
election.save()
self.assertEqual(election.results[0].choices, [2, 1, 1])
self.assertEqual(election.results[1].choices, [2, 1])

View File

@ -131,7 +131,7 @@ class EosObject(metaclass=EosObjectType):
class EosList(EosObject, list): class EosList(EosObject, list):
def append(self, value): def append(self, value):
if isinstance(value, EosObject): if isinstance(value, EosObject):
value._instance = (self, None) value._instance = (self, len(self))
if not value._inited: if not value._inited:
value.post_init() value.post_init()
return super().append(value) return super().append(value)