diff --git a/helios/datatypes/__init__.py b/helios/datatypes/__init__.py index 2a641e68095db4fddbdb3a10fe1f9458aaedfa13..6281ca24b0947a082f81965581223974a57fd7aa 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 aacb833b3dce083b7ec1efdcba69fde1331c4349..d82a94b4ee2b2887fb582f8a66a069af22d5a677 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 8084560e1bde7eb78bc3f613c61270ea0c081094..00ed736541064236a6f2abdebbd5523f9de733b1 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 084a696f6636f6a09df476cb11f2f72535955194..f251054f4609aed1e1beff24f5b717e02934fdfc 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 fe5154299d1219a22146235441bddc473dff6b51..57915f179855b1bff6f107cc69fd606755caecc3 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]