diff --git a/election/templates/election/election_detail.jinja b/election/templates/election/election_detail.jinja index 6c0e6cb6..c8fdf077 100644 --- a/election/templates/election/election_detail.jinja +++ b/election/templates/election/election_detail.jinja @@ -350,7 +350,7 @@ th { {%- endif %} {% trans %}Add a new list{% endtrans %} {%- if user.can_edit(election) %} - {% trans %}Add a new role{% endtrans %} + {% trans %}Add a new role{% endtrans %} {%- endif %} {% endblock %} diff --git a/election/urls.py b/election/urls.py index 59ad8a42..33cfdb33 100644 --- a/election/urls.py +++ b/election/urls.py @@ -4,10 +4,10 @@ from election.views import * urlpatterns = [ url(r'^$', ElectionsListView.as_view(), name='list'), - url(r'^create$', ElectionCreateView.as_view(), name='create'), - url(r'^list/create$', ElectionListCreateView.as_view(), name='create_list'), - url(r'^role/create$', RoleCreateView.as_view(), name='create_role'), - url(r'^(?P[0-9]+)/candidate$', CandidatureCreateView.as_view(), name='candidate'), + url(r'^add$', ElectionCreateView.as_view(), name='create'), + url(r'^list/add$', ElectionListCreateView.as_view(), name='create_list'), + url(r'^(?P[0-9]+)/role/create$', RoleCreateView.as_view(), name='create_role'), + url(r'^(?P[0-9]+)/candidate/add$', CandidatureCreateView.as_view(), name='candidate'), url(r'^(?P[0-9]+)/vote$', VoteFormView.as_view(), name='vote'), url(r'^(?P[0-9]+)/detail$', ElectionDetailView.as_view(), name='detail'), ] diff --git a/election/views.py b/election/views.py index 6a3c098f..21bf5a87 100644 --- a/election/views.py +++ b/election/views.py @@ -31,7 +31,7 @@ class LimitedCheckboxField(forms.ModelMultipleChoiceField): self.max_choice = max_choice widget = forms.CheckboxSelectMultiple() super(LimitedCheckboxField, self).__init__(queryset, None, required, widget, label, - initial, help_text, *args, **kwargs) + initial, help_text, *args, **kwargs) def clean(self, value): qs = super(LimitedCheckboxField, self).clean(value) @@ -87,6 +87,12 @@ class RoleForm(forms.ModelForm): model = Role fields = ['title', 'election', 'description', 'max_choice'] + def __init__(self, *args, **kwargs): + election_id = kwargs.pop('election_id', None) + super(RoleForm, self).__init__(*args, **kwargs) + if election_id: + self.fields['election'].queryset = Election.objects.filter(id=election_id).all() + def clean(self): cleaned_data = super(RoleForm, self).clean() title = cleaned_data.get('title') @@ -267,6 +273,15 @@ class RoleCreateView(CanCreateMixin, CreateView): form_class = RoleForm template_name = 'core/page_prop.jinja' + def dispatch(self, request, *arg, **kwargs): + self.election = get_object_or_404(Election, pk=kwargs['election_id']) + return super(RoleCreateView, self).dispatch(request, *arg, **kwargs) + + def get_initial(self): + init = {} + init['election'] = self.election + return init + def form_valid(self, form): """ Verify that the user can edit proprely @@ -278,6 +293,11 @@ class RoleCreateView(CanCreateMixin, CreateView): return super(CreateView, self).form_valid(form) raise PermissionDenied + def get_form_kwargs(self): + kwargs = super(RoleCreateView, self).get_form_kwargs() + kwargs['election_id'] = self.election.id + return kwargs + def get_success_url(self, **kwargs): return reverse_lazy('election:detail', kwargs={'election_id': self.object.election.id})