From 247f050d5ba37b715a0f8abac293fa5aec3c3565 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Fri, 24 Nov 2017 19:51:10 +1100 Subject: [PATCH] Pretty results display --- eos/base/election.py | 14 ++++++++++++++ eos/core/objects/__init__.py | 5 +++++ .../templates/question/approval/result_raw.html | 15 +++++++-------- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/eos/base/election.py b/eos/base/election.py index 85c637e..b30cd0d 100644 --- a/eos/base/election.py +++ b/eos/base/election.py @@ -55,12 +55,26 @@ class ApprovalQuestion(Question): choices = ListField(StringField()) min_choices = IntField() max_choices = IntField() + + def pretty_answer(self, answer): + return ', '.join([self.choices[choice] for choice in answer.choices]) class ApprovalAnswer(Answer): choices = ListField(IntField()) class RawResult(Result): answers = EmbeddedObjectListField() + + def count(self): + combined = [] + for answer in self.answers: + index = next((i for i, val in enumerate(combined) if val[0] == answer), None) + if index is None: + combined.append([answer, 1]) + else: + combined[index][1] += 1 + combined.sort(key=lambda x: x[1], reverse=True) + return combined class Election(TopLevelObject): _id = UUIDField() diff --git a/eos/core/objects/__init__.py b/eos/core/objects/__init__.py index ad5f57a..86ead73 100644 --- a/eos/core/objects/__init__.py +++ b/eos/core/objects/__init__.py @@ -154,6 +154,11 @@ class EosObject(metaclass=EosObjectType): return self._instance[0].recurse_parents(cls) return None + def __eq__(self, other): + if not isinstance(other, EosObject): + return False + return EosObject.serialise_and_wrap(self) == EosObject.serialise_and_wrap(other) + @staticmethod def serialise_and_wrap(value, object_type=None, for_hash=False, should_protect=False): if object_type: diff --git a/eosweb/core/templates/question/approval/result_raw.html b/eosweb/core/templates/question/approval/result_raw.html index 8db40f3..2712b64 100644 --- a/eosweb/core/templates/question/approval/result_raw.html +++ b/eosweb/core/templates/question/approval/result_raw.html @@ -16,12 +16,11 @@ along with this program. If not, see . #} -