Commit d7387005 authored by Sli's avatar Sli

Better role creation

parent 02913d91
......@@ -350,7 +350,7 @@ th {
{%- endif %}
<a href="{{url('election:create_list')}}">{% trans %}Add a new list{% endtrans %}</a>
{%- if user.can_edit(election) %}
<a href="{{url('election:create_role')}}">{% trans %}Add a new role{% endtrans %}</a>
<a href="{{url('election:create_role', election_id=object.id)}}">{% trans %}Add a new role{% endtrans %}</a>
{%- endif %}
</section>
{% endblock %}
......
......@@ -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<election_id>[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<election_id>[0-9]+)/role/create$', RoleCreateView.as_view(), name='create_role'),
url(r'^(?P<election_id>[0-9]+)/candidate/add$', CandidatureCreateView.as_view(), name='candidate'),
url(r'^(?P<election_id>[0-9]+)/vote$', VoteFormView.as_view(), name='vote'),
url(r'^(?P<election_id>[0-9]+)/detail$', ElectionDetailView.as_view(), name='detail'),
]
......@@ -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})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment