diff --git a/auth/auth_systems/cas.py b/auth/auth_systems/cas.py index 407cccac471c8548f4af42638efe28b166538ed7..d89f6fec1897e837d2ddc77664a0f3c52300571a 100644 --- a/auth/auth_systems/cas.py +++ b/auth/auth_systems/cas.py @@ -215,8 +215,30 @@ def send_message(user_id, name, user_info, subject, body): name = email send_mail(subject, body, settings.SERVER_EMAIL, ["%s <%s>" % (name, email)], fail_silently=False) - + +# +# eligibility +# + def check_constraint(constraint, user): - if not user.user_info.has_key('category'): + if not user.info.has_key('category'): return False - return constraint['year'] == user.user_info['category'] + return constraint['year'] == user.info['category'] + +def generate_constraint(category_id, user): + """ + generate the proper basic data structure to express a constraint + based on the category string + """ + return {'year': category_id} + +def list_categories(user): + current_year = datetime.datetime.now().year + return [{'id': str(y), 'name': 'Class of %s' % y} for y + in range(current_year, current_year+5)] + +def eligibility_category_id(constraint): + return constraint['year'] + +def pretty_eligibility(constraint): + return "Members of the Class of %s" % constraint['year'] diff --git a/auth/auth_systems/facebook.py b/auth/auth_systems/facebook.py index 7951197f051fbabf6900653c6f42204fdc6f2f91..860d2122dc32828759ae80b020b2dae6f76aaa3b 100644 --- a/auth/auth_systems/facebook.py +++ b/auth/auth_systems/facebook.py @@ -107,5 +107,8 @@ def generate_constraint(category_id, user): def list_categories(user): return get_user_groups(user) +def eligibility_category_id(constraint): + return constraint['group']['id'] + def pretty_eligibility(constraint): return "Facebook users who are members of the \"%s\" group" % constraint['group']['name'] diff --git a/helios/models.py b/helios/models.py index 9841a773e566981d01fda3222ff2cb4713646535..967709c91868c09c0d4f50606e217c45d9d6c958 100644 --- a/helios/models.py +++ b/helios/models.py @@ -250,10 +250,18 @@ class Election(HeliosModel): return [constraint['constraint'] for constraint in self.eligibility if constraint['auth_system'] == user_type][0] + def eligibility_category_id(self, user_type): + "when eligibility is by category, this returns the category_id" + if not self.eligibility: + return None + + constraint = self.eligibility_constraint_for(user_type)[0] + return AUTH_SYSTEMS[user_type].eligibility_category_id(constraint) + @property def pretty_eligibility(self): if not self.eligibility: - return "Everyone can vote." + return "Anyone can vote." else: return_val = "<ul>" diff --git a/helios/templates/voters_list.html b/helios/templates/voters_list.html index fba557f6ebcbd2ed72f76627ab6aa31b0dcd09ba..68d46cb3d3b64ade97a4fd663ff96dfec917e2aa 100644 --- a/helios/templates/voters_list.html +++ b/helios/templates/voters_list.html @@ -26,7 +26,7 @@ You can change this setting: <input type="radio" name="eligibility" value="limitedreg" {% if election.eligibility %}CHECKED{% endif %} /> only voters who are members of <select name="category_id"> {% for category in categories %} -<option value="{{category.id}}"> {{category.name}}</option> +<option value="{{category.id}}" {% if eligibility_category_id == category.id %}SELECTED{% endif %}> {{category.name}}</option> {% endfor %} </select> <br /> diff --git a/helios/tests.py b/helios/tests.py index 61ccfa6debd9ebd05d23b2a6de819e4c5e6da86f..aa94b774968c99b273c4270ad8c1214e04599e55 100644 --- a/helios/tests.py +++ b/helios/tests.py @@ -166,6 +166,9 @@ class ElectionModelTests(TestCase): self.assertTrue(self.election.user_eligible_p(self.fb_user)) + # also check that eligibility_category_id does the right thing + self.assertEquals(self.election.eligibility_category_id('facebook'), '123') + def test_freeze(self): # freezing without trustees and questions, no good def try_freeze(): @@ -711,7 +714,7 @@ class ElectionBlackboxTests(WebTest): elig_form['eligibility'] = 'openreg' elig_page = elig_form.submit().follow() - self.assertContains(elig_page, "Everyone can vote") + self.assertContains(elig_page, "Anyone can vote") elig_form = elig_page.form elig_form['eligibility'] = 'closedreg' diff --git a/helios/views.py b/helios/views.py index 169d9c43d46012ee798d4d5eca2c1e3ae4657b32..e54e50871e5336c5e7e589df419a711e0d94a0b2 100644 --- a/helios/views.py +++ b/helios/views.py @@ -1115,8 +1115,10 @@ def voters_list_pretty(request, election): admin_p = security.user_can_admin_election(user, election) categories = None + eligibility_category_id = None if admin_p and can_list_categories(user.user_type): categories = AUTH_SYSTEMS[user.user_type].list_categories(user) + eligibility_category_id = election.eligibility_category_id(user.user_type) # files being processed voter_files = election.voterfile_set.all() @@ -1143,7 +1145,8 @@ def voters_list_pretty(request, election): 'limit': limit, 'total_voters': total_voters, 'upload_p': helios.VOTERS_UPLOAD, 'q' : q, 'voter_files': voter_files, - 'categories': categories}) + 'categories': categories, + 'eligibility_category_id' : eligibility_category_id}) @election_admin() def voters_eligibility(request, election):