mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-22 14:13:21 +00:00
Better role creation
This commit is contained in:
parent
02913d91e6
commit
d7387005c0
@ -350,7 +350,7 @@ th {
|
|||||||
{%- endif %}
|
{%- endif %}
|
||||||
<a href="{{url('election:create_list')}}">{% trans %}Add a new list{% endtrans %}</a>
|
<a href="{{url('election:create_list')}}">{% trans %}Add a new list{% endtrans %}</a>
|
||||||
{%- if user.can_edit(election) %}
|
{%- 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 %}
|
{%- endif %}
|
||||||
</section>
|
</section>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -4,10 +4,10 @@ from election.views import *
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', ElectionsListView.as_view(), name='list'),
|
url(r'^$', ElectionsListView.as_view(), name='list'),
|
||||||
url(r'^create$', ElectionCreateView.as_view(), name='create'),
|
url(r'^add$', ElectionCreateView.as_view(), name='create'),
|
||||||
url(r'^list/create$', ElectionListCreateView.as_view(), name='create_list'),
|
url(r'^list/add$', ElectionListCreateView.as_view(), name='create_list'),
|
||||||
url(r'^role/create$', RoleCreateView.as_view(), name='create_role'),
|
url(r'^(?P<election_id>[0-9]+)/role/create$', RoleCreateView.as_view(), name='create_role'),
|
||||||
url(r'^(?P<election_id>[0-9]+)/candidate$', CandidatureCreateView.as_view(), name='candidate'),
|
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]+)/vote$', VoteFormView.as_view(), name='vote'),
|
||||||
url(r'^(?P<election_id>[0-9]+)/detail$', ElectionDetailView.as_view(), name='detail'),
|
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
|
self.max_choice = max_choice
|
||||||
widget = forms.CheckboxSelectMultiple()
|
widget = forms.CheckboxSelectMultiple()
|
||||||
super(LimitedCheckboxField, self).__init__(queryset, None, required, widget, label,
|
super(LimitedCheckboxField, self).__init__(queryset, None, required, widget, label,
|
||||||
initial, help_text, *args, **kwargs)
|
initial, help_text, *args, **kwargs)
|
||||||
|
|
||||||
def clean(self, value):
|
def clean(self, value):
|
||||||
qs = super(LimitedCheckboxField, self).clean(value)
|
qs = super(LimitedCheckboxField, self).clean(value)
|
||||||
@ -87,6 +87,12 @@ class RoleForm(forms.ModelForm):
|
|||||||
model = Role
|
model = Role
|
||||||
fields = ['title', 'election', 'description', 'max_choice']
|
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):
|
def clean(self):
|
||||||
cleaned_data = super(RoleForm, self).clean()
|
cleaned_data = super(RoleForm, self).clean()
|
||||||
title = cleaned_data.get('title')
|
title = cleaned_data.get('title')
|
||||||
@ -267,6 +273,15 @@ class RoleCreateView(CanCreateMixin, CreateView):
|
|||||||
form_class = RoleForm
|
form_class = RoleForm
|
||||||
template_name = 'core/page_prop.jinja'
|
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):
|
def form_valid(self, form):
|
||||||
"""
|
"""
|
||||||
Verify that the user can edit proprely
|
Verify that the user can edit proprely
|
||||||
@ -278,6 +293,11 @@ class RoleCreateView(CanCreateMixin, CreateView):
|
|||||||
return super(CreateView, self).form_valid(form)
|
return super(CreateView, self).form_valid(form)
|
||||||
raise PermissionDenied
|
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):
|
def get_success_url(self, **kwargs):
|
||||||
return reverse_lazy('election:detail', kwargs={'election_id': self.object.election.id})
|
return reverse_lazy('election:detail', kwargs={'election_id': self.object.election.id})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user