2016-12-21 23:15:16 +00:00
|
|
|
from django.shortcuts import redirect, get_object_or_404
|
2016-12-05 19:18:03 +00:00
|
|
|
from django.views.generic import ListView, DetailView, RedirectView
|
|
|
|
from django.views.generic.edit import UpdateView, CreateView, DeleteView, FormView
|
|
|
|
from django.core.urlresolvers import reverse_lazy, reverse
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-12-20 15:00:14 +00:00
|
|
|
from django.forms.models import modelform_factory
|
2016-12-20 20:03:52 +00:00
|
|
|
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist, ImproperlyConfigured
|
2016-12-20 15:00:14 +00:00
|
|
|
from django.forms import CheckboxSelectMultiple
|
2016-12-19 02:54:57 +00:00
|
|
|
from django.utils import timezone
|
2016-12-05 19:18:03 +00:00
|
|
|
from django.conf import settings
|
2016-12-20 15:00:14 +00:00
|
|
|
from django import forms
|
2016-12-05 19:18:03 +00:00
|
|
|
|
|
|
|
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin
|
2016-12-21 16:33:16 +00:00
|
|
|
from django.views.generic.edit import FormMixin
|
2016-12-20 15:00:14 +00:00
|
|
|
from core.views.forms import SelectDateTime
|
2016-12-20 20:03:52 +00:00
|
|
|
from election.models import Election, Role, Candidature, ElectionList
|
2016-12-05 19:18:03 +00:00
|
|
|
|
2016-12-20 15:00:14 +00:00
|
|
|
from ajax_select.fields import AutoCompleteSelectField
|
|
|
|
|
2016-12-20 20:03:52 +00:00
|
|
|
|
2016-12-21 23:15:16 +00:00
|
|
|
# Custom form field
|
|
|
|
|
|
|
|
class VoteCheckbox(forms.ModelMultipleChoiceField):
|
|
|
|
"""
|
|
|
|
Used to replace ModelMultipleChoiceField but with
|
|
|
|
automatic backend verification
|
|
|
|
"""
|
|
|
|
def __init__(self, queryset, max_choice, required=True, widget=None, label=None,
|
|
|
|
initial=None, help_text='', *args, **kwargs):
|
|
|
|
self.max_choice = max_choice
|
|
|
|
widget = forms.CheckboxSelectMultiple
|
|
|
|
super(VoteCheckbox, self).__init__(queryset, None, required, widget, label,
|
|
|
|
initial, help_text, *args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
qs = super(VoteCheckbox, self).clean(value)
|
|
|
|
self.validate(qs)
|
|
|
|
|
|
|
|
def validate(self, qs):
|
|
|
|
if qs.count() > self.max_choice:
|
|
|
|
raise forms.ValidationError(_("You have selected too much candidate"))
|
|
|
|
|
|
|
|
|
2016-12-20 15:00:14 +00:00
|
|
|
# Forms
|
|
|
|
|
|
|
|
|
|
|
|
class CandidateForm(forms.Form):
|
2016-12-20 20:03:52 +00:00
|
|
|
""" 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))
|
2016-12-20 15:00:14 +00:00
|
|
|
|
2016-12-21 16:33:16 +00:00
|
|
|
|
|
|
|
class VoteForm(forms.Form):
|
2016-12-21 23:15:16 +00:00
|
|
|
def __init__(self, election, user, *args, **kwargs):
|
2016-12-21 16:33:16 +00:00
|
|
|
super(VoteForm, self).__init__(*args, **kwargs)
|
2016-12-21 23:15:16 +00:00
|
|
|
for role in election.role.all():
|
|
|
|
if role.user_has_voted(user):
|
|
|
|
cand = role.candidature
|
|
|
|
if role.max_choice > 1:
|
|
|
|
self.fields[role.title] = VoteCheckbox(cand, role.max_choice, required=False)
|
|
|
|
else:
|
|
|
|
self.fields[role.title] = forms.ModelChoiceField(cand, required=False,
|
|
|
|
widget=forms.RadioSelect(), empty_label=_("Blank vote"))
|
|
|
|
|
2016-12-21 16:33:16 +00:00
|
|
|
|
2016-12-05 19:18:03 +00:00
|
|
|
# Display elections
|
|
|
|
|
|
|
|
|
|
|
|
class ElectionsListView(CanViewMixin, ListView):
|
|
|
|
"""
|
|
|
|
A list with all responsabilities and their candidates
|
|
|
|
"""
|
|
|
|
model = Election
|
|
|
|
template_name = 'election/election_list.jinja'
|
2016-12-05 22:49:13 +00:00
|
|
|
|
2016-12-19 02:54:57 +00:00
|
|
|
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
|
|
|
|
|
2016-12-05 22:49:13 +00:00
|
|
|
|
|
|
|
class ElectionDetailView(CanViewMixin, DetailView):
|
|
|
|
"""
|
|
|
|
Details an election responsability by responsability
|
|
|
|
"""
|
|
|
|
model = Election
|
|
|
|
template_name = 'election/election_detail.jinja'
|
|
|
|
pk_url_kwarg = "election_id"
|
|
|
|
|
2016-12-20 20:03:52 +00:00
|
|
|
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
|
|
|
|
|
2016-12-20 22:08:47 +00:00
|
|
|
|
|
|
|
# Form view
|
|
|
|
|
|
|
|
class CandidatureCreateView(CanCreateMixin, FormView):
|
|
|
|
"""
|
|
|
|
View dedicated to a cundidature creation
|
|
|
|
"""
|
|
|
|
form_class = CandidateForm
|
|
|
|
template_name = 'core/page_prop.jinja'
|
|
|
|
|
|
|
|
def dispatch(self, request, *arg, **kwargs):
|
|
|
|
self.election_id = kwargs['election_id']
|
|
|
|
return super(CandidatureCreateView, self).dispatch(request, *arg, **kwargs)
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(CandidatureCreateView, self).get_form_kwargs()
|
|
|
|
kwargs['election_id'] = self.election_id
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def create_candidature(self, data):
|
|
|
|
cand = Candidature(
|
|
|
|
role=data['role'],
|
|
|
|
user=data['user'],
|
|
|
|
election_list=data['election_list'],
|
|
|
|
program=data['program']
|
|
|
|
)
|
|
|
|
cand.save()
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
"""
|
|
|
|
Verify that the selected user is in candidate group
|
|
|
|
"""
|
|
|
|
data = form.clean()
|
|
|
|
res = super(FormView, self).form_valid(form)
|
|
|
|
data['election'] = Election.objects.get(id=self.election_id)
|
|
|
|
if data['user'].is_root:
|
|
|
|
self.create_candidature(data)
|
|
|
|
return res
|
|
|
|
for grp in data['election'].candidature_groups.all():
|
|
|
|
if data['user'].is_in_group(grp):
|
|
|
|
self.create_candidature(data)
|
|
|
|
return res
|
|
|
|
return res
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
return reverse_lazy('election:detail', kwargs={'election_id': self.election_id})
|
|
|
|
|
|
|
|
|
2016-12-21 16:33:16 +00:00
|
|
|
class VoteFormView(CanCreateMixin, FormView):
|
|
|
|
"""
|
|
|
|
Alows users to vote
|
|
|
|
"""
|
|
|
|
form_class = VoteForm
|
2016-12-21 23:32:14 +00:00
|
|
|
template_name = 'election/vote_form.jinja'
|
2016-12-21 16:33:16 +00:00
|
|
|
|
|
|
|
def dispatch(self, request, *arg, **kwargs):
|
|
|
|
self.election_id = kwargs['election_id']
|
2016-12-21 23:15:16 +00:00
|
|
|
self.election = get_object_or_404(Election, pk=self.election_id)
|
2016-12-21 16:33:16 +00:00
|
|
|
return super(VoteFormView, self).dispatch(request, *arg, **kwargs)
|
|
|
|
|
|
|
|
def vote(self, data):
|
2016-12-21 23:15:16 +00:00
|
|
|
print(data)
|
2016-12-21 16:33:16 +00:00
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(VoteFormView, self).get_form_kwargs()
|
2016-12-21 23:15:16 +00:00
|
|
|
kwargs['election'] = self.election
|
|
|
|
kwargs['user'] = self.request.user
|
2016-12-21 16:33:16 +00:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
"""
|
|
|
|
Verify that the user is part in a vote group
|
|
|
|
"""
|
|
|
|
data = form.clean()
|
|
|
|
res = super(FormView, self).form_valid(form)
|
|
|
|
if self.request.user.is_root:
|
|
|
|
self.vote(data)
|
|
|
|
return res
|
|
|
|
for grp in data['role'].election.vote_groups.all():
|
|
|
|
if self.request.user.is_in_group(grp):
|
|
|
|
self.vote(data)
|
|
|
|
return res
|
|
|
|
return res
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
return reverse_lazy('election:detail', kwargs={'election_id': self.election_id})
|
|
|
|
|
2016-12-20 20:03:52 +00:00
|
|
|
# Create views
|
2016-12-20 15:00:14 +00:00
|
|
|
|
2016-12-20 22:08:47 +00:00
|
|
|
|
2016-12-20 20:03:52 +00:00
|
|
|
class ElectionCreateView(CanCreateMixin, CreateView):
|
2016-12-20 15:00:14 +00:00
|
|
|
model = Election
|
|
|
|
form_class = modelform_factory(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,
|
|
|
|
})
|
|
|
|
template_name = 'core/page_prop.jinja'
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
return reverse_lazy('election:detail', kwargs={'election_id': self.object.id})
|
2016-12-20 20:03:52 +00:00
|
|
|
|
|
|
|
|
2016-12-20 22:08:47 +00:00
|
|
|
class RoleCreateView(CanCreateMixin, CreateView):
|
|
|
|
model = Role
|
|
|
|
form_class = modelform_factory(Role,
|
|
|
|
fields=['title', 'election', 'title', 'description', 'max_choice'])
|
|
|
|
template_name = 'core/page_prop.jinja'
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
"""
|
|
|
|
Verify that the user can edit proprely
|
|
|
|
"""
|
|
|
|
obj = form.instance
|
|
|
|
res = super(CreateView, self).form_valid
|
|
|
|
if self.request.user.is_root:
|
|
|
|
return res(form)
|
|
|
|
if obj.election:
|
|
|
|
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})
|
|
|
|
|
|
|
|
|
2016-12-20 20:03:52 +00:00
|
|
|
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:
|
2016-12-20 22:08:47 +00:00
|
|
|
if self.request.user.is_root:
|
|
|
|
return res(form)
|
2016-12-20 20:03:52 +00:00
|
|
|
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})
|