Implement vote tallying!
This commit is contained in:
parent
20eb60e737
commit
c7d7c611b6
@ -25,6 +25,9 @@ class EncryptedAnswer(EmbeddedObject):
|
||||
|
||||
class NullEncryptedAnswer(EncryptedAnswer):
|
||||
answer = EmbeddedObjectField()
|
||||
|
||||
def decrypt(self):
|
||||
return self.answer
|
||||
|
||||
class Ballot(EmbeddedObject):
|
||||
_id = UUIDField()
|
||||
@ -36,15 +39,23 @@ class Voter(EmbeddedObject):
|
||||
|
||||
class Question(EmbeddedObject):
|
||||
prompt = StringField()
|
||||
|
||||
def compute_result(self):
|
||||
...
|
||||
|
||||
class Result(EmbeddedObject):
|
||||
pass
|
||||
|
||||
class ApprovalQuestion(Question):
|
||||
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):
|
||||
choices = ListField(IntField())
|
||||
|
@ -48,7 +48,7 @@ class ElectionTestCase(TestCase):
|
||||
voter = Voter()
|
||||
election.voters.append(voter)
|
||||
# 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'])
|
||||
election.questions.append(question)
|
||||
@ -83,5 +83,12 @@ class ElectionTestCase(TestCase):
|
||||
election.save()
|
||||
|
||||
# Compute result
|
||||
election.results = [None, None]
|
||||
for i in range(2):
|
||||
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])
|
||||
|
@ -131,7 +131,7 @@ class EosObject(metaclass=EosObjectType):
|
||||
class EosList(EosObject, list):
|
||||
def append(self, value):
|
||||
if isinstance(value, EosObject):
|
||||
value._instance = (self, None)
|
||||
value._instance = (self, len(self))
|
||||
if not value._inited:
|
||||
value.post_init()
|
||||
return super().append(value)
|
||||
|
Loading…
Reference in New Issue
Block a user