2012-07-06 18:00:43 +02:00
|
|
|
|
from django.contrib import messages
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from django.core.urlresolvers import reverse
|
2012-07-10 11:27:06 +02:00
|
|
|
|
from django.shortcuts import redirect
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
from django.utils.translation import ungettext
|
|
|
|
|
from reportlab.lib import colors
|
|
|
|
|
from reportlab.lib.units import cm
|
|
|
|
|
from reportlab.platypus import (PageBreak, Paragraph, SimpleDocTemplate, Spacer,
|
2014-11-28 20:51:24 +01:00
|
|
|
|
LongTable, Table, TableStyle)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from openslides.agenda.views import CreateRelatedAgendaItemView as _CreateRelatedAgendaItemView
|
|
|
|
|
from openslides.config.api import config
|
2014-10-11 14:34:49 +02:00
|
|
|
|
from openslides.users.models import Group, User # TODO: remove this
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from openslides.poll.views import PollFormView
|
2012-07-10 14:00:51 +02:00
|
|
|
|
from openslides.utils.pdf import stylesheet
|
2013-09-08 14:33:43 +02:00
|
|
|
|
from openslides.utils.utils import html_strong
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from openslides.utils.views import (CreateView, DeleteView, DetailView,
|
|
|
|
|
ListView, PDFView, PermissionMixin,
|
|
|
|
|
QuestionView, RedirectView,
|
|
|
|
|
SingleObjectMixin, UpdateView, View)
|
|
|
|
|
|
|
|
|
|
from .forms import AssignmentForm, AssignmentRunForm
|
|
|
|
|
from .models import Assignment, AssignmentPoll
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
2013-09-08 14:33:43 +02:00
|
|
|
|
class AssignmentListView(ListView):
|
|
|
|
|
"""ListView for all Assignments"""
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_see_assignment'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
model = Assignment
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
2013-09-08 14:33:43 +02:00
|
|
|
|
class AssignmentDetail(DetailView):
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_see_assignment'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
model = Assignment
|
|
|
|
|
form_class = AssignmentRunForm
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
|
context = super(AssignmentDetail, self).get_context_data(*args, **kwargs)
|
|
|
|
|
if self.request.method == 'POST':
|
|
|
|
|
context['form'] = self.form_class(self.request.POST)
|
|
|
|
|
else:
|
|
|
|
|
context['form'] = self.form_class()
|
2014-12-22 18:09:05 +01:00
|
|
|
|
polls = self.get_object().poll_set.all()
|
2013-09-08 14:33:43 +02:00
|
|
|
|
if not self.request.user.has_perm('assignment.can_manage_assignment'):
|
2014-12-22 18:09:05 +01:00
|
|
|
|
polls = self.get_object().poll_set.filter(published=True)
|
|
|
|
|
vote_results = self.get_object().vote_results(only_published=True)
|
2013-09-08 14:33:43 +02:00
|
|
|
|
else:
|
2014-12-22 18:09:05 +01:00
|
|
|
|
polls = self.get_object().poll_set.all()
|
|
|
|
|
vote_results = self.get_object().vote_results(only_published=False)
|
2013-09-08 14:33:43 +02:00
|
|
|
|
|
|
|
|
|
blocked_candidates = [
|
|
|
|
|
candidate.person for candidate in
|
2014-12-22 18:09:05 +01:00
|
|
|
|
self.get_object().assignment_candidates.filter(blocked=True)]
|
2013-09-08 14:33:43 +02:00
|
|
|
|
context['polls'] = polls
|
|
|
|
|
context['vote_results'] = vote_results
|
|
|
|
|
context['blocked_candidates'] = blocked_candidates
|
2014-12-22 18:09:05 +01:00
|
|
|
|
context['user_is_candidate'] = self.get_object().is_candidate(self.request.user)
|
2013-09-08 14:33:43 +02:00
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def post(self, *args, **kwargs):
|
|
|
|
|
if self.request.user.has_perm('assignment.can_nominate_other'):
|
|
|
|
|
form = self.form_class(self.request.POST)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
if form.is_valid():
|
|
|
|
|
user = form.cleaned_data['candidate']
|
|
|
|
|
try:
|
2014-12-22 18:09:05 +01:00
|
|
|
|
self.get_object().run(user, self.request.user)
|
2014-08-16 09:25:18 +02:00
|
|
|
|
except NameError as e:
|
2013-09-08 14:33:43 +02:00
|
|
|
|
messages.error(self.request, e)
|
2012-09-06 15:57:42 +02:00
|
|
|
|
else:
|
2013-09-08 14:33:43 +02:00
|
|
|
|
messages.success(self.request, _(
|
|
|
|
|
"Candidate %s was nominated successfully.")
|
|
|
|
|
% html_strong(user))
|
|
|
|
|
return super(AssignmentDetail, self).get(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentCreateView(CreateView):
|
|
|
|
|
model = Assignment
|
|
|
|
|
form_class = AssignmentForm
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_manage_assignment'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentUpdateView(UpdateView):
|
|
|
|
|
model = Assignment
|
|
|
|
|
form_class = AssignmentForm
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_manage_assignment'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentDeleteView(DeleteView):
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_manage_assignment'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
model = Assignment
|
|
|
|
|
success_url_name = 'assignment_list'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentSetStatusView(SingleObjectMixin, RedirectView):
|
|
|
|
|
model = Assignment
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_manage_assignment'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
url_name = 'assignment_detail'
|
|
|
|
|
|
|
|
|
|
def pre_redirect(self, *args, **kwargs):
|
|
|
|
|
status = kwargs.get('status')
|
2012-07-06 18:00:43 +02:00
|
|
|
|
if status is not None:
|
2013-09-08 14:33:43 +02:00
|
|
|
|
try:
|
2014-12-22 18:09:05 +01:00
|
|
|
|
self.get_object().set_status(status)
|
2014-08-16 09:25:18 +02:00
|
|
|
|
except ValueError as e:
|
2013-09-08 14:33:43 +02:00
|
|
|
|
messages.error(self.request, e)
|
|
|
|
|
else:
|
|
|
|
|
messages.success(
|
|
|
|
|
self.request,
|
|
|
|
|
_('Election status was set to: %s.') %
|
2014-12-22 18:09:05 +01:00
|
|
|
|
html_strong(self.get_object().get_status_display())
|
2013-09-08 14:33:43 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentRunView(SingleObjectMixin, PermissionMixin, View):
|
|
|
|
|
model = Assignment
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_nominate_self'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
|
|
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
|
assignment = self.get_object()
|
2012-09-13 15:07:24 +02:00
|
|
|
|
try:
|
2013-09-08 14:33:43 +02:00
|
|
|
|
assignment.run(self.request.user, self.request.user)
|
2014-08-16 09:25:18 +02:00
|
|
|
|
except NameError as e:
|
2013-09-08 14:33:43 +02:00
|
|
|
|
messages.error(self.request, e)
|
2012-08-04 15:02:02 +02:00
|
|
|
|
else:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
messages.success(
|
2013-09-08 14:33:43 +02:00
|
|
|
|
self.request, _('You have set your candidature successfully.'))
|
|
|
|
|
return redirect(reverse('assignment_detail', args=[assignment.pk]))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
2013-09-08 14:33:43 +02:00
|
|
|
|
class AssignmentRunDeleteView(SingleObjectMixin, RedirectView):
|
|
|
|
|
model = Assignment
|
|
|
|
|
url_name = 'assignment_detail'
|
|
|
|
|
|
|
|
|
|
def pre_redirect(self, *args, **kwargs):
|
2014-12-22 18:09:05 +01:00
|
|
|
|
if self.get_object().status == 'sea' or self.request.user.has_perm(
|
2013-09-08 14:33:43 +02:00
|
|
|
|
"assignment.can_manage_assignment"):
|
2013-02-05 21:31:45 +01:00
|
|
|
|
try:
|
2014-12-22 18:09:05 +01:00
|
|
|
|
self.get_object().delrun(self.request.user, blocked=True)
|
2014-08-16 09:25:18 +02:00
|
|
|
|
except Exception as e:
|
|
|
|
|
# TODO: only catch relevant exception
|
2013-09-08 14:33:43 +02:00
|
|
|
|
messages.error(self.request, e)
|
2012-09-13 21:51:50 +02:00
|
|
|
|
else:
|
2013-09-25 12:53:44 +02:00
|
|
|
|
messages.success(self.request, _(
|
|
|
|
|
'You have withdrawn your candidature successfully. '
|
|
|
|
|
'You can not be nominated by other participants anymore.'))
|
2013-09-08 14:33:43 +02:00
|
|
|
|
else:
|
|
|
|
|
messages.error(self.request, _('The candidate list is already closed.'))
|
|
|
|
|
|
|
|
|
|
|
2013-09-25 12:53:44 +02:00
|
|
|
|
class AssignmentRunOtherDeleteView(SingleObjectMixin, QuestionView):
|
2013-09-08 14:33:43 +02:00
|
|
|
|
model = Assignment
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_manage_assignment'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
|
2013-09-25 12:53:44 +02:00
|
|
|
|
def get_question_message(self):
|
|
|
|
|
self._get_person_information()
|
2013-09-08 14:33:43 +02:00
|
|
|
|
if not self.is_blocked:
|
2013-09-25 12:53:44 +02:00
|
|
|
|
question = _("Do you really want to withdraw %s from the election?") % html_strong(self.person)
|
2012-09-13 21:51:50 +02:00
|
|
|
|
else:
|
2013-09-25 12:53:44 +02:00
|
|
|
|
question = _("Do you really want to unblock %s for the election?") % html_strong(self.person)
|
|
|
|
|
return question
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2013-09-25 12:53:44 +02:00
|
|
|
|
def on_clicked_yes(self):
|
|
|
|
|
self._get_person_information()
|
2013-09-08 14:33:43 +02:00
|
|
|
|
try:
|
2014-12-22 18:09:05 +01:00
|
|
|
|
self.get_object().delrun(self.person, blocked=False)
|
2014-08-16 09:25:18 +02:00
|
|
|
|
except Exception as e:
|
|
|
|
|
# TODO: only catch relevant exception
|
2013-09-25 12:53:44 +02:00
|
|
|
|
self.error = e
|
|
|
|
|
else:
|
|
|
|
|
self.error = False
|
|
|
|
|
|
|
|
|
|
def create_final_message(self):
|
|
|
|
|
if self.error:
|
|
|
|
|
messages.error(self.request, self.error)
|
2013-09-08 14:33:43 +02:00
|
|
|
|
else:
|
2013-09-25 12:53:44 +02:00
|
|
|
|
messages.success(self.request, self.get_final_message())
|
2013-09-08 14:33:43 +02:00
|
|
|
|
|
2013-09-25 12:53:44 +02:00
|
|
|
|
def get_final_message(self):
|
|
|
|
|
message = _("Candidate %s was withdrawn successfully.") % html_strong(self.person)
|
2013-09-08 14:33:43 +02:00
|
|
|
|
if self.is_blocked:
|
2013-09-25 12:53:44 +02:00
|
|
|
|
message = _("%s was unblocked successfully.") % html_strong(self.person)
|
|
|
|
|
return message
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2013-09-25 12:53:44 +02:00
|
|
|
|
def _get_person_information(self):
|
2014-10-11 14:34:49 +02:00
|
|
|
|
self.person = User.objects.get(pk=self.kwargs.get('user_id'))
|
2014-12-22 18:09:05 +01:00
|
|
|
|
self.is_blocked = self.get_object().is_blocked(self.person)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
2013-09-08 14:33:43 +02:00
|
|
|
|
class PollCreateView(SingleObjectMixin, RedirectView):
|
|
|
|
|
model = Assignment
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_manage_assignment'
|
2014-01-11 17:07:47 +01:00
|
|
|
|
url_name = 'assignment_detail'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
|
|
|
|
|
def pre_redirect(self, *args, **kwargs):
|
2014-12-22 18:09:05 +01:00
|
|
|
|
self.get_object().gen_poll()
|
2013-09-08 14:33:43 +02:00
|
|
|
|
messages.success(self.request, _("New ballot was successfully created."))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PollUpdateView(PollFormView):
|
2012-07-06 18:00:43 +02:00
|
|
|
|
poll_class = AssignmentPoll
|
2013-10-17 11:34:54 +02:00
|
|
|
|
template_name = 'assignment/assignmentpoll_form.html'
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2013-09-08 14:33:43 +02:00
|
|
|
|
context = super(PollUpdateView, self).get_context_data(**kwargs)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
self.assignment = self.poll.get_assignment()
|
|
|
|
|
context['assignment'] = self.assignment
|
|
|
|
|
context['poll'] = self.poll
|
2014-01-11 17:07:47 +01:00
|
|
|
|
context['polls'] = self.assignment.poll_set.all()
|
2012-07-06 18:00:43 +02:00
|
|
|
|
context['ballotnumber'] = self.poll.get_ballot()
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2013-09-08 14:33:43 +02:00
|
|
|
|
return_url = ''
|
2014-03-27 20:38:13 +01:00
|
|
|
|
if 'apply' not in self.request.POST:
|
2013-09-08 14:33:43 +02:00
|
|
|
|
return_url = reverse('assignment_detail', args=[self.poll.assignment.id])
|
|
|
|
|
return return_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SetPublishStatusView(SingleObjectMixin, RedirectView):
|
|
|
|
|
model = AssignmentPoll
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_manage_assignment'
|
2013-10-03 21:49:51 +02:00
|
|
|
|
url_name = 'assignment_detail'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
allow_ajax = True
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2013-10-03 21:49:51 +02:00
|
|
|
|
def get_ajax_context(self, **kwargs):
|
2013-09-08 14:33:43 +02:00
|
|
|
|
return {'published': self.object.published}
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2013-09-08 14:33:43 +02:00
|
|
|
|
def pre_redirect(self, *args, **kwargs):
|
|
|
|
|
try:
|
2014-12-22 18:09:05 +01:00
|
|
|
|
poll = self.get_object()
|
2013-09-08 14:33:43 +02:00
|
|
|
|
except self.model.DoesNotExist:
|
|
|
|
|
messages.error(self.request, _('Ballot ID %d does not exist.') %
|
|
|
|
|
int(kwargs['poll_id']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
2014-12-22 18:09:05 +01:00
|
|
|
|
if poll.published:
|
|
|
|
|
poll.set_published(False)
|
2013-10-03 21:49:51 +02:00
|
|
|
|
else:
|
2014-12-22 18:09:05 +01:00
|
|
|
|
poll.set_published(True)
|
2013-09-08 14:33:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SetElectedView(SingleObjectMixin, RedirectView):
|
|
|
|
|
model = Assignment
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_manage_assignment'
|
2013-09-08 14:33:43 +02:00
|
|
|
|
url_name = 'assignment_detail'
|
|
|
|
|
allow_ajax = True
|
|
|
|
|
|
|
|
|
|
def pre_redirect(self, *args, **kwargs):
|
2014-10-11 14:34:49 +02:00
|
|
|
|
self.person = User.objects.get(pk=kwargs['user_id'])
|
2013-09-08 14:33:43 +02:00
|
|
|
|
self.elected = kwargs['elected']
|
2014-12-22 18:09:05 +01:00
|
|
|
|
self.get_object().set_elected(self.person, self.elected)
|
2013-09-08 14:33:43 +02:00
|
|
|
|
|
2013-10-03 21:49:51 +02:00
|
|
|
|
def get_ajax_context(self, **kwargs):
|
2013-09-08 14:33:43 +02:00
|
|
|
|
if self.elected:
|
|
|
|
|
link = reverse('assignment_user_not_elected',
|
2014-12-22 18:09:05 +01:00
|
|
|
|
args=[self.get_object().id, self.person.person_id])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
text = _('not elected')
|
|
|
|
|
else:
|
2013-09-08 14:33:43 +02:00
|
|
|
|
link = reverse('assignment_user_elected',
|
2014-12-22 18:09:05 +01:00
|
|
|
|
args=[self.get_object().id, self.person.person_id])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
text = _('elected')
|
2013-09-08 14:33:43 +02:00
|
|
|
|
return {'elected': self.elected, 'link': link, 'text': text}
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
2013-09-08 14:33:43 +02:00
|
|
|
|
class AssignmentPollDeleteView(DeleteView):
|
2012-07-06 18:00:43 +02:00
|
|
|
|
"""
|
|
|
|
|
Delete an assignment poll object.
|
|
|
|
|
"""
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_manage_assignment'
|
2012-07-06 18:00:43 +02:00
|
|
|
|
model = AssignmentPoll
|
|
|
|
|
|
|
|
|
|
def pre_redirect(self, request, *args, **kwargs):
|
|
|
|
|
self.set_assignment()
|
2013-09-08 14:33:43 +02:00
|
|
|
|
super(AssignmentPollDeleteView, self).pre_redirect(request, *args, **kwargs)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
def pre_post_redirect(self, request, *args, **kwargs):
|
|
|
|
|
self.set_assignment()
|
2013-09-08 14:33:43 +02:00
|
|
|
|
super(AssignmentPollDeleteView, self).pre_post_redirect(request, *args, **kwargs)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
def set_assignment(self):
|
2014-12-22 18:09:05 +01:00
|
|
|
|
self.assignment = self.get_object().assignment
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
def get_redirect_url(self, **kwargs):
|
2013-09-08 14:33:43 +02:00
|
|
|
|
return reverse('assignment_detail', args=[self.assignment.id])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2013-09-25 12:53:44 +02:00
|
|
|
|
def get_final_message(self):
|
2013-06-13 16:24:57 +02:00
|
|
|
|
return _('Ballot was successfully deleted.')
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentPDF(PDFView):
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_see_assignment'
|
2012-07-06 18:00:43 +02:00
|
|
|
|
top_space = 0
|
|
|
|
|
|
|
|
|
|
def get_filename(self):
|
|
|
|
|
try:
|
2013-09-08 14:33:43 +02:00
|
|
|
|
assignment_id = self.kwargs['pk']
|
2012-07-06 18:00:43 +02:00
|
|
|
|
assignment = Assignment.objects.get(id=assignment_id)
|
2012-11-24 14:01:21 +01:00
|
|
|
|
filename = u'%s-%s' % (
|
|
|
|
|
_("Assignment"),
|
|
|
|
|
assignment.name.replace(' ', '_'))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
except:
|
|
|
|
|
filename = _("Elections")
|
|
|
|
|
return filename
|
|
|
|
|
|
|
|
|
|
def append_to_pdf(self, story):
|
|
|
|
|
try:
|
2013-09-08 14:33:43 +02:00
|
|
|
|
assignment_id = self.kwargs['pk']
|
2012-07-06 18:00:43 +02:00
|
|
|
|
except KeyError:
|
|
|
|
|
assignment_id = None
|
2012-11-24 14:01:21 +01:00
|
|
|
|
if assignment_id is None: # print all assignments
|
2012-07-06 18:00:43 +02:00
|
|
|
|
title = config["assignment_pdf_title"]
|
|
|
|
|
story.append(Paragraph(title, stylesheet['Heading1']))
|
|
|
|
|
preamble = config["assignment_pdf_preamble"]
|
|
|
|
|
if preamble:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
story.append(Paragraph(
|
|
|
|
|
"%s" % preamble.replace('\r\n', '<br/>'),
|
2012-07-10 11:27:06 +02:00
|
|
|
|
stylesheet['Paragraph']))
|
2012-07-10 12:11:07 +02:00
|
|
|
|
story.append(Spacer(0, 0.75 * cm))
|
2012-09-14 00:21:59 +02:00
|
|
|
|
assignments = Assignment.objects.all()
|
2012-11-24 14:01:21 +01:00
|
|
|
|
if not assignments: # No assignments existing
|
|
|
|
|
story.append(Paragraph(
|
|
|
|
|
_("No assignments available."), stylesheet['Heading3']))
|
|
|
|
|
else: # Print all assignments
|
2012-07-06 18:00:43 +02:00
|
|
|
|
# List of assignments
|
|
|
|
|
for assignment in assignments:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
story.append(Paragraph(
|
|
|
|
|
assignment.name, stylesheet['Heading3']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
# Assignment details (each assignment on single page)
|
|
|
|
|
for assignment in assignments:
|
|
|
|
|
story.append(PageBreak())
|
2012-07-10 00:47:00 +02:00
|
|
|
|
# append the assignment to the story-object
|
|
|
|
|
self.get_assignment(assignment, story)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else: # print selected assignment
|
|
|
|
|
assignment = Assignment.objects.get(id=assignment_id)
|
2012-07-10 00:47:00 +02:00
|
|
|
|
# append the assignment to the story-object
|
|
|
|
|
self.get_assignment(assignment, story)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
def get_assignment(self, assignment, story):
|
|
|
|
|
# title
|
2012-11-24 14:01:21 +01:00
|
|
|
|
story.append(Paragraph(
|
|
|
|
|
_("Election: %s") % assignment.name, stylesheet['Heading1']))
|
2012-07-10 12:11:07 +02:00
|
|
|
|
story.append(Spacer(0, 0.5 * cm))
|
2014-11-28 20:51:24 +01:00
|
|
|
|
|
|
|
|
|
# Filling table rows...
|
2012-07-06 18:00:43 +02:00
|
|
|
|
data = []
|
2014-11-28 20:51:24 +01:00
|
|
|
|
polls = assignment.poll_set.filter(published=True)
|
|
|
|
|
# 1. posts
|
|
|
|
|
data.append([
|
|
|
|
|
Paragraph("%s:" %
|
|
|
|
|
_("Number of available posts"), stylesheet['Bold']),
|
|
|
|
|
Paragraph(str(assignment.posts), stylesheet['Paragraph'])])
|
|
|
|
|
|
|
|
|
|
# 2a. if no polls available print candidates
|
|
|
|
|
if not polls:
|
|
|
|
|
data.append([
|
|
|
|
|
Paragraph("%s:<seqreset id='counter'>" %
|
|
|
|
|
_("Candidates"), stylesheet['Heading4']),
|
|
|
|
|
[]])
|
|
|
|
|
for candidate in assignment.candidates:
|
|
|
|
|
data.append([
|
|
|
|
|
[],
|
|
|
|
|
Paragraph("<seq id='counter'/>. %s" % candidate,
|
|
|
|
|
stylesheet['Signaturefield'])])
|
|
|
|
|
if assignment.status == "sea":
|
|
|
|
|
for x in range(0, 7):
|
|
|
|
|
data.append([
|
|
|
|
|
[],
|
|
|
|
|
Paragraph("<seq id='counter'/>. "
|
|
|
|
|
"__________________________________________",
|
|
|
|
|
stylesheet['Signaturefield'])])
|
|
|
|
|
|
|
|
|
|
# 2b. if polls available print election result
|
2012-07-10 00:47:00 +02:00
|
|
|
|
if polls:
|
2014-11-28 20:51:24 +01:00
|
|
|
|
# Preparing
|
|
|
|
|
vote_results = assignment.vote_results(only_published=True)
|
|
|
|
|
data_votes = []
|
|
|
|
|
|
|
|
|
|
# Left side
|
|
|
|
|
cell = []
|
|
|
|
|
cell.append(Paragraph(
|
|
|
|
|
"%s:" % (_("Election result")), stylesheet['Heading4']))
|
|
|
|
|
|
|
|
|
|
# Add table head row
|
|
|
|
|
headrow = []
|
|
|
|
|
headrow.append(_("Candidates"))
|
|
|
|
|
for poll in polls:
|
|
|
|
|
headrow.append("%s. %s" % (poll.get_ballot(), _("ballot")))
|
|
|
|
|
data_votes.append(headrow)
|
|
|
|
|
|
|
|
|
|
# Add result rows
|
|
|
|
|
elected_candidates = list(assignment.elected)
|
|
|
|
|
length = len(vote_results)
|
|
|
|
|
for candidate, poll_list in vote_results.iteritems():
|
|
|
|
|
row = []
|
|
|
|
|
candidate_string = candidate.clean_name
|
|
|
|
|
if candidate in elected_candidates:
|
|
|
|
|
candidate_string = "* " + candidate_string
|
|
|
|
|
if candidate.name_suffix and length < 20:
|
|
|
|
|
candidate_string += "\n(%s)" % candidate.name_suffix
|
|
|
|
|
row.append(candidate_string)
|
|
|
|
|
for vote in poll_list:
|
|
|
|
|
if vote is None:
|
|
|
|
|
row.append('–')
|
|
|
|
|
elif 'Yes' in vote and 'No' in vote and 'Abstain' in vote:
|
|
|
|
|
row.append(
|
|
|
|
|
_("Y: %(YES)s\nN: %(NO)s\nA: %(ABSTAIN)s")
|
|
|
|
|
% {'YES': vote['Yes'], 'NO': vote['No'],
|
|
|
|
|
'ABSTAIN': vote['Abstain']})
|
|
|
|
|
elif 'Votes' in vote:
|
|
|
|
|
row.append(vote['Votes'])
|
|
|
|
|
else:
|
|
|
|
|
pass
|
|
|
|
|
data_votes.append(row)
|
|
|
|
|
|
|
|
|
|
# Add valid votes row
|
|
|
|
|
footrow_one = []
|
|
|
|
|
footrow_one.append(_("Valid votes"))
|
|
|
|
|
votesvalid_is_used = False
|
|
|
|
|
for poll in polls:
|
|
|
|
|
footrow_one.append(poll.print_votesvalid())
|
|
|
|
|
if poll.votesvalid is not None:
|
|
|
|
|
votesvalid_is_used = True
|
|
|
|
|
if votesvalid_is_used:
|
|
|
|
|
data_votes.append(footrow_one)
|
|
|
|
|
|
|
|
|
|
# Add invalid votes row
|
|
|
|
|
footrow_two = []
|
|
|
|
|
footrow_two.append(_("Invalid votes"))
|
|
|
|
|
votesinvalid_is_used = False
|
|
|
|
|
for poll in polls:
|
|
|
|
|
footrow_two.append(poll.print_votesinvalid())
|
|
|
|
|
if poll.votesinvalid is not None:
|
|
|
|
|
votesinvalid_is_used = True
|
|
|
|
|
if votesinvalid_is_used:
|
|
|
|
|
data_votes.append(footrow_two)
|
|
|
|
|
|
|
|
|
|
# Add votes cast row
|
|
|
|
|
footrow_three = []
|
|
|
|
|
footrow_three.append(_("Votes cast"))
|
|
|
|
|
votescast_is_used = False
|
|
|
|
|
for poll in polls:
|
|
|
|
|
footrow_three.append(poll.print_votescast())
|
|
|
|
|
if poll.votescast is not None:
|
|
|
|
|
votescast_is_used = True
|
|
|
|
|
if votescast_is_used:
|
|
|
|
|
data_votes.append(footrow_three)
|
|
|
|
|
|
|
|
|
|
table_votes = Table(data_votes)
|
|
|
|
|
table_votes.setStyle(
|
|
|
|
|
TableStyle([
|
|
|
|
|
('GRID', (0, 0), (-1, -1), 0.5, colors.grey),
|
|
|
|
|
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
|
|
|
|
('LINEABOVE', (0, 0), (-1, 0), 2, colors.black),
|
|
|
|
|
('LINEABOVE', (0, 1), (-1, 1), 1, colors.black),
|
|
|
|
|
('LINEBELOW', (0, -1), (-1, -1), 2, colors.black),
|
|
|
|
|
('ROWBACKGROUNDS', (0, 1), (-1, -1), (colors.white, (.9, .9, .9)))
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
data.append([cell, table_votes])
|
|
|
|
|
if elected_candidates:
|
|
|
|
|
data.append(['', '* = ' + _('elected')])
|
|
|
|
|
|
|
|
|
|
# table style
|
|
|
|
|
data.append(['', ''])
|
|
|
|
|
t = LongTable(data)
|
2012-07-10 11:27:06 +02:00
|
|
|
|
t._argW[0] = 4.5 * cm
|
|
|
|
|
t._argW[1] = 11 * cm
|
2012-11-24 14:01:21 +01:00
|
|
|
|
t.setStyle(TableStyle([
|
|
|
|
|
('BOX', (0, 0), (-1, -1), 1, colors.black),
|
|
|
|
|
('VALIGN', (0, 0), (-1, -1), 'TOP')]))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
story.append(t)
|
2012-07-10 11:27:06 +02:00
|
|
|
|
story.append(Spacer(0, 1 * cm))
|
2012-07-10 00:47:00 +02:00
|
|
|
|
|
2014-11-28 20:51:24 +01:00
|
|
|
|
# election description
|
2013-09-08 14:33:43 +02:00
|
|
|
|
story.append(
|
|
|
|
|
Paragraph("%s" % assignment.description.replace('\r\n', '<br/>'),
|
|
|
|
|
stylesheet['Paragraph']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
2013-09-07 00:18:13 +02:00
|
|
|
|
class CreateRelatedAgendaItemView(_CreateRelatedAgendaItemView):
|
|
|
|
|
"""
|
|
|
|
|
View to create and agenda item for an assignment.
|
|
|
|
|
"""
|
|
|
|
|
model = Assignment
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentPollPDF(PDFView):
|
2014-05-15 20:07:09 +02:00
|
|
|
|
required_permission = 'assignment.can_manage_assignment'
|
2012-07-06 18:00:43 +02:00
|
|
|
|
top_space = 0
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
self.poll = AssignmentPoll.objects.get(id=self.kwargs['poll_id'])
|
2014-10-11 14:34:49 +02:00
|
|
|
|
return super().get(request, *args, **kwargs)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
def get_filename(self):
|
2012-11-24 14:01:21 +01:00
|
|
|
|
filename = u'%s-%s_%s' % (
|
|
|
|
|
_("Election"), self.poll.assignment.name.replace(' ', '_'),
|
2012-07-23 23:00:00 +02:00
|
|
|
|
self.poll.get_ballot())
|
2012-07-06 18:00:43 +02:00
|
|
|
|
return filename
|
|
|
|
|
|
|
|
|
|
def get_template(self, buffer):
|
2012-11-24 14:01:21 +01:00
|
|
|
|
return SimpleDocTemplate(
|
|
|
|
|
buffer, topMargin=-6, bottomMargin=-6, leftMargin=0, rightMargin=0,
|
|
|
|
|
showBoundary=False)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
def build_document(self, pdf_document, story):
|
|
|
|
|
pdf_document.build(story)
|
|
|
|
|
|
|
|
|
|
def append_to_pdf(self, story):
|
2014-03-07 00:17:25 +01:00
|
|
|
|
circle = "*" # = Unicode Character 'HEAVY LARGE CIRCLE' (U+2B55)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
cell = []
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Spacer(0, 0.8 * cm))
|
|
|
|
|
cell.append(Paragraph(
|
|
|
|
|
_("Election") + ": " + self.poll.assignment.name,
|
2012-07-10 11:27:06 +02:00
|
|
|
|
stylesheet['Ballot_title']))
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
2014-01-11 17:07:47 +01:00
|
|
|
|
self.poll.description or '',
|
2012-07-10 11:27:06 +02:00
|
|
|
|
stylesheet['Ballot_subtitle']))
|
2012-11-22 16:19:09 +01:00
|
|
|
|
options = self.poll.get_options()
|
2012-07-10 11:27:06 +02:00
|
|
|
|
|
|
|
|
|
ballot_string = _("%d. ballot") % self.poll.get_ballot()
|
2012-11-24 14:01:21 +01:00
|
|
|
|
candidate_string = ungettext(
|
|
|
|
|
"%d candidate", "%d candidates", len(options)) % len(options)
|
|
|
|
|
available_posts_string = ungettext(
|
|
|
|
|
"%d available post", "%d available posts",
|
2012-10-25 21:57:02 +02:00
|
|
|
|
self.poll.assignment.posts) % self.poll.assignment.posts
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
2013-09-08 14:33:43 +02:00
|
|
|
|
"%s, %s, %s" % (ballot_string, candidate_string, available_posts_string),
|
|
|
|
|
stylesheet['Ballot_description']))
|
2012-07-10 11:27:06 +02:00
|
|
|
|
cell.append(Spacer(0, 0.4 * cm))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
|
data = []
|
2012-07-06 18:00:43 +02:00
|
|
|
|
# get ballot papers config values
|
|
|
|
|
ballot_papers_selection = config["assignment_pdf_ballot_papers_selection"]
|
|
|
|
|
ballot_papers_number = config["assignment_pdf_ballot_papers_number"]
|
|
|
|
|
|
|
|
|
|
# set number of ballot papers
|
|
|
|
|
if ballot_papers_selection == "NUMBER_OF_DELEGATES":
|
2013-04-22 19:59:05 +02:00
|
|
|
|
try:
|
|
|
|
|
if Group.objects.get(pk=3):
|
|
|
|
|
number = User.objects.filter(groups__pk=3).count()
|
|
|
|
|
except Group.DoesNotExist:
|
|
|
|
|
number = 0
|
2012-07-06 18:00:43 +02:00
|
|
|
|
elif ballot_papers_selection == "NUMBER_OF_ALL_PARTICIPANTS":
|
2012-09-18 22:47:12 +02:00
|
|
|
|
number = int(User.objects.count())
|
2012-11-24 14:01:21 +01:00
|
|
|
|
else: # ballot_papers_selection == "CUSTOM_NUMBER"
|
2012-07-06 18:00:43 +02:00
|
|
|
|
number = int(ballot_papers_number)
|
|
|
|
|
number = max(1, number)
|
|
|
|
|
|
2014-03-07 00:17:25 +01:00
|
|
|
|
counter = 0
|
|
|
|
|
cellcolumnA = []
|
|
|
|
|
# Choose kind of ballot paper (YesNoAbstain or Yes)
|
|
|
|
|
if self.poll.yesnoabstain: # YesNoAbstain ballot: max 27 candidates
|
2012-07-06 18:00:43 +02:00
|
|
|
|
for option in options:
|
2014-03-07 00:17:25 +01:00
|
|
|
|
counter += 1
|
2012-07-06 18:00:43 +02:00
|
|
|
|
candidate = option.candidate
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
2014-10-11 14:34:49 +02:00
|
|
|
|
candidate.get_short_name(), stylesheet['Ballot_option_name_YNA']))
|
|
|
|
|
if candidate.structure_level:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
2014-10-11 14:34:49 +02:00
|
|
|
|
"(%s)" % candidate.structure_level,
|
2014-03-07 00:17:25 +01:00
|
|
|
|
stylesheet['Ballot_option_suffix_YNA']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
2014-03-07 00:17:25 +01:00
|
|
|
|
" ", stylesheet['Ballot_option_suffix_YNA']))
|
|
|
|
|
cell.append(Paragraph("<font name='circlefont' size='15'>%(circle)s</font> \
|
|
|
|
|
<font name='Ubuntu'>%(yes)s </font> \
|
|
|
|
|
<font name='circlefont' size='15'>%(circle)s</font> \
|
|
|
|
|
<font name='Ubuntu'>%(no)s </font> \
|
|
|
|
|
<font name='circlefont' size='15'>%(circle)s</font> \
|
|
|
|
|
<font name='Ubuntu'>%(abstain)s</font>" %
|
|
|
|
|
{'circle': circle,
|
|
|
|
|
'yes': _("Yes"),
|
|
|
|
|
'no': _("No"),
|
|
|
|
|
'abstain': _("Abstention")},
|
|
|
|
|
stylesheet['Ballot_option_circle_YNA']))
|
|
|
|
|
if counter == 13:
|
|
|
|
|
cellcolumnA = cell
|
|
|
|
|
cell = []
|
|
|
|
|
cell.append(Spacer(0, 1.3 * cm))
|
|
|
|
|
|
2012-07-06 18:00:43 +02:00
|
|
|
|
# print ballot papers
|
2014-08-16 09:25:18 +02:00
|
|
|
|
for user in range(number // 2):
|
2014-03-07 00:17:25 +01:00
|
|
|
|
if len(options) > 13:
|
|
|
|
|
data.append([cellcolumnA, cell])
|
|
|
|
|
else:
|
|
|
|
|
data.append([cell, cell])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
rest = number % 2
|
|
|
|
|
if rest:
|
|
|
|
|
data.append([cell, ''])
|
|
|
|
|
if len(options) <= 2:
|
2012-07-10 11:27:06 +02:00
|
|
|
|
t = Table(data, 10.5 * cm, 7.42 * cm)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
elif len(options) <= 5:
|
2012-07-10 11:27:06 +02:00
|
|
|
|
t = Table(data, 10.5 * cm, 14.84 * cm)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
2012-07-10 11:27:06 +02:00
|
|
|
|
t = Table(data, 10.5 * cm, 29.7 * cm)
|
2014-03-07 00:17:25 +01:00
|
|
|
|
else: # Yes ballot: max 46 candidates
|
2012-07-06 18:00:43 +02:00
|
|
|
|
for option in options:
|
2014-03-07 00:17:25 +01:00
|
|
|
|
counter += 1
|
2012-07-06 18:00:43 +02:00
|
|
|
|
candidate = option.candidate
|
2014-03-07 00:17:25 +01:00
|
|
|
|
cell.append(Paragraph("<font name='circlefont' size='15'>%s</font> \
|
|
|
|
|
<font name='Ubuntu'>%s</font>" %
|
|
|
|
|
(circle, candidate.clean_name), stylesheet['Ballot_option_name']))
|
2014-10-11 14:34:49 +02:00
|
|
|
|
if candidate.structure_level:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
2014-10-11 14:34:49 +02:00
|
|
|
|
"(%s)" % candidate.structure_level,
|
2014-03-07 00:17:25 +01:00
|
|
|
|
stylesheet['Ballot_option_suffix']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
2014-03-07 00:17:25 +01:00
|
|
|
|
" ", stylesheet['Ballot_option_suffix']))
|
|
|
|
|
if counter == 22:
|
|
|
|
|
cellcolumnA = cell
|
|
|
|
|
cell = []
|
|
|
|
|
cell.append(Spacer(0, 0.75 * cm))
|
|
|
|
|
|
2012-07-06 18:00:43 +02:00
|
|
|
|
# print ballot papers
|
2014-08-16 09:25:18 +02:00
|
|
|
|
for user in range(number // 2):
|
2014-03-07 00:17:25 +01:00
|
|
|
|
if len(options) > 22:
|
|
|
|
|
data.append([cellcolumnA, cell])
|
|
|
|
|
else:
|
|
|
|
|
data.append([cell, cell])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
rest = number % 2
|
|
|
|
|
if rest:
|
|
|
|
|
data.append([cell, ''])
|
|
|
|
|
if len(options) <= 4:
|
2012-07-10 11:27:06 +02:00
|
|
|
|
t = Table(data, 10.5 * cm, 7.42 * cm)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
elif len(options) <= 8:
|
2012-07-10 11:27:06 +02:00
|
|
|
|
t = Table(data, 10.5 * cm, 14.84 * cm)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
2012-07-10 11:27:06 +02:00
|
|
|
|
t = Table(data, 10.5 * cm, 29.7 * cm)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
|
t.setStyle(TableStyle([
|
|
|
|
|
('GRID', (0, 0), (-1, -1), 0.25, colors.grey),
|
|
|
|
|
('VALIGN', (0, 0), (-1, -1), 'TOP')]))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
story.append(t)
|