Can add ElectionList and start of candidature form

This commit is contained in:
Antoine Bartuccio 2016-12-20 21:03:52 +01:00
parent d72d8366cf
commit 51bb6c8472
4 changed files with 51 additions and 5 deletions

View File

@ -67,6 +67,9 @@ class ElectionList(models.Model):
title = models.CharField(_('title'), max_length=255) title = models.CharField(_('title'), max_length=255)
election = models.ForeignKey(Election, related_name='election_list', verbose_name=_("election")) election = models.ForeignKey(Election, related_name='election_list', verbose_name=_("election"))
def __str__(self):
return self.title
class Candidature(models.Model): class Candidature(models.Model):
""" """

View File

@ -54,6 +54,7 @@
{% endfor %} {% endfor %}
</table> </table>
{% endif %} {% endif %}
<form>{{candidate_form}}</form>
{% if object.is_candidature_active -%} {% if object.is_candidature_active -%}
candidature candidature
{% endif -%} {% endif -%}

View File

@ -1,9 +1,10 @@
from django.conf.urls import url, include from django.conf.urls import url
from election.views import * from election.views import *
urlpatterns = [ urlpatterns = [
url(r'^$', ElectionsListView.as_view(), name='list'), url(r'^$', ElectionsListView.as_view(), name='list'),
url(r'^create$', PageCreateView.as_view(), name='create'), url(r'^create$', ElectionCreateView.as_view(), name='create'),
url(r'^list/create$', ElectionListCreateView.as_view(), name='create_list'),
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'),
] ]

View File

@ -4,6 +4,7 @@ from django.views.generic.edit import UpdateView, CreateView, DeleteView, FormVi
from django.core.urlresolvers import reverse_lazy, reverse from django.core.urlresolvers import reverse_lazy, reverse
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.forms.models import modelform_factory from django.forms.models import modelform_factory
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist, ImproperlyConfigured
from django.forms import CheckboxSelectMultiple from django.forms import CheckboxSelectMultiple
from django.utils import timezone from django.utils import timezone
from django.conf import settings from django.conf import settings
@ -12,15 +13,23 @@ from django import forms
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin
from core.views.forms import SelectDateTime from core.views.forms import SelectDateTime
from core.widgets import ChoiceWithOtherField from core.widgets import ChoiceWithOtherField
from election.models import Election, Role, Candidature from election.models import Election, Role, Candidature, ElectionList
from ajax_select.fields import AutoCompleteSelectField from ajax_select.fields import AutoCompleteSelectField
# Forms # Forms
class CandidateForm(forms.Form): class CandidateForm(forms.Form):
user = AutoCompleteSelectField('users', label=_('Refound this account'), help_text=None, required=True) """ Form to candidate """
user = AutoCompleteSelectField('users', label=_('User to candidate'), help_text=None, required=True)
program = forms.CharField(widget=forms.Textarea)
def __init__(self, election_id, *args, **kwargs):
super(CandidateForm, self).__init__(*args, **kwargs)
self.fields['role'] = forms.ModelChoiceField(Role.objects.filter(election__id=election_id))
self.fields['election_list'] = forms.ModelChoiceField(ElectionList.objects.filter(election__id=election_id))
# Display elections # Display elections
@ -47,8 +56,15 @@ class ElectionDetailView(CanViewMixin, DetailView):
template_name = 'election/election_detail.jinja' template_name = 'election/election_detail.jinja'
pk_url_kwarg = "election_id" pk_url_kwarg = "election_id"
def get_context_data(self, **kwargs):
""" Add additionnal data to the template """
kwargs = super(ElectionDetailView, self).get_context_data(**kwargs)
kwargs['candidate_form'] = CandidateForm(self.get_object().id)
return kwargs
class PageCreateView(CanCreateMixin, CreateView): # Create views
class ElectionCreateView(CanCreateMixin, CreateView):
model = Election model = Election
form_class = modelform_factory(Election, form_class = modelform_factory(Election,
fields=['title', 'description', 'start_candidature', 'end_candidature', 'start_date', 'end_date', fields=['title', 'description', 'start_candidature', 'end_candidature', 'start_date', 'end_date',
@ -68,3 +84,28 @@ class PageCreateView(CanCreateMixin, CreateView):
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})
class ElectionListCreateView(CanCreateMixin, CreateView):
model = ElectionList
form_class = modelform_factory(ElectionList,
fields=['title', 'election'])
template_name = 'core/page_prop.jinja'
def form_valid(self, form):
"""
Verify that the user can vote on this election
"""
obj = form.instance
res = super(CreateView, self).form_valid
if obj.election:
for grp in obj.election.candidature_groups.all():
if self.request.user.is_in_group(grp):
return res(form)
for grp in obj.election.edit_groups.all():
if self.request.user.is_in_group(grp):
return res(form)
raise PermissionDenied
def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.object.election.id})