From 0f744df54521e2238b4d8b19380a4c03bdb83aa5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Nov=C3=BD?= <github-lnovy@krtek.net>
Date: Mon, 6 Jul 2015 13:30:36 +0000
Subject: [PATCH] Add support for fetch voters list and freezing it

---
 helios/models.py | 39 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)

diff --git a/helios/models.py b/helios/models.py
index 695494f..a10d2fa 100644
--- a/helios/models.py
+++ b/helios/models.py
@@ -436,7 +436,40 @@ class Election(HeliosModel):
   def increment_cast_votes(self):
     ## FIXME
     return 0
-        
+
+  def load_eligible_voters(self):
+    """
+    try to load voters by eligibility constraints
+    """
+
+    if self.eligibility == None:
+      return
+
+    if self.openreg == False:
+      return
+
+    total_load = True
+
+    for eligibility_case in self.eligibility:
+      print eligibility_case
+      auth_system = eligibility_case['auth_system']
+
+      if not eligibility_case.has_key('constraint'):
+        total_load = False
+      else:
+        if hasattr(AUTH_SYSTEMS[auth_system], 'can_list_category_members'):
+          for constraint in eligibility_case['constraint']:
+            category_id = AUTH_SYSTEMS[auth_system].eligibility_category_id(constraint)
+            for u in AUTH_SYSTEMS[auth_system].list_category_members(category_id):
+              user = User.update_or_create(user_type = u['type'], user_id = u['id'], name = u['name'], info = u['info'], token = u['token'])
+              Voter.register_user_in_election(user, self)
+        else:
+          total_load = False
+
+    if total_load:
+      self.openreg = False
+
+
   def set_eligibility(self):
     """
     if registration is closed and eligibility has not been
@@ -485,7 +518,9 @@ class Election(HeliosModel):
       raise Exception("cannot freeze an election that has issues")
 
     self.frozen_at = datetime.datetime.utcnow()
-    
+
+    self.load_eligible_voters()
+
     # voters hash
     self.generate_voters_hash()
 
-- 
GitLab