2012-07-06 18:00:43 +02:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
openslides.assignment.views
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Views for the assignment app.
|
|
|
|
|
|
|
|
|
|
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
|
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
from reportlab.lib import colors
|
2012-11-24 14:01:21 +01:00
|
|
|
|
from reportlab.platypus import (
|
|
|
|
|
SimpleDocTemplate, PageBreak, Paragraph, Spacer, Table, TableStyle)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
from reportlab.lib.units import cm
|
|
|
|
|
|
2012-07-10 11:27:06 +02:00
|
|
|
|
from django.conf import settings
|
2012-07-06 18:00:43 +02:00
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
from django.contrib import messages
|
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2012-07-10 11:27:06 +02:00
|
|
|
|
from django.shortcuts import redirect
|
2012-07-10 12:11:07 +02:00
|
|
|
|
from django.utils.translation import ungettext, ugettext as _
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2012-07-10 14:00:51 +02:00
|
|
|
|
from openslides.utils.pdf import stylesheet
|
|
|
|
|
from openslides.utils.template import Tab
|
2012-11-24 14:01:21 +01:00
|
|
|
|
from openslides.utils.utils import (
|
|
|
|
|
template, permission_required, gen_confirm_form, del_confirm_form, ajax_request)
|
2012-07-10 14:00:51 +02:00
|
|
|
|
from openslides.utils.views import FormView, DeleteView, PDFView, RedirectView
|
2012-08-07 22:43:57 +02:00
|
|
|
|
from openslides.utils.person import get_person
|
2012-07-10 11:27:06 +02:00
|
|
|
|
from openslides.config.models import config
|
2012-08-12 12:52:38 +02:00
|
|
|
|
from openslides.participant.models import User
|
2012-07-10 11:27:06 +02:00
|
|
|
|
from openslides.projector.projector import Widget
|
|
|
|
|
from openslides.poll.views import PollFormView
|
|
|
|
|
from openslides.agenda.models import Item
|
2012-11-24 14:01:21 +01:00
|
|
|
|
from openslides.assignment.models import Assignment, AssignmentPoll
|
|
|
|
|
from openslides.assignment.forms import (
|
|
|
|
|
AssignmentForm, AssignmentRunForm, ConfigForm)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_see_assignment')
|
|
|
|
|
@template('assignment/overview.html')
|
|
|
|
|
def get_overview(request):
|
|
|
|
|
query = Assignment.objects
|
|
|
|
|
if 'status' in request.GET and '---' not in request.GET['status']:
|
|
|
|
|
query = query.filter(status__iexact=request.GET['status'])
|
|
|
|
|
try:
|
|
|
|
|
sort = request.GET['sort']
|
2012-11-24 14:01:21 +01:00
|
|
|
|
if sort in ['name', 'status']:
|
2012-07-06 18:00:43 +02:00
|
|
|
|
query = query.order_by(sort)
|
|
|
|
|
except KeyError:
|
2012-09-14 00:21:59 +02:00
|
|
|
|
pass
|
2012-07-06 18:00:43 +02:00
|
|
|
|
if 'reverse' in request.GET:
|
|
|
|
|
query = query.reverse()
|
|
|
|
|
|
|
|
|
|
assignments = query.all()
|
|
|
|
|
return {
|
|
|
|
|
'assignments': assignments,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_see_assignment')
|
|
|
|
|
@template('assignment/view.html')
|
|
|
|
|
def view(request, assignment_id=None):
|
|
|
|
|
form = None
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
if request.user.has_perm('assignment.can_nominate_other'):
|
|
|
|
|
form = AssignmentRunForm(request.POST)
|
|
|
|
|
if form.is_valid():
|
|
|
|
|
user = form.cleaned_data['candidate']
|
|
|
|
|
try:
|
2012-07-16 22:21:23 +02:00
|
|
|
|
assignment.run(user, request.user)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
except NameError, e:
|
|
|
|
|
messages.error(request, e)
|
2012-09-06 15:57:42 +02:00
|
|
|
|
else:
|
|
|
|
|
messages.success(request, _(
|
|
|
|
|
"Candidate <b>%s</b> was nominated successfully.")
|
|
|
|
|
% user)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
|
|
|
|
if request.user.has_perm('assignment.can_nominate_other'):
|
|
|
|
|
form = AssignmentRunForm()
|
|
|
|
|
|
|
|
|
|
polls = assignment.poll_set.all()
|
2012-07-10 00:47:00 +02:00
|
|
|
|
if not request.user.has_perm('assignment.can_manage_assignment'):
|
|
|
|
|
polls = assignment.poll_set.filter(published=True)
|
|
|
|
|
vote_results = assignment.vote_results(only_published=True)
|
|
|
|
|
else:
|
|
|
|
|
polls = assignment.poll_set.all()
|
|
|
|
|
vote_results = assignment.vote_results(only_published=False)
|
2012-08-06 14:58:52 +02:00
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
|
blocked_candidates = [
|
|
|
|
|
candidate.person for candidate in
|
2012-11-21 20:12:08 +01:00
|
|
|
|
assignment.assignment_candidates.filter(blocked=True)]
|
2012-07-06 18:00:43 +02:00
|
|
|
|
return {
|
|
|
|
|
'assignment': assignment,
|
2012-09-13 14:59:14 +02:00
|
|
|
|
'blocked_candidates': blocked_candidates,
|
2012-07-06 18:00:43 +02:00
|
|
|
|
'form': form,
|
|
|
|
|
'vote_results': vote_results,
|
|
|
|
|
'polls': polls,
|
2012-09-13 14:59:14 +02:00
|
|
|
|
'user_is_candidate': assignment.is_candidate(request.user)}
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
@template('assignment/edit.html')
|
|
|
|
|
def edit(request, assignment_id=None):
|
|
|
|
|
"""
|
|
|
|
|
View zum editieren und neuanlegen von Wahlen
|
|
|
|
|
"""
|
|
|
|
|
if assignment_id is not None:
|
|
|
|
|
assignment = Assignment.objects.get(id=assignment_id)
|
|
|
|
|
else:
|
|
|
|
|
assignment = None
|
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
form = AssignmentForm(request.POST, instance=assignment)
|
|
|
|
|
if form.is_valid():
|
|
|
|
|
assignment = form.save()
|
|
|
|
|
if assignment_id is None:
|
|
|
|
|
messages.success(request, _('New election was successfully created.'))
|
|
|
|
|
else:
|
|
|
|
|
messages.success(request, _('Election was successfully modified.'))
|
|
|
|
|
if not 'apply' in request.POST:
|
|
|
|
|
return redirect(reverse("assignment_overview"))
|
|
|
|
|
if assignment_id is None:
|
|
|
|
|
return redirect(reverse('assignment_edit', args=[assignment.id]))
|
|
|
|
|
else:
|
|
|
|
|
messages.error(request, _('Please check the form for errors.'))
|
|
|
|
|
else:
|
|
|
|
|
form = AssignmentForm(instance=assignment)
|
|
|
|
|
if assignment:
|
|
|
|
|
polls = assignment.poll_set.filter(assignment=assignment)
|
|
|
|
|
else:
|
|
|
|
|
polls = None
|
|
|
|
|
return {
|
|
|
|
|
'form': form,
|
|
|
|
|
'assignment': assignment,
|
|
|
|
|
'polls': polls,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
def delete(request, assignment_id):
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
assignment.delete()
|
|
|
|
|
messages.success(request, _('Election <b>%s</b> was successfully deleted.') % assignment)
|
|
|
|
|
else:
|
|
|
|
|
del_confirm_form(request, assignment)
|
|
|
|
|
return redirect(reverse('assignment_overview'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
@template('assignment/view.html')
|
|
|
|
|
def set_status(request, assignment_id=None, status=None):
|
|
|
|
|
try:
|
|
|
|
|
if status is not None:
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
assignment.set_status(status)
|
|
|
|
|
messages.success(request, _('Election status was set to: <b>%s</b>.') % assignment.get_status_display())
|
|
|
|
|
except Assignment.DoesNotExist:
|
|
|
|
|
pass
|
2012-11-21 19:36:12 +01:00
|
|
|
|
except NameError, e:
|
|
|
|
|
messages.error(request, e)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_nominate_self')
|
|
|
|
|
def run(request, assignment_id):
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
try:
|
2012-08-12 12:52:38 +02:00
|
|
|
|
assignment.run(request.user, request.user)
|
2012-11-24 14:01:21 +01:00
|
|
|
|
messages.success(request, _('You have set your candidature successfully.'))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
except NameError, e:
|
|
|
|
|
messages.error(request, e)
|
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
def delrun(request, assignment_id):
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
2012-09-13 15:07:24 +02:00
|
|
|
|
if assignment.status == 'sea' or request.user.has_perm("assignment.can_manage_assignment"):
|
|
|
|
|
try:
|
2012-09-06 15:57:42 +02:00
|
|
|
|
assignment.delrun(request.user, blocked=True)
|
2012-09-13 15:07:24 +02:00
|
|
|
|
except Exception, e:
|
|
|
|
|
messages.error(request, e)
|
2012-08-04 15:02:02 +02:00
|
|
|
|
else:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
messages.success(
|
|
|
|
|
request,
|
2012-09-13 21:51:50 +02:00
|
|
|
|
_("You have withdrawn your candidature successfully. "
|
|
|
|
|
"You can not be nominated by other participants anymore."))
|
2012-08-03 13:49:05 +02:00
|
|
|
|
else:
|
2012-09-13 15:07:24 +02:00
|
|
|
|
messages.error(request, _('The candidate list is already closed.'))
|
|
|
|
|
|
2012-07-06 18:00:43 +02:00
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
2012-08-03 13:49:05 +02:00
|
|
|
|
def delother(request, assignment_id, user_id):
|
2012-07-06 18:00:43 +02:00
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
2012-08-07 22:43:57 +02:00
|
|
|
|
person = get_person(user_id)
|
2012-09-13 21:51:50 +02:00
|
|
|
|
is_blocked = assignment.is_blocked(person)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
2012-07-16 22:21:23 +02:00
|
|
|
|
try:
|
2012-09-06 15:57:42 +02:00
|
|
|
|
assignment.delrun(person, blocked=False)
|
2012-08-03 13:49:05 +02:00
|
|
|
|
except Exception, e:
|
2012-07-16 22:21:23 +02:00
|
|
|
|
messages.error(request, e)
|
2012-08-03 13:49:05 +02:00
|
|
|
|
else:
|
2012-09-13 21:51:50 +02:00
|
|
|
|
if not is_blocked:
|
|
|
|
|
message = _("Candidate <b>%s</b> was withdrawn successfully.") % person
|
|
|
|
|
else:
|
|
|
|
|
message = _("<b>%s</b> was unblocked successfully.") % person
|
|
|
|
|
messages.success(request, message)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
2012-09-13 21:51:50 +02:00
|
|
|
|
if not is_blocked:
|
|
|
|
|
message = _("Do you really want to withdraw <b>%s</b> from the election?") % person
|
|
|
|
|
else:
|
2012-11-07 22:37:33 +01:00
|
|
|
|
message = _("Do you really want to unblock <b>%s</b> for the election?") % person
|
2012-09-13 21:51:50 +02:00
|
|
|
|
gen_confirm_form(request, message, reverse('assignment_delother', args=[assignment_id, user_id]))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
def set_active(request, assignment_id):
|
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
|
|
|
|
assignment.set_active()
|
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
def gen_poll(request, assignment_id):
|
|
|
|
|
poll = Assignment.objects.get(pk=assignment_id).gen_poll()
|
2012-11-24 14:01:21 +01:00
|
|
|
|
messages.success(request, _("New ballot was successfully created."))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
return redirect(reverse('assignment_poll_view', args=[poll.id]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ViewPoll(PollFormView):
|
|
|
|
|
poll_class = AssignmentPoll
|
|
|
|
|
template_name = 'assignment/poll_view.html'
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super(ViewPoll, self).get_context_data(**kwargs)
|
|
|
|
|
self.assignment = self.poll.get_assignment()
|
|
|
|
|
context['assignment'] = self.assignment
|
|
|
|
|
context['poll'] = self.poll
|
|
|
|
|
context['polls'] = self.assignment.poll_set.filter(assignment=self.assignment)
|
|
|
|
|
context['ballotnumber'] = self.poll.get_ballot()
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
|
if not 'apply' in self.request.POST:
|
|
|
|
|
return reverse('assignment_view', args=[self.poll.assignment.id])
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
|
|
|
|
def set_publish_status(request, poll_id):
|
|
|
|
|
try:
|
|
|
|
|
poll = AssignmentPoll.objects.get(pk=poll_id)
|
|
|
|
|
if poll.published:
|
|
|
|
|
poll.set_published(False)
|
|
|
|
|
else:
|
|
|
|
|
poll.set_published(True)
|
|
|
|
|
except AssignmentPoll.DoesNotExist:
|
|
|
|
|
messages.error(request, _('Ballot ID %d does not exist.') % int(poll_id))
|
2012-07-27 22:49:38 +02:00
|
|
|
|
return redirect(reverse('assignment_overview'))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
if request.is_ajax():
|
|
|
|
|
return ajax_request({'published': poll.published})
|
|
|
|
|
|
|
|
|
|
if poll.published:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
messages.success(request, _("Ballot successfully published."))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
messages.success(request, _("Ballot successfully unpublished."))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
return redirect(reverse('assignment_view', args=[poll.assignment.id]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@permission_required('assignment.can_manage_assignment')
|
2012-08-03 18:56:00 +02:00
|
|
|
|
def set_elected(request, assignment_id, user_id, elected=True):
|
2012-07-06 18:00:43 +02:00
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_id)
|
2012-08-07 22:43:57 +02:00
|
|
|
|
person = get_person(user_id)
|
|
|
|
|
assignment.set_elected(person, elected)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
if request.is_ajax():
|
|
|
|
|
if elected:
|
2012-08-07 22:43:57 +02:00
|
|
|
|
link = reverse('assignment_user_not_elected', args=[assignment.id, person.person_id])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
text = _('not elected')
|
|
|
|
|
else:
|
2012-08-07 22:43:57 +02:00
|
|
|
|
link = reverse('assignment_user_elected', args=[assignment.id, person.person_id])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
text = _('elected')
|
2012-08-03 18:56:00 +02:00
|
|
|
|
return ajax_request({'elected': elected, 'link': link, 'text': text})
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
return redirect(reverse('assignment_view', args=[assignment_id]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentPollDelete(DeleteView):
|
|
|
|
|
"""
|
|
|
|
|
Delete an assignment poll object.
|
|
|
|
|
"""
|
|
|
|
|
permission_required = 'assignment.can_manage_assignment'
|
|
|
|
|
model = AssignmentPoll
|
|
|
|
|
|
|
|
|
|
def pre_redirect(self, request, *args, **kwargs):
|
|
|
|
|
self.set_assignment()
|
|
|
|
|
super(AssignmentPollDelete, self).pre_redirect(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def pre_post_redirect(self, request, *args, **kwargs):
|
|
|
|
|
self.set_assignment()
|
|
|
|
|
super(AssignmentPollDelete, self).pre_post_redirect(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def set_assignment(self):
|
|
|
|
|
self.assignment = self.object.assignment
|
|
|
|
|
|
|
|
|
|
def get_redirect_url(self, **kwargs):
|
|
|
|
|
return reverse('assignment_view', args=[self.assignment.id])
|
|
|
|
|
|
|
|
|
|
def get_success_message(self):
|
2013-01-04 12:39:42 +01:00
|
|
|
|
return _('Ballot was successfully deleted.') % self.object
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentPDF(PDFView):
|
|
|
|
|
permission_required = 'assignment.can_see_assignment'
|
|
|
|
|
top_space = 0
|
|
|
|
|
|
|
|
|
|
def get_filename(self):
|
|
|
|
|
try:
|
|
|
|
|
assignment_id = self.kwargs['assignment_id']
|
|
|
|
|
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:
|
|
|
|
|
assignment_id = self.kwargs['assignment_id']
|
|
|
|
|
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))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
# posts
|
|
|
|
|
cell1a = []
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell1a.append(Paragraph(
|
|
|
|
|
"<font name='Ubuntu-Bold'>%s:</font>" %
|
2012-07-10 11:27:06 +02:00
|
|
|
|
_("Number of available posts"), stylesheet['Bold']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
cell1b = []
|
|
|
|
|
cell1b.append(Paragraph(str(assignment.posts), stylesheet['Paragraph']))
|
|
|
|
|
# candidates
|
|
|
|
|
cell2a = []
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell2a.append(Paragraph(
|
|
|
|
|
"<font name='Ubuntu-Bold'>%s:</font><seqreset"
|
2012-07-10 11:27:06 +02:00
|
|
|
|
" id='counter'>" % _("Candidates"), stylesheet['Heading4']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
cell2b = []
|
2012-08-04 15:02:02 +02:00
|
|
|
|
for candidate in assignment.candidates:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell2b.append(Paragraph(
|
|
|
|
|
"<seq id='counter'/>. %s" % candidate,
|
2012-07-10 11:27:06 +02:00
|
|
|
|
stylesheet['Signaturefield']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
if assignment.status == "sea":
|
2012-07-10 00:47:00 +02:00
|
|
|
|
for x in range(0, 2 * assignment.posts):
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell2b.append(
|
|
|
|
|
Paragraph(
|
|
|
|
|
"<seq id='counter'/>. "
|
|
|
|
|
"__________________________________________",
|
|
|
|
|
stylesheet['Signaturefield']))
|
2012-07-10 12:11:07 +02:00
|
|
|
|
cell2b.append(Spacer(0, 0.2 * cm))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
# Vote results
|
|
|
|
|
|
|
|
|
|
# Preparing
|
2012-07-10 00:47:00 +02:00
|
|
|
|
vote_results = assignment.vote_results(only_published=True)
|
|
|
|
|
polls = assignment.poll_set.filter(published=True)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
data_votes = []
|
|
|
|
|
|
|
|
|
|
# Left side
|
|
|
|
|
cell3a = []
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell3a.append(Paragraph(
|
|
|
|
|
"%s:" % (_("Vote results")), stylesheet['Heading4']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2012-07-10 00:47:00 +02:00
|
|
|
|
if polls.count() == 1:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell3a.append(Paragraph(
|
|
|
|
|
"%s %s" % (polls.count(), _("ballot")), stylesheet['Normal']))
|
2012-07-10 00:47:00 +02:00
|
|
|
|
elif polls.count() > 1:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell3a.append(Paragraph(
|
|
|
|
|
"%s %s" % (polls.count(), _("ballots")), stylesheet['Normal']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
# Add table head row
|
|
|
|
|
headrow = []
|
|
|
|
|
headrow.append(_("Candidates"))
|
2012-07-10 00:47:00 +02:00
|
|
|
|
for poll in polls:
|
|
|
|
|
headrow.append("%s." % poll.get_ballot())
|
2012-07-06 18:00:43 +02:00
|
|
|
|
data_votes.append(headrow)
|
|
|
|
|
|
|
|
|
|
# Add result rows
|
2012-08-08 10:52:13 +02:00
|
|
|
|
elected_candidates = list(assignment.elected)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
for candidate, poll_list in vote_results.iteritems():
|
|
|
|
|
row = []
|
2012-07-10 00:47:00 +02:00
|
|
|
|
|
2012-10-24 12:15:29 +02:00
|
|
|
|
candidate_string = candidate.clean_name
|
2012-07-10 00:47:00 +02:00
|
|
|
|
if candidate in elected_candidates:
|
|
|
|
|
candidate_string = "* " + candidate_string
|
2012-10-24 12:30:45 +02:00
|
|
|
|
if candidate.name_suffix:
|
|
|
|
|
candidate_string += "\n(%s)" % candidate.name_suffix
|
2012-07-10 00:47:00 +02:00
|
|
|
|
row.append(candidate_string)
|
|
|
|
|
for vote in poll_list:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
if vote is None:
|
2012-07-10 00:47:00 +02:00
|
|
|
|
row.append('–')
|
|
|
|
|
elif 'Yes' in vote and 'No' in vote and 'Abstain' in vote:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
row.append(
|
|
|
|
|
_("Y: %(YES)s\nN: %(NO)s\nA: %(ABSTAIN)s")
|
|
|
|
|
% {'YES': vote['Yes'], 'NO': vote['No'],
|
|
|
|
|
'ABSTAIN': vote['Abstain']})
|
2012-07-10 00:47:00 +02:00
|
|
|
|
elif 'Votes' in vote:
|
|
|
|
|
row.append(vote['Votes'])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
|
|
|
|
pass
|
|
|
|
|
data_votes.append(row)
|
|
|
|
|
|
|
|
|
|
# Add votes invalid row
|
|
|
|
|
footrow_one = []
|
|
|
|
|
footrow_one.append(_("Invalid votes"))
|
2012-07-10 00:47:00 +02:00
|
|
|
|
for poll in polls:
|
|
|
|
|
footrow_one.append(poll.print_votesinvalid())
|
2012-07-06 18:00:43 +02:00
|
|
|
|
data_votes.append(footrow_one)
|
|
|
|
|
|
|
|
|
|
# Add votes cast row
|
|
|
|
|
footrow_two = []
|
|
|
|
|
footrow_two.append(_("Votes cast"))
|
2012-07-10 00:47:00 +02:00
|
|
|
|
for poll in polls:
|
|
|
|
|
footrow_two.append(poll.print_votescast())
|
2012-07-06 18:00:43 +02:00
|
|
|
|
data_votes.append(footrow_two)
|
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
|
table_votes = Table(data_votes)
|
|
|
|
|
table_votes.setStyle(TableStyle([
|
2012-07-10 00:47:00 +02:00
|
|
|
|
('GRID', (0, 0), (-1, -1), 0.5, colors.grey),
|
2012-11-24 14:01:21 +01:00
|
|
|
|
('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)))]))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
# table
|
|
|
|
|
data = []
|
2012-07-10 12:11:07 +02:00
|
|
|
|
data.append([cell1a, cell1b])
|
2012-07-10 00:47:00 +02:00
|
|
|
|
if polls:
|
2012-07-10 12:11:07 +02:00
|
|
|
|
data.append([cell3a, table_votes])
|
|
|
|
|
data.append(['', '* = ' + _('elected')])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
2012-07-10 00:47:00 +02:00
|
|
|
|
data.append([cell2a, cell2b])
|
2012-07-10 11:27:06 +02:00
|
|
|
|
data.append([Spacer(0, 0.2 * cm), ''])
|
2012-11-24 14:01:21 +01:00
|
|
|
|
t = Table(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
|
|
|
|
|
2012-07-06 18:00:43 +02:00
|
|
|
|
# text
|
2012-11-24 14:01:21 +01:00
|
|
|
|
story.append(Paragraph(
|
|
|
|
|
"%s" % assignment.description.replace('\r\n',
|
2012-07-10 11:27:06 +02:00
|
|
|
|
'<br/>'), stylesheet['Paragraph']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CreateAgendaItem(RedirectView):
|
|
|
|
|
permission_required = 'agenda.can_manage_agenda'
|
|
|
|
|
|
|
|
|
|
def pre_redirect(self, request, *args, **kwargs):
|
|
|
|
|
self.assignment = Assignment.objects.get(pk=kwargs['assignment_id'])
|
|
|
|
|
self.item = Item(related_sid=self.assignment.sid)
|
|
|
|
|
self.item.save()
|
|
|
|
|
|
|
|
|
|
def get_redirect_url(self, **kwargs):
|
|
|
|
|
return reverse('item_overview')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentPollPDF(PDFView):
|
|
|
|
|
permission_required = 'assignment.can_manage_assignment'
|
|
|
|
|
top_space = 0
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
self.poll = AssignmentPoll.objects.get(id=self.kwargs['poll_id'])
|
|
|
|
|
return super(AssignmentPollPDF, self).get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
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):
|
2013-01-18 22:23:31 +01:00
|
|
|
|
imgpath = os.path.join(settings.SITE_ROOT, 'static/img/circle.png')
|
2012-07-06 18:00:43 +02:00
|
|
|
|
circle = "<img src='%s' width='15' height='15'/> " % imgpath
|
|
|
|
|
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(
|
|
|
|
|
self.poll.assignment.polldescription,
|
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(
|
|
|
|
|
"%s, %s, %s" % (ballot_string, candidate_string,
|
2012-07-10 11:27:06 +02:00
|
|
|
|
available_posts_string), stylesheet['Ballot_description']))
|
|
|
|
|
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":
|
2012-09-18 22:47:12 +02:00
|
|
|
|
number = User.objects.filter(type__iexact="delegate").count()
|
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)
|
|
|
|
|
|
|
|
|
|
# Choose kind of ballot paper
|
|
|
|
|
if self.poll.yesnoabstain:
|
|
|
|
|
for option in options:
|
|
|
|
|
candidate = option.candidate
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
|
|
|
|
candidate.clean_name, stylesheet['Ballot_option_name']))
|
2012-09-18 22:37:53 +02:00
|
|
|
|
if candidate.name_suffix:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
|
|
|
|
"(%s)" % candidate.name_suffix,
|
2012-07-10 11:27:06 +02:00
|
|
|
|
stylesheet['Ballot_option_group']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
|
|
|
|
" ", stylesheet['Ballot_option_group']))
|
|
|
|
|
cell.append(Paragraph(
|
|
|
|
|
circle + _("Yes") + " " * 3 + circle
|
|
|
|
|
+ _("No") + " " * 3 + circle + _("Abstention"),
|
2012-07-10 11:27:06 +02:00
|
|
|
|
stylesheet['Ballot_option_YNA']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
# print ballot papers
|
|
|
|
|
for user in xrange(number / 2):
|
|
|
|
|
data.append([cell, cell])
|
|
|
|
|
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)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
|
|
|
|
for option in options:
|
|
|
|
|
candidate = option.candidate
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
|
|
|
|
circle + candidate.clean_name,
|
2012-07-10 11:27:06 +02:00
|
|
|
|
stylesheet['Ballot_option_name']))
|
2012-10-24 12:15:29 +02:00
|
|
|
|
if candidate.name_suffix:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
|
|
|
|
"(%s)" % candidate.name_suffix,
|
2012-07-10 11:27:06 +02:00
|
|
|
|
stylesheet['Ballot_option_group_right']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
else:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
cell.append(Paragraph(
|
|
|
|
|
" ", stylesheet['Ballot_option_group_right']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
# print ballot papers
|
|
|
|
|
for user in xrange(number / 2):
|
|
|
|
|
data.append([cell, cell])
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Config(FormView):
|
|
|
|
|
permission_required = 'config.can_manage_config'
|
|
|
|
|
form_class = ConfigForm
|
|
|
|
|
template_name = 'assignment/config.html'
|
2013-02-03 19:43:36 +01:00
|
|
|
|
success_url_name = 'config_assignment'
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
|
return {
|
2012-07-10 11:27:06 +02:00
|
|
|
|
'assignment_publish_winner_results_only':
|
2012-11-24 14:01:21 +01:00
|
|
|
|
config['assignment_publish_winner_results_only'],
|
2012-07-10 11:27:06 +02:00
|
|
|
|
'assignment_pdf_ballot_papers_selection':
|
2012-11-24 14:01:21 +01:00
|
|
|
|
config['assignment_pdf_ballot_papers_selection'],
|
2012-07-10 11:27:06 +02:00
|
|
|
|
'assignment_pdf_ballot_papers_number':
|
2012-11-24 14:01:21 +01:00
|
|
|
|
config['assignment_pdf_ballot_papers_number'],
|
2012-07-06 18:00:43 +02:00
|
|
|
|
'assignment_pdf_title': config['assignment_pdf_title'],
|
|
|
|
|
'assignment_pdf_preamble': config['assignment_pdf_preamble'],
|
2012-07-10 11:27:06 +02:00
|
|
|
|
'assignment_poll_vote_values':
|
2012-11-24 14:01:21 +01:00
|
|
|
|
config['assignment_poll_vote_values']}
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
|
if form.cleaned_data['assignment_publish_winner_results_only']:
|
|
|
|
|
config['assignment_publish_winner_results_only'] = True
|
|
|
|
|
else:
|
|
|
|
|
config['assignment_publish_winner_results_only'] = False
|
2012-07-10 11:27:06 +02:00
|
|
|
|
config['assignment_pdf_ballot_papers_selection'] = \
|
|
|
|
|
form.cleaned_data['assignment_pdf_ballot_papers_selection']
|
|
|
|
|
config['assignment_pdf_ballot_papers_number'] = \
|
|
|
|
|
form.cleaned_data['assignment_pdf_ballot_papers_number']
|
|
|
|
|
config['assignment_pdf_title'] = \
|
|
|
|
|
form.cleaned_data['assignment_pdf_title']
|
|
|
|
|
config['assignment_pdf_preamble'] = \
|
|
|
|
|
form.cleaned_data['assignment_pdf_preamble']
|
|
|
|
|
config['assignment_poll_vote_values'] = \
|
|
|
|
|
form.cleaned_data['assignment_poll_vote_values']
|
2012-11-24 14:01:21 +01:00
|
|
|
|
messages.success(
|
|
|
|
|
self.request, _('Election settings successfully saved.'))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
return super(Config, self).form_valid(form)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def register_tab(request):
|
2012-07-10 11:27:06 +02:00
|
|
|
|
selected = request.path.startswith('/assignment/')
|
2012-07-06 18:00:43 +02:00
|
|
|
|
return Tab(
|
|
|
|
|
title=_('Elections'),
|
2012-09-05 00:20:28 +02:00
|
|
|
|
app='assignment',
|
2012-07-06 18:00:43 +02:00
|
|
|
|
url=reverse('assignment_overview'),
|
2012-11-24 14:01:21 +01:00
|
|
|
|
permission=(
|
|
|
|
|
request.user.has_perm('assignment.can_see_assignment') or
|
|
|
|
|
request.user.has_perm('assignment.can_nominate_other') or
|
|
|
|
|
request.user.has_perm('assignment.can_nominate_self') or
|
|
|
|
|
request.user.has_perm('assignment.can_manage_assignment')),
|
2012-07-06 18:00:43 +02:00
|
|
|
|
selected=selected,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_widgets(request):
|
2013-02-03 19:43:36 +01:00
|
|
|
|
return [Widget(
|
|
|
|
|
name='assignments',
|
|
|
|
|
display_name=_('Elections'),
|
|
|
|
|
template='assignment/widget.html',
|
|
|
|
|
context={'assignments': Assignment.objects.all()},
|
|
|
|
|
permission_required='projector.can_manage_projector')]
|