mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-22 14:13:21 +00:00
Pimp role and list forms and add edit for election
This commit is contained in:
parent
d7387005c0
commit
4f62863599
@ -88,8 +88,10 @@
|
|||||||
<hr>
|
<hr>
|
||||||
<h4>{% trans %}Elections{% endtrans %}</h4>
|
<h4>{% trans %}Elections{% endtrans %}</h4>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="{{ url('election:list') }}">{% trans %}Vote{% endtrans %}</a></li>
|
<li><a href="{{ url('election:list') }}">{% trans %}See avaliable elections{% endtrans %}</a></li>
|
||||||
<li><a href="">{% trans %}Candidate{% endtrans %}</a></li>
|
{%- if user.is_subscribed() -%}
|
||||||
|
<li><a href="{{ url('election:create') }}">{% trans %}Create a new election{% endtrans %}</a></li>
|
||||||
|
{%- endif -%}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -348,9 +348,10 @@ th {
|
|||||||
{%- if election.can_candidate(user) or user.can_edit(election) %}
|
{%- if election.can_candidate(user) or user.can_edit(election) %}
|
||||||
<a href="{{url('election:candidate', election_id=object.id)}}">{% trans %}Candidate{% endtrans %}</a>
|
<a href="{{url('election:candidate', election_id=object.id)}}">{% trans %}Candidate{% endtrans %}</a>
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
<a href="{{url('election:create_list')}}">{% trans %}Add a new list{% endtrans %}</a>
|
<a href="{{url('election:create_list', election_id=object.id)}}">{% trans %}Add a new list{% endtrans %}</a>
|
||||||
{%- if user.can_edit(election) %}
|
{%- if user.can_edit(election) %}
|
||||||
<a href="{{url('election:create_role', election_id=object.id)}}">{% trans %}Add a new role{% endtrans %}</a>
|
<a href="{{url('election:create_role', election_id=object.id)}}">{% trans %}Add a new role{% endtrans %}</a>
|
||||||
|
<a href="{{url('election:update', election_id=object.id)}}">{% trans %}Edit{% endtrans %}</a>
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
</section>
|
</section>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -5,7 +5,8 @@ from election.views import *
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', ElectionsListView.as_view(), name='list'),
|
url(r'^$', ElectionsListView.as_view(), name='list'),
|
||||||
url(r'^add$', ElectionCreateView.as_view(), name='create'),
|
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]+)/edit$', ElectionUpdateView.as_view(), name='update'),
|
||||||
|
url(r'^(?P<election_id>[0-9]+)/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]+)/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]+)/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'),
|
||||||
|
@ -101,6 +101,36 @@ class RoleForm(forms.ModelForm):
|
|||||||
raise forms.ValidationError(_("This role already exists for this election"), code='invalid')
|
raise forms.ValidationError(_("This role already exists for this election"), code='invalid')
|
||||||
|
|
||||||
|
|
||||||
|
class ElectionListForm(forms.ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = ElectionList
|
||||||
|
fields = ('title','election')
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
election_id = kwargs.pop('election_id', None)
|
||||||
|
super(ElectionListForm, self).__init__(*args, **kwargs)
|
||||||
|
if election_id:
|
||||||
|
self.fields['election'].queryset = Election.objects.filter(id=election_id).all()
|
||||||
|
|
||||||
|
|
||||||
|
class ElectionForm(forms.ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = Election
|
||||||
|
fields = ['title', 'description', 'start_candidature', 'end_candidature', 'start_date', 'end_date',
|
||||||
|
'edit_groups', 'view_groups', 'vote_groups', 'candidature_groups']
|
||||||
|
widgets = {
|
||||||
|
'edit_groups': CheckboxSelectMultiple,
|
||||||
|
'view_groups': CheckboxSelectMultiple,
|
||||||
|
'edit_groups': CheckboxSelectMultiple,
|
||||||
|
'vote_groups': CheckboxSelectMultiple,
|
||||||
|
'candidature_groups': CheckboxSelectMultiple,
|
||||||
|
'start_date': SelectDateTime,
|
||||||
|
'end_date': SelectDateTime,
|
||||||
|
'start_candidature': SelectDateTime,
|
||||||
|
'end_candidature': SelectDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# Display elections
|
# Display elections
|
||||||
|
|
||||||
|
|
||||||
@ -111,12 +141,6 @@ class ElectionsListView(CanViewMixin, ListView):
|
|||||||
model = Election
|
model = Election
|
||||||
template_name = 'election/election_list.jinja'
|
template_name = 'election/election_list.jinja'
|
||||||
|
|
||||||
def get_queryset(self):
|
|
||||||
qs = super(ElectionsListView, self).get_queryset()
|
|
||||||
today = timezone.now()
|
|
||||||
qs = qs.filter(end_date__gte=today, start_date__lte=today)
|
|
||||||
return qs
|
|
||||||
|
|
||||||
|
|
||||||
class ElectionDetailView(CanViewMixin, DetailView):
|
class ElectionDetailView(CanViewMixin, DetailView):
|
||||||
"""
|
"""
|
||||||
@ -240,29 +264,19 @@ class CandidatureCreateView(CanCreateMixin, CreateView):
|
|||||||
|
|
||||||
class ElectionCreateView(CanCreateMixin, CreateView):
|
class ElectionCreateView(CanCreateMixin, CreateView):
|
||||||
model = Election
|
model = Election
|
||||||
form_class = modelform_factory(Election,
|
form_class = ElectionForm
|
||||||
fields=['title', 'description', 'start_candidature', 'end_candidature', 'start_date', 'end_date',
|
|
||||||
'edit_groups', 'view_groups', 'vote_groups', 'candidature_groups'],
|
|
||||||
widgets={
|
|
||||||
'edit_groups': CheckboxSelectMultiple,
|
|
||||||
'view_groups': CheckboxSelectMultiple,
|
|
||||||
'edit_groups': CheckboxSelectMultiple,
|
|
||||||
'vote_groups': CheckboxSelectMultiple,
|
|
||||||
'candidature_groups': CheckboxSelectMultiple,
|
|
||||||
'start_date': SelectDateTime,
|
|
||||||
'end_date': SelectDateTime,
|
|
||||||
'start_candidature': SelectDateTime,
|
|
||||||
'end_candidature': SelectDateTime,
|
|
||||||
})
|
|
||||||
template_name = 'core/page_prop.jinja'
|
template_name = 'core/page_prop.jinja'
|
||||||
|
|
||||||
|
def dispatch(self, request, *args, **kwargs):
|
||||||
|
if not request.user.is_subscribed():
|
||||||
|
raise PermissionDenied
|
||||||
|
return super(ElectionCreateView, self).dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
"""
|
"""
|
||||||
Verify that the user is suscribed
|
Allow every users that had passed the dispatch to create an election
|
||||||
"""
|
"""
|
||||||
res = super(CreateView, self).form_valid(form)
|
return super(CreateView, self).form_valid(form)
|
||||||
if self.request.user.is_subscribed():
|
|
||||||
return res
|
|
||||||
|
|
||||||
def get_success_url(self, **kwargs):
|
def get_success_url(self, **kwargs):
|
||||||
return reverse_lazy('election:detail', kwargs={'election_id': self.object.id})
|
return reverse_lazy('election:detail', kwargs={'election_id': self.object.id})
|
||||||
@ -275,6 +289,8 @@ class RoleCreateView(CanCreateMixin, CreateView):
|
|||||||
|
|
||||||
def dispatch(self, request, *arg, **kwargs):
|
def dispatch(self, request, *arg, **kwargs):
|
||||||
self.election = get_object_or_404(Election, pk=kwargs['election_id'])
|
self.election = get_object_or_404(Election, pk=kwargs['election_id'])
|
||||||
|
if self.election.is_vote_active or self.election.is_vote_finished:
|
||||||
|
raise PermissionDenied
|
||||||
return super(RoleCreateView, self).dispatch(request, *arg, **kwargs)
|
return super(RoleCreateView, self).dispatch(request, *arg, **kwargs)
|
||||||
|
|
||||||
def get_initial(self):
|
def get_initial(self):
|
||||||
@ -304,10 +320,25 @@ class RoleCreateView(CanCreateMixin, CreateView):
|
|||||||
|
|
||||||
class ElectionListCreateView(CanCreateMixin, CreateView):
|
class ElectionListCreateView(CanCreateMixin, CreateView):
|
||||||
model = ElectionList
|
model = ElectionList
|
||||||
form_class = modelform_factory(ElectionList,
|
form_class = ElectionListForm
|
||||||
fields=['title', 'election'])
|
|
||||||
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'])
|
||||||
|
if self.election.is_vote_finished:
|
||||||
|
raise PermissionDenied
|
||||||
|
return super(ElectionListCreateView, self).dispatch(request, *arg, **kwargs)
|
||||||
|
|
||||||
|
def get_initial(self):
|
||||||
|
init = {}
|
||||||
|
init['election'] = self.election
|
||||||
|
return init
|
||||||
|
|
||||||
|
def get_form_kwargs(self):
|
||||||
|
kwargs = super(ElectionListCreateView, self).get_form_kwargs()
|
||||||
|
kwargs['election_id'] = self.election.id
|
||||||
|
return kwargs
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
"""
|
"""
|
||||||
Verify that the user can vote on this election
|
Verify that the user can vote on this election
|
||||||
@ -324,3 +355,13 @@ class ElectionListCreateView(CanCreateMixin, CreateView):
|
|||||||
|
|
||||||
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})
|
||||||
|
|
||||||
|
# Update view
|
||||||
|
|
||||||
|
|
||||||
|
class ElectionUpdateView(CanEditMixin, UpdateView):
|
||||||
|
model = Election
|
||||||
|
form_class = ElectionForm
|
||||||
|
template_name = 'core/page_prop.jinja'
|
||||||
|
pk_url_kwarg = 'election_id'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user