Skip to content
This repository has been archived by the owner on Jan 28, 2020. It is now read-only.

Commit

Permalink
Add candidacy wizard: make historic elections an option
Browse files Browse the repository at this point in the history
Previously the default was to show all elections, whether they were
current or not, but @symroe pointed out that there might be thousands of
such elections, which in normal use would be irrelevant.

This commit makes the inclusion of non-current ('historic') elections in
the election picker stage an option - they're only shown if there's an
'historic=1' query parameter. There's also a link to that version of the
picker from the normal one.
  • Loading branch information
mhl committed Dec 20, 2016
1 parent 93bdc7b commit e571ab3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
9 changes: 7 additions & 2 deletions candidates/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,13 @@ class Meta:

class AddCandidacyPickElectionForm(forms.Form):

election = forms.ModelChoiceField(
queryset=Election.objects.order_by('-election_date', 'name'))
def __init__(self, *args, **kwargs):
include_historic = kwargs.pop('include_historic')
super(AddCandidacyPickElectionForm, self).__init__(*args, **kwargs)
election_qs = Election.objects.order_by('-election_date', 'name')
if not include_historic:
election_qs = election_qs.filter(current=True)
self.fields['election'] = forms.ModelChoiceField(queryset=election_qs)

def clean_election(self):
election = self.cleaned_data['election']
Expand Down
3 changes: 2 additions & 1 deletion candidates/templates/candidates/_person_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ <h3>{{ constituency_fields.election_name }}</h3>
{% endfor %}

{% if not add_candidate_form %}
<h2>{% trans "Add a candidacy in another election" %}</h2>
<div class="add-candidacy-link">
<a href="{% url 'person-update-add-candidacy' person_id=person.id %}">
{% trans "Add a candidacy in an election" %}
{% trans "Add a new candidacy" %}
</a>
</div>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ <h3>{% trans "Choose the election to which you're adding a candidacy" %}</h3>
{% block candidacy_wizard_submit %}
<input type="submit" class="button pick-election"
value="{% trans "Select" context "On the submit button to select an election to add a candidacy for." %}" />
{% if not include_historic %}
<p>Or there's <a href="?historic=1">another form if you need to add a candidacy for a historic election.</a></p>
{% endif %}
{% endblock %}

{% block extra_js %}
Expand Down
33 changes: 33 additions & 0 deletions candidates/tests/test_add_candidacy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

import re

from django.core.urlresolvers import reverse
from django.utils.six import text_type
Expand Down Expand Up @@ -228,3 +229,35 @@ def test_all_steps_with_party_list_position_successful(self):
person=self.person,
role='Candidate')
self.assertEqual(m.extra.party_list_position, 3)

def test_election_pick_historic_link_present(self):
response = self.app.get(self.wizard_url, user=self.user)
link = response.html.find(
'a',
text=re.compile(r'add a candidacy for a historic election'))
self.assertTrue(link)

def test_election_pick_historic_link_not_present(self):
url = self.wizard_url + '?historic=1'
response = self.app.get(url, user=self.user)
link = response.html.find(
'a',
text=re.compile(r'add a candidacy for a historic election'))
self.assertFalse(link)

def test_election_pick_only_current_by_default(self):
response = self.app.get(self.wizard_url, user=self.user)
election_form = response.forms['add-candidacy-wizard']
election_field = election_form['election-election']
self.assertEqual(
[t[2] for t in election_field.options],
['---------', '2015 General Election'])

def test_election_all_elections_with_historic_parameter(self):
url = self.wizard_url + '?historic=1'
response = self.app.get(url, user=self.user)
election_form = response.forms['add-candidacy-wizard']
election_field = election_form['election-election']
self.assertEqual(
[t[2] for t in election_field.options],
['---------', '2015 General Election', '2010 General Election'])
4 changes: 3 additions & 1 deletion candidates/views/candidacies.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ def get_post_slug(self):
def get_form_kwargs(self, step=None):
kwargs = super(AddCandidacyWizardView, self).get_form_kwargs(step)
if step == 'election':
return kwargs
include_historic = self.request.GET.get('historic') == '1'
kwargs['include_historic'] = include_historic
elif step == 'post':
cleaned_data = self.get_cleaned_data_for_step('election')
kwargs['election'] = cleaned_data['election']
Expand All @@ -198,6 +199,7 @@ def get_form_kwargs(self, step=None):
def get_context_data(self, form, **kwargs):
context = super(AddCandidacyWizardView, self).get_context_data(form, **kwargs)
context['person'] = self.person
context['include_historic'] = (self.request.GET.get('historic') == '1')
return context

def done(self, form_list, **kwargs):
Expand Down

0 comments on commit e571ab3

Please sign in to comment.