From 3002d45719b82010e74f941f1fa5c5f57d2b5596 Mon Sep 17 00:00:00 2001
From: Ben Adida <ben@adida.net>
Date: Sun, 8 Nov 2015 20:11:14 +0000
Subject: [PATCH] added ability to distinguish teachers and students and do
 eligibility by grade

---
 helios/security.py                   |  2 +-
 helios_auth/auth_systems/cas.py      |  8 ++++++
 helios_auth/auth_systems/clever.py   | 40 +++++++++++++++++++++++++---
 helios_auth/auth_systems/facebook.py |  7 +++++
 helios_auth/auth_systems/google.py   |  8 ++++++
 helios_auth/auth_systems/linkedin.py |  7 +++++
 helios_auth/auth_systems/live.py     |  8 ++++++
 helios_auth/auth_systems/password.py |  8 ++++++
 helios_auth/auth_systems/twitter.py  |  7 +++++
 helios_auth/auth_systems/yahoo.py    |  8 ++++++
 helios_auth/models.py                | 10 +++++++
 11 files changed, 108 insertions(+), 5 deletions(-)

diff --git a/helios/security.py b/helios/security.py
index 1a5a78d..971499e 100644
--- a/helios/security.py
+++ b/helios/security.py
@@ -187,7 +187,7 @@ def can_create_election(request):
   if helios.ADMIN_ONLY:
     return user.admin_p
   else:
-    return user != None
+    return user.can_create_election()
   
 def user_can_feature_election(user, election):
   if not user:
diff --git a/helios_auth/auth_systems/cas.py b/helios_auth/auth_systems/cas.py
index 139a6d7..8202ad2 100644
--- a/helios_auth/auth_systems/cas.py
+++ b/helios_auth/auth_systems/cas.py
@@ -242,3 +242,11 @@ def eligibility_category_id(constraint):
 
 def pretty_eligibility(constraint):
   return "Members of the Class of %s" % constraint['year']
+
+
+#
+# Election Creation
+#
+
+def can_create_election(user_id, user_info):
+  return True
diff --git a/helios_auth/auth_systems/clever.py b/helios_auth/auth_systems/clever.py
index 08456f5..dcd52d0 100644
--- a/helios_auth/auth_systems/clever.py
+++ b/helios_auth/auth_systems/clever.py
@@ -71,13 +71,14 @@ def get_user_info_after_auth(request):
   user_name = "%s %s" % (response['data']['name']['first'], response['data']['name']['last'])
   user_type = response['type']
   user_district = response['data']['district']
+  user_grade = response['data'].get('grade', None)
 
   print content
   
   # watch out, response also contains email addresses, but not sure whether thsoe are verified or not
   # so for email address we will only look at the id_token
   
-  return {'type' : 'clever', 'user_id': user_id, 'name': user_name , 'info': {"district": user_district, "type": user_type}, 'token': {'access_token': access_token}}
+  return {'type' : 'clever', 'user_id': user_id, 'name': user_name , 'info': {"district": user_district, "type": user_type, "grade": user_grade}, 'token': {'access_token': access_token}}
     
 def do_logout(user):
   """
@@ -96,9 +97,40 @@ def send_message(user_id, name, user_info, subject, body):
   send email to google users. user_id is the email for google.
   """
   pass
+
+#
+# eligibility
+#
+
+def check_constraint(constraint, user):
+  if not user.info.has_key('grade'):
+    return False
+  return constraint['grade'] == user.info['grade']
+
+def generate_constraint(category, user):
+  """
+  generate the proper basic data structure to express a constraint
+  based on the category string
+  """
+  return {'grade': category}
+
+def list_categories(user):
+  return [{"id": str(g), "name": "Grade %d" % g} for g in range(3,13)]
   
-def check_constraint(constraint, user_info):
+def eligibility_category_id(constraint):
+  return constraint['grade']
+
+def pretty_eligibility(constraint):
+  return "Grade %s" % constraint['grade']
+  
+
+
+#
+# Election Creation
+#
+
+def can_create_election(user_id, user_info):
   """
-  for eligibility
+  Teachers only for now
   """
