Sith/election/views.py

479 lines
17 KiB
Python
Raw Normal View History

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 _
from django.forms.models import modelform_factory
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist, ImproperlyConfigured
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
from django import forms
2016-12-05 19:18:03 +00:00
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin
2016-12-22 16:39:32 +00:00
from django.db.models.query import QuerySet
from django.views.generic.edit import FormMixin
from core.views.forms import SelectDateTime
2016-12-22 16:39:32 +00:00
from election.models import Election, Role, Candidature, ElectionList, Vote
2016-12-05 19:18:03 +00:00
from ajax_select.fields import AutoCompleteSelectField
2016-12-21 23:15:16 +00:00
# Custom form field
2016-12-24 01:16:21 +00:00
class LimitedCheckboxField(forms.ModelMultipleChoiceField):
2016-12-21 23:15:16 +00:00
"""
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
2016-12-22 16:39:32 +00:00
widget = forms.CheckboxSelectMultiple()
2016-12-24 01:16:21 +00:00
super(LimitedCheckboxField, self).__init__(queryset, None, required, widget, label,
2016-12-24 02:06:39 +00:00
initial, help_text, *args, **kwargs)
2016-12-21 23:15:16 +00:00
def clean(self, value):
2016-12-24 01:16:21 +00:00
qs = super(LimitedCheckboxField, self).clean(value)
2016-12-21 23:15:16 +00:00
self.validate(qs)
2016-12-22 16:39:32 +00:00
return qs
2016-12-21 23:15:16 +00:00
def validate(self, qs):
if qs.count() > self.max_choice:
raise forms.ValidationError(_("You have selected too much candidates."), code='invalid')
2016-12-21 23:15:16 +00:00
# Forms
2016-12-24 01:16:21 +00:00
class CandidateForm(forms.ModelForm):
""" Form to candidate """
2016-12-24 01:16:21 +00:00
class Meta:
model = Candidature
fields = ['user', 'role', 'program', 'election_list']
widgets = {
'program': forms.Textarea
}
user = AutoCompleteSelectField('users', label=_('User to candidate'), help_text=None, required=True)
2016-12-24 01:16:21 +00:00
def __init__(self, *args, **kwargs):
election_id = kwargs.pop('election_id', None)
can_edit = kwargs.pop('can_edit', False)
super(CandidateForm, self).__init__(*args, **kwargs)
2016-12-24 01:16:21 +00:00
if election_id:
self.fields['role'].queryset = Role.objects.filter(election__id=election_id).all()
self.fields['election_list'].queryset = ElectionList.objects.filter(election__id=election_id).all()
if not can_edit:
self.fields['user'].widget = forms.HiddenInput()
class VoteForm(forms.Form):
2016-12-21 23:15:16 +00:00
def __init__(self, election, user, *args, **kwargs):
super(VoteForm, self).__init__(*args, **kwargs)
2016-12-23 22:49:00 +00:00
if not election.has_voted(user):
for role in election.roles.all():
2016-12-23 21:58:54 +00:00
cand = role.candidatures
2016-12-21 23:15:16 +00:00
if role.max_choice > 1:
2016-12-24 01:16:21 +00:00
self.fields[role.title] = LimitedCheckboxField(cand, role.max_choice, required=False)
2016-12-21 23:15:16 +00:00
else:
self.fields[role.title] = forms.ModelChoiceField(cand, required=False,
2016-12-23 16:13:46 +00:00
widget=forms.RadioSelect(), empty_label=_("Blank vote"))
class RoleForm(forms.ModelForm):
""" Form for creating a role """
class Meta:
model = Role
fields = ['title', 'election', 'description', 'max_choice']
2016-12-24 02:06:39 +00:00
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()
2016-12-23 16:13:46 +00:00
def clean(self):
cleaned_data = super(RoleForm, self).clean()
title = cleaned_data.get('title')
election = cleaned_data.get('election')
if Role.objects.filter(title=title, election=election).exists():
raise forms.ValidationError(_("This role already exists for this election"), code='invalid')
2016-12-21 23:15:16 +00:00
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',
2016-12-25 22:36:34 +00:00
'edit_groups', 'view_groups', 'vote_groups', 'candidature_groups']
widgets = {
'edit_groups': CheckboxSelectMultiple,
'view_groups': CheckboxSelectMultiple,
'edit_groups': CheckboxSelectMultiple,
'vote_groups': CheckboxSelectMultiple,
2016-12-25 22:36:34 +00:00
'candidature_groups': CheckboxSelectMultiple
}
2016-12-25 22:36:34 +00:00
start_date = forms.DateTimeField(['%Y-%m-%d %H:%M:%S'], label=_("Start date"), widget=SelectDateTime, required=True)
end_date = forms.DateTimeField(['%Y-%m-%d %H:%M:%S'], label=_("End date"), widget=SelectDateTime, required=True)
start_candidature = forms.DateTimeField(['%Y-%m-%d %H:%M:%S'], label=_("Start candidature"), widget=SelectDateTime, required=True)
end_candidature = forms.DateTimeField(['%Y-%m-%d %H:%M:%S'], label=_("End candidature"), widget=SelectDateTime, required=True)
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
class ElectionDetailView(CanViewMixin, DetailView):
"""
Details an election responsability by responsability
"""
model = Election
template_name = 'election/election_detail.jinja'
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)
2016-12-23 22:49:00 +00:00
kwargs['election_form'] = VoteForm(self.object, self.request.user)
kwargs['election_results'] = self.object.results
return kwargs
2016-12-20 22:08:47 +00:00
# Form view
class VoteFormView(CanCreateMixin, FormView):
"""
Alows users to vote
"""
form_class = VoteForm
2016-12-22 00:28:51 +00:00
template_name = 'election/election_detail.jinja'
def dispatch(self, request, *arg, **kwargs):
2016-12-22 00:28:51 +00:00
self.election = get_object_or_404(Election, pk=kwargs['election_id'])
return super(VoteFormView, self).dispatch(request, *arg, **kwargs)
def vote(self, election_data):
for role_title in election_data.keys():
# If we have a multiple choice field
if isinstance(election_data[role_title], QuerySet):
if election_data[role_title].count() > 0:
vote = Vote(role=election_data[role_title].first().role)
2016-12-22 16:39:32 +00:00
vote.save()
for el in election_data[role_title]:
2016-12-22 16:39:32 +00:00
vote.candidature.add(el)
# If we have a single choice
elif election_data[role_title] is not None:
vote = Vote(role=election_data[role_title].role)
2016-12-22 16:39:32 +00:00
vote.save()
vote.candidature.add(election_data[role_title])
2016-12-23 22:49:00 +00:00
self.election.voters.add(self.request.user)
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
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)
2016-12-22 00:28:51 +00:00
for grp in self.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):
2016-12-22 00:28:51 +00:00
return reverse_lazy('election:detail', kwargs={'election_id': self.election.id})
def get_context_data(self, **kwargs):
""" Add additionnal data to the template """
kwargs = super(VoteFormView, self).get_context_data(**kwargs)
kwargs['object'] = self.election
kwargs['election'] = self.election
kwargs['election_form'] = self.get_form()
return kwargs
# Create views
2016-12-24 01:16:21 +00:00
class CandidatureCreateView(CanCreateMixin, CreateView):
"""
View dedicated to a cundidature creation
"""
form_class = CandidateForm
model = Candidature
template_name = 'election/candidate_form.jinja'
def dispatch(self, request, *arg, **kwargs):
self.election = get_object_or_404(Election, pk=kwargs['election_id'])
return super(CandidatureCreateView, self).dispatch(request, *arg, **kwargs)
def get_initial(self):
init = {}
self.can_edit = self.request.user.can_edit(self.election)
init['user'] = self.request.user.id
return init
def get_form_kwargs(self):
kwargs = super(CandidatureCreateView, self).get_form_kwargs()
kwargs['election_id'] = self.election.id
kwargs['can_edit'] = self.can_edit
return kwargs
def form_valid(self, form):
"""
Verify that the selected user is in candidate group
"""
obj = form.instance
obj.election = Election.objects.get(id=self.election.id)
if(obj.election.can_candidate(obj.user)) and (obj.user == self.request.user or self.can_edit):
return super(CreateView, self).form_valid(form)
raise PermissionDenied
def get_context_data(self, **kwargs):
kwargs = super(CandidatureCreateView, self).get_context_data(**kwargs)
kwargs['election'] = self.election
return kwargs
def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.election.id})
2016-12-20 22:08:47 +00:00
class ElectionCreateView(CanCreateMixin, CreateView):
model = Election
form_class = ElectionForm
template_name = 'core/create.jinja'
def dispatch(self, request, *args, **kwargs):
if not request.user.is_subscribed():
raise PermissionDenied
return super(ElectionCreateView, self).dispatch(request, *args, **kwargs)
2016-12-24 01:16:21 +00:00
def form_valid(self, form):
"""
Allow every users that had passed the dispatch to create an election
2016-12-24 01:16:21 +00:00
"""
return super(CreateView, self).form_valid(form)
2016-12-24 01:16:21 +00:00
def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.object.id})
2016-12-20 22:08:47 +00:00
class RoleCreateView(CanCreateMixin, CreateView):
model = Role
2016-12-23 16:13:46 +00:00
form_class = RoleForm
template_name = 'core/create.jinja'
2016-12-20 22:08:47 +00:00
2016-12-24 02:06:39 +00:00
def dispatch(self, request, *arg, **kwargs):
self.election = get_object_or_404(Election, pk=kwargs['election_id'])
2016-12-25 21:04:31 +00:00
if not self.election.is_vote_editable:
raise PermissionDenied
2016-12-24 02:06:39 +00:00
return super(RoleCreateView, self).dispatch(request, *arg, **kwargs)
def get_initial(self):
init = {}
init['election'] = self.election
return init
2016-12-20 22:08:47 +00:00
def form_valid(self, form):
"""
Verify that the user can edit proprely
"""
obj = form.instance
if obj.election:
for grp in obj.election.edit_groups.all():
if self.request.user.is_in_group(grp):
2016-12-24 01:16:21 +00:00
return super(CreateView, self).form_valid(form)
2016-12-20 22:08:47 +00:00
raise PermissionDenied
2016-12-24 02:06:39 +00:00
def get_form_kwargs(self):
kwargs = super(RoleCreateView, self).get_form_kwargs()
kwargs['election_id'] = self.election.id
return kwargs
2016-12-20 22:08:47 +00:00
def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.object.election.id})
class ElectionListCreateView(CanCreateMixin, CreateView):
model = ElectionList
form_class = ElectionListForm
template_name = 'core/create.jinja'
def dispatch(self, request, *arg, **kwargs):
self.election = get_object_or_404(Election, pk=kwargs['election_id'])
2016-12-24 17:29:26 +00:00
if not self.election.is_vote_editable:
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):
"""
Verify that the user can vote on this election
"""
obj = form.instance
if obj.election:
for grp in obj.election.candidature_groups.all():
if self.request.user.is_in_group(grp):
2016-12-24 01:16:21 +00:00
return super(CreateView, self).form_valid(form)
for grp in obj.election.edit_groups.all():
if self.request.user.is_in_group(grp):
2016-12-24 01:16:21 +00:00
return super(CreateView, self).form_valid(form)
raise PermissionDenied
def get_success_url(self, **kwargs):
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/edit.jinja'
pk_url_kwarg = 'election_id'
2016-12-24 17:29:26 +00:00
def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.object.id})
class CandidatureUpdateView(CanEditMixin, UpdateView):
model = Candidature
form_class = CandidateForm
template_name = 'core/edit.jinja'
2016-12-24 17:29:26 +00:00
pk_url_kwarg = 'candidature_id'
def dispatch(self, request, *arg, **kwargs):
self.object = self.get_object()
if not self.object.role.election.is_vote_editable:
raise PermissionDenied
return super(CandidatureUpdateView, self).dispatch(request, *arg, **kwargs)
def remove_fields(self):
self.form.fields.pop('role', None)
def get(self, request, *args, **kwargs):
self.form = self.get_form()
self.remove_fields()
return self.render_to_response(self.get_context_data(form=self.form))
def post(self, request, *args, **kwargs):
self.form = self.get_form()
self.remove_fields()
if request.user.is_authenticated() and request.user.can_edit(self.object) and self.form.is_valid():
return super(CandidatureUpdateView, self).form_valid(self.form)
return self.form_invalid(self.form)
def get_form_kwargs(self):
kwargs = super(CandidatureUpdateView, self).get_form_kwargs()
kwargs['election_id'] = self.object.role.election.id
return kwargs
def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.object.role.election.id})
class RoleUpdateView(CanEditMixin, UpdateView):
model = Role
form_class = RoleForm
template_name = 'core/edit.jinja'
2016-12-24 17:29:26 +00:00
pk_url_kwarg = 'role_id'
def dispatch(self, request, *arg, **kwargs):
self.object = self.get_object()
if not self.object.election.is_vote_editable:
raise PermissionDenied
return super(RoleUpdateView, self).dispatch(request, *arg, **kwargs)
def remove_fields(self):
self.form.fields.pop('election', None)
def get(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = self.get_form()
self.remove_fields()
return self.render_to_response(self.get_context_data(form=self.form))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = self.get_form()
self.remove_fields()
if request.user.is_authenticated() and request.user.can_edit(self.object) and self.form.is_valid():
return super(RoleUpdateView, self).form_valid(self.form)
return self.form_invalid(self.form)
def get_form_kwargs(self):
kwargs = super(RoleUpdateView, self).get_form_kwargs()
kwargs['election_id'] = self.object.election.id
return kwargs
def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.object.election.id})
# Delete Views
class CandidatureDeleteView(CanEditMixin, DeleteView):
model = Candidature
template_name = 'core/delete_confirm.jinja'
2016-12-24 17:29:26 +00:00
pk_url_kwarg = 'candidature_id'
def dispatch(self, request, *arg, **kwargs):
self.object = self.get_object()
self.election = self.object.role.election
if not self.election.can_candidate:
raise PermissionDenied
return super(CandidatureDeleteView, self).dispatch(request, *arg, **kwargs)
def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.election.id})
class RoleDeleteView(CanEditMixin, DeleteView):
model = Role
template_name = 'core/delete_confirm.jinja'
2016-12-24 17:29:26 +00:00
pk_url_kwarg = 'role_id'
def dispatch(self, request, *arg, **kwargs):
self.object = self.get_object()
self.election = self.object.election
if not self.election.is_vote_editable:
raise PermissionDenied
return super(RoleDeleteView, self).dispatch(request, *arg, **kwargs)
def get_success_url(self, **kwargs):
return reverse_lazy('election:detail', kwargs={'election_id': self.election.id})