From e9bbd3db8ee0513c17ea013b882859cec6f82134 Mon Sep 17 00:00:00 2001 From: Ben Adida <ben@adida.net> Date: Mon, 17 Jan 2011 18:56:45 -0800 Subject: [PATCH] fixed views to use automatically generated linked data object, and added interface from full cast vote to short cast vote --- helios/datatypes/__init__.py | 4 ++-- helios/datatypes/core.py | 4 ++-- helios/datatypes/legacy.py | 16 ++++++++++------ helios/view_utils.py | 6 +++--- helios/views.py | 6 ++++-- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/helios/datatypes/__init__.py b/helios/datatypes/__init__.py index 2a641e6..6281ca2 100644 --- a/helios/datatypes/__init__.py +++ b/helios/datatypes/__init__.py @@ -274,8 +274,8 @@ class BaseArrayOfObjects(LDObject): def __init__(self, wrapped_obj): super(BaseArrayOfObjects, self).__init__(wrapped_obj) - def toDict(self): - return [item.toDict() for item in self.items] + def toDict(self, complete=False): + return [item.toDict(complete=complete) for item in self.items] def loadData(self): "go through each item and LD instantiate it, as if it were a structured field" diff --git a/helios/datatypes/core.py b/helios/datatypes/core.py index aacb833..d82a94b 100644 --- a/helios/datatypes/core.py +++ b/helios/datatypes/core.py @@ -11,7 +11,7 @@ class BigInteger(LDObject): """ WRAPPED_OBJ_CLASS = int - def toDict(self): + def toDict(self, complete=False): if self.wrapped_obj: return str(self.wrapped_obj) else: @@ -22,7 +22,7 @@ class BigInteger(LDObject): self.wrapped_obj = int(d) class Timestamp(LDObject): - def toDict(self): + def toDict(self, complete=False): if self.wrapped_obj: return str(self.wrapped_obj) else: diff --git a/helios/datatypes/legacy.py b/helios/datatypes/legacy.py index 8084560..00ed736 100644 --- a/helios/datatypes/legacy.py +++ b/helios/datatypes/legacy.py @@ -66,14 +66,14 @@ class Voter(LegacyObject): ALIASED_VOTER_FIELDS = ['election_uuid', 'uuid', 'alias'] - def toDict(self): + def toDict(self, complete=False): """ depending on whether the voter is aliased, use different fields """ if self.wrapped_obj.alias != None: - return super(Voter, self).toDict(self.ALIASED_VOTER_FIELDS) + return super(Voter, self).toDict(self.ALIASED_VOTER_FIELDS, complete = complete) else: - return super(Voter,self).toDict() + return super(Voter,self).toDict(complete = complete) class ShortCastVote(LegacyObject): @@ -86,6 +86,10 @@ class CastVote(LegacyObject): 'cast_at' : 'core/Timestamp', 'vote' : 'legacy/EncryptedVote'} + @property + def short(self): + return self.instantiate(self.wrapped_obj, datatype='legacy/ShortCastVote') + class Trustee(LegacyObject): FIELDS = ['uuid', 'public_key', 'public_key_hash', 'pok', 'decryption_factors', 'decryption_proofs', 'email'] @@ -143,9 +147,9 @@ class EGZKDisjunctiveProof(LegacyObject): "hijack and make sure we add the proofs name back on" return super(EGZKDisjunctiveProof, self).loadDataFromDict({'proofs': d}) - def toDict(self): + def toDict(self, complete = False): "hijack toDict and make it return the proofs array only, since that's the spec for legacy" - return super(EGZKDisjunctiveProof, self).toDict()['proofs'] + return super(EGZKDisjunctiveProof, self).toDict(complete=complete)['proofs'] class DLogProof(LegacyObject): WRAPPED_OBJ_CLASS = crypto_elgamal.DLogProof @@ -170,7 +174,7 @@ class Questions(LegacyObject): def loadDataFromDict(self, d): self.wrapped_obj = d - def toDict(self): + def toDict(self, complete=False): return self.wrapped_obj diff --git a/helios/view_utils.py b/helios/view_utils.py index 084a696..f251054 100644 --- a/helios/view_utils.py +++ b/helios/view_utils.py @@ -84,10 +84,10 @@ def json(func): """ def convert_to_json(self, *args, **kwargs): return_val = func(self, *args, **kwargs) - if isinstance(return_val, datatypes.LDObject): - return render_json(return_val.serialize()) - else: + try: return render_json(utils.to_json(return_val)) + except: + import pdb; pdb.set_trace() return update_wrapper(convert_to_json,func) diff --git a/helios/views.py b/helios/views.py index fe51542..57915f1 100644 --- a/helios/views.py +++ b/helios/views.py @@ -319,7 +319,7 @@ def socialbuttons(request): @election_view() def list_trustees(request, election): trustees = Trustee.get_by_election(election) - return [datatypes.LDObject.instantiate(t, 'legacy/Trustee').toDict() for t in trustees] + return [t.toJSONDict(complete=True) for t in trustees] @election_view() def list_trustees_view(request, election): @@ -1172,7 +1172,9 @@ def ballot_list(request, election): after = datetime.datetime.strptime(request.GET['after'], '%Y-%m-%d %H:%M:%S') voters = Voter.get_by_election(election, cast=True, order_by='cast_at', limit=limit, after=after) - return [datatypes.LDObject.instantiate(v.last_cast_vote(), 'legacy/ShortCastVote').toDict() for v in voters] + + # we explicitly cast this to a short cast vote + return [v.last_cast_vote().ld_object.short.toDict(complete=True) for v in voters] -- GitLab