-  pass
+  return user_info['type'] == 'teacher'
diff --git a/helios_auth/auth_systems/facebook.py b/helios_auth/auth_systems/facebook.py
index d510ced..1790135 100644
--- a/helios_auth/auth_systems/facebook.py
+++ b/helios_auth/auth_systems/facebook.py
@@ -116,3 +116,10 @@ def eligibility_category_id(constraint):
 
 def pretty_eligibility(constraint):
   return "Facebook users who are members of the \"%s\" group" % constraint['group']['name']
+
+#
+# Election Creation
+#
+
+def can_create_election(user_id, user_info):
+  return True
diff --git a/helios_auth/auth_systems/google.py b/helios_auth/auth_systems/google.py
index 00a6bb7..2bdd38c 100644
--- a/helios_auth/auth_systems/google.py
+++ b/helios_auth/auth_systems/google.py
@@ -82,3 +82,11 @@ def check_constraint(constraint, user_info):
   for eligibility
   """
   pass
+
+
+#
+# Election Creation
+#
+
+def can_create_election(user_id, user_info):
+  return True
diff --git a/helios_auth/auth_systems/linkedin.py b/helios_auth/auth_systems/linkedin.py
index 69e3e9a..32b0033 100644
--- a/helios_auth/auth_systems/linkedin.py
+++ b/helios_auth/auth_systems/linkedin.py
@@ -89,3 +89,10 @@ def send_notification(user_id, user_info, message):
   pass
 
 
+
+#
+# Election Creation
+#
+
+def can_create_election(user_id, user_info):
+  return True
diff --git a/helios_auth/auth_systems/live.py b/helios_auth/auth_systems/live.py
index 6a1b20e..9f34a27 100644
--- a/helios_auth/auth_systems/live.py
+++ b/helios_auth/auth_systems/live.py
@@ -65,3 +65,11 @@ def update_status(user_id, user_info, token, message):
 
 def send_message(user_id, user_name, user_info, subject, body):
   pass
+
+
+#
+# Election Creation
+#
+
+def can_create_election(user_id, user_info):
+  return True
diff --git a/helios_auth/auth_systems/password.py b/helios_auth/auth_systems/password.py
index e39573e..625b865 100644
--- a/helios_auth/auth_systems/password.py
+++ b/helios_auth/auth_systems/password.py
@@ -117,3 +117,11 @@ def send_message(user_id, user_name, user_info, subject, body):
   email = user_id
   name = user_name or user_info.get('name', email)
   send_mail(subject, body, settings.SERVER_EMAIL, ["\"%s\" <%s>" % (name, email)], fail_silently=False)    
+
+
+#
+# Election Creation
+#
+
+def can_create_election(user_id, user_info):
+  return True
diff --git a/helios_auth/auth_systems/twitter.py b/helios_auth/auth_systems/twitter.py
index 343b555..9963f91 100644
--- a/helios_auth/auth_systems/twitter.py
+++ b/helios_auth/auth_systems/twitter.py
@@ -118,3 +118,10 @@ def follow_view(request):
     return HttpResponseRedirect(reverse(after_intervention))
 
 
+
+#
+# Election Creation
+#
+
+def can_create_election(user_id, user_info):
+  return True
diff --git a/helios_auth/auth_systems/yahoo.py b/helios_auth/auth_systems/yahoo.py
index dc29ab7..16bc034 100644
--- a/helios_auth/auth_systems/yahoo.py
+++ b/helios_auth/auth_systems/yahoo.py
@@ -52,3 +52,11 @@ def check_constraint(constraint, user_info):
   for eligibility
   """
   pass
+
+
+#
+# Election Creation
+#
+
+def can_create_election(user_id, user_info):
+  return True
diff --git a/helios_auth/models.py b/helios_auth/models.py
index 5ec2adf..b917995 100644
--- a/helios_auth/models.py
+++ b/helios_auth/models.py
@@ -70,6 +70,16 @@ class User(models.Model):
 
     return AUTH_SYSTEMS[self.user_type].STATUS_UPDATES
 
+  def can_create_election(self):
+    """
+    Certain auth systems can choose to limit election creation
+    to certain users. 
+    """
+    if not AUTH_SYSTEMS.has_key(self.user_type):
+      return False
+    
+    return AUTH_SYSTEMS[self.user_type].can_create_election(self.user_id, self.info)
+
   def update_status_template(self):
     if not self.can_update_status():
       return None
-- 
GitLab