From dfe1074c6c5afa2f66519771416b84c042bc3614 Mon Sep 17 00:00:00 2001
From: Ben Adida <ben@adida.net>
Date: Mon, 24 Jan 2011 14:50:32 -0800
Subject: [PATCH] fixed logic for password+other elections, mostly

---
 auth/auth_systems/__init__.py               |  2 +-
 auth/views.py                               |  8 ++++--
 helios/datatypes/2011/01.py                 |  8 ++++--
 helios/datatypes/__init__.py                | 28 ++++++++++++++++++---
 helios/datatypes/legacy.py                  | 19 ++------------
 helios/templates/election_cast_confirm.html |  5 ++++
 helios/views.py                             | 12 ++++++---
 reset.sh                                    |  2 +-
 8 files changed, 53 insertions(+), 31 deletions(-)

diff --git a/auth/auth_systems/__init__.py b/auth/auth_systems/__init__.py
index 39f5ef1..676d268 100644
--- a/auth/auth_systems/__init__.py
+++ b/auth/auth_systems/__init__.py
@@ -4,7 +4,7 @@ AUTH_SYSTEMS = {}
 import twitter, password, cas, facebook, google, yahoo, linkedin
 AUTH_SYSTEMS['twitter'] = twitter
 AUTH_SYSTEMS['linkedin'] = linkedin
-AUTH_SYSTEMS['password'] = password
+#AUTH_SYSTEMS['password'] = password
 AUTH_SYSTEMS['cas'] = cas
 AUTH_SYSTEMS['facebook'] = facebook
 AUTH_SYSTEMS['google'] = google
diff --git a/auth/views.py b/auth/views.py
index ce0bda8..db68095 100644
--- a/auth/views.py
+++ b/auth/views.py
@@ -55,8 +55,12 @@ def login_box_raw(request, return_url='/', auth_systems = None):
   default_auth_system_obj = None
   if auth.DEFAULT_AUTH_SYSTEM:
     default_auth_system_obj = AUTH_SYSTEMS[auth.DEFAULT_AUTH_SYSTEM]
-  
-  enabled_auth_systems = auth_systems or auth.ENABLED_AUTH_SYSTEMS
+
+  # make sure that auth_systems includes only available and enabled auth systems
+  if auth_systems != None:
+    enabled_auth_systems = set(auth_systems).intersection(set(auth.ENABLED_AUTH_SYSTEMS)).intersection(set(AUTH_SYSTEMS.keys()))
+  else:
+    enabled_auth_systems = set(auth.ENABLED_AUTH_SYSTEMS).intersection(set(AUTH_SYSTEMS.keys()))
 
   form = password.LoginForm()
 
diff --git a/helios/datatypes/2011/01.py b/helios/datatypes/2011/01.py
index 7f30ca2..9770c60 100644
--- a/helios/datatypes/2011/01.py
+++ b/helios/datatypes/2011/01.py
@@ -2,7 +2,7 @@
 data types for 2011/01 Helios
 """
 
-from helios.datatypes import LDObject, arrayOf
+from helios.datatypes import LDObject, arrayOf, DictObject, ListObject
 
 class Trustee(LDObject):
   """
@@ -26,7 +26,8 @@ class Election(LDObject):
     'public_key' : 'pkc/elgamal/PublicKey',
     'voting_starts_at': 'core/Timestamp',
     'voting_ends_at': 'core/Timestamp',
-    'frozen_at': 'core/Timestamp'
+    'frozen_at': 'core/Timestamp',
+    'questions': '2011/01/Questions',
     }
 
 class Voter(LDObject):
@@ -43,3 +44,6 @@ class EncryptedAnswer(LDObject):
         }
 
 
+class Questions(ListObject, LDObject):
+    WRAPPED_OBJ = list
+
diff --git a/helios/datatypes/__init__.py b/helios/datatypes/__init__.py
index 6281ca2..0901a33 100644
--- a/helios/datatypes/__init__.py
+++ b/helios/datatypes/__init__.py
@@ -62,8 +62,9 @@ def get_class(datatype):
             dynamic_ptr = getattr(dynamic_ptr, attr)
         dynamic_cls = dynamic_ptr
     except AttributeError:
-        raise Exception ("no module for %s" % datatype)
-    
+        raise Exception ("no module for %s" % datatype)    
+
+    dynamic_cls.datatype = datatype
         
     return dynamic_cls
         
@@ -189,8 +190,12 @@ class LDObject(object):
             else:
                 val[f] = self.process_value_out(f, self._getattr_wrapped(f))
 
-        if complete and self.USE_JSON_LD:
-            val['#'] = {'_': 'http://heliosvoting.org/ns#'}
+        if self.USE_JSON_LD:
+            if complete:
+                val['#'] = {'_': 'http://heliosvoting.org/ns#'}
+
+            if hasattr(self, 'datatype'):
+                val['a'] = self.datatype
 
         return val
 
@@ -298,3 +303,18 @@ def arrayOf(element_type):
 
     return ArrayOfTypedObjects
 
+class DictObject(object):
+    "when the wrapped object is actually dictionary"
+    def _getattr_wrapped(self, attr):
+        return self.wrapped_obj[attr]
+
+    def _setattr_wrapped(self, attr, val):
+        self.wrapped_obj[attr] = val
+
+class ListObject(object):
+    def loadDataFromDict(self, d):
+        self.wrapped_obj = d
+
+    def toDict(self, complete=False):
+        return self.wrapped_obj
+
diff --git a/helios/datatypes/legacy.py b/helios/datatypes/legacy.py
index 75f8e8f..36cbe9d 100644
--- a/helios/datatypes/legacy.py
+++ b/helios/datatypes/legacy.py
@@ -2,7 +2,7 @@
 Legacy datatypes for Helios (v3.0)
 """
 
-from helios.datatypes import LDObject, arrayOf
+from helios.datatypes import LDObject, arrayOf, DictObject, ListObject
 from helios.crypto import elgamal as crypto_elgamal
 from helios.workflows import homomorphic
 
@@ -13,14 +13,6 @@ class LegacyObject(LDObject):
     WRAPPED_OBJ_CLASS = dict
     USE_JSON_LD = False
 
-class DictObject(object):
-    "when the wrapped object is actually dictionary"
-    def _getattr_wrapped(self, attr):
-        return self.wrapped_obj[attr]
-
-    def _setattr_wrapped(self, attr, val):
-        self.wrapped_obj[attr] = val
-
 class Election(LegacyObject):
     WRAPPED_OBJ_CLASS = homomorphic.Election
     FIELDS = ['uuid', 'questions', 'name', 'short_name', 'description', 'voters_hash', 'openreg',
@@ -188,16 +180,9 @@ class Result(LegacyObject):
     def toDict(self, complete=False):
         return self.wrapped_obj
 
-class Questions(LegacyObject):
+class Questions(ListObject, LegacyObject):
     WRAPPED_OBJ = list
 
-    def loadDataFromDict(self, d):
-        self.wrapped_obj = d
-
-    def toDict(self, complete=False):
-        return self.wrapped_obj
-
-
 class Tally(LegacyObject):
     WRAPPED_OBJ_CLASS = homomorphic.Tally
     FIELDS = ['tally', 'num_tallied']
diff --git a/helios/templates/election_cast_confirm.html b/helios/templates/election_cast_confirm.html
index a16a786..5621d85 100644
--- a/helios/templates/election_cast_confirm.html
+++ b/helios/templates/election_cast_confirm.html
@@ -98,7 +98,12 @@ the same account you registered with.
 {% endif %}
 </p>
 
+{% if show_password %}
+{% include "_castconfirm_password.html" %}
+{% endif %}
+
 {{login_box|safe}}
+
 <br />
 Don't worry, we'll remember your ballot while you log in.
 {% endif %}
diff --git a/helios/views.py b/helios/views.py
index eaa25ec..6907d29 100644
--- a/helios/views.py
+++ b/helios/views.py
@@ -567,11 +567,15 @@ def one_election_cast_confirm(request, election):
     else:
       auth_systems = None
 
-    if auth_systems == ['password']:
-      password_only = True
+    password_only = False
+    if auth_systems == None or 'password' in auth_systems:
+      show_password = True
       password_login_form = forms.VoterPasswordForm()
+
+      if auth_systems == ['password']:
+        password_only = True
     else:
-      password_only = False
+      show_password = False
       password_login_form = None
 
     return_url = reverse(one_election_cast_confirm, args=[election.uuid])
@@ -581,7 +585,7 @@ def one_election_cast_confirm(request, election):
         'login_box': login_box, 'election' : election, 'vote_fingerprint': vote_fingerprint,
         'past_votes': past_votes, 'issues': issues, 'voter' : voter,
         'status_update_label': status_update_label, 'status_update_message': status_update_message,
-        'password_only': password_only, 'password_login_form': password_login_form})
+        'show_password': show_password, 'password_only': password_only, 'password_login_form': password_login_form})
       
   if request.method == "POST":
     check_csrf(request)
diff --git a/reset.sh b/reset.sh
index eb22b5c..685d4b6 100755
--- a/reset.sh
+++ b/reset.sh
@@ -3,4 +3,4 @@ dropdb helios
 createdb helios
 python manage.py syncdb
 python manage.py migrate
-echo "from auth.models import User; User.update_or_create(user_type='password',user_id='benadida',info={'password':'test'})" | python manage.py shell
\ No newline at end of file
+echo "from auth.models import User; User.update_or_create(user_type='google',user_id='ben@adida.net',info={})" | python manage.py shell
\ No newline at end of file
-- 
GitLab