2015-01-21 12:58:46 +01:00
|
|
|
|
from cgi import escape
|
|
|
|
|
|
2015-09-07 17:09:29 +02:00
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.contrib.auth import get_user_model
|
2015-06-14 23:26:06 +02:00
|
|
|
|
from django.db import transaction
|
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
|
2015-06-16 10:37:23 +02:00
|
|
|
|
from reportlab.platypus import (
|
|
|
|
|
LongTable,
|
|
|
|
|
PageBreak,
|
|
|
|
|
Paragraph,
|
|
|
|
|
SimpleDocTemplate,
|
|
|
|
|
Spacer,
|
|
|
|
|
Table,
|
|
|
|
|
TableStyle,
|
|
|
|
|
)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
2015-06-29 12:08:15 +02:00
|
|
|
|
from openslides.core.config import config
|
2012-07-10 14:00:51 +02:00
|
|
|
|
from openslides.utils.pdf import stylesheet
|
2015-06-14 23:26:06 +02:00
|
|
|
|
from openslides.utils.rest_api import (
|
|
|
|
|
DestroyModelMixin,
|
|
|
|
|
GenericViewSet,
|
|
|
|
|
ModelViewSet,
|
|
|
|
|
Response,
|
|
|
|
|
UpdateModelMixin,
|
|
|
|
|
ValidationError,
|
2015-06-16 10:37:23 +02:00
|
|
|
|
detail_route,
|
|
|
|
|
)
|
2015-03-09 15:40:54 +01:00
|
|
|
|
from openslides.utils.views import PDFView
|
2013-09-25 10:01:01 +02:00
|
|
|
|
|
|
|
|
|
from .models import Assignment, AssignmentPoll
|
2015-06-14 23:26:06 +02:00
|
|
|
|
from .serializers import (
|
|
|
|
|
AssignmentAllPollSerializer,
|
|
|
|
|
AssignmentFullSerializer,
|
2015-06-16 10:37:23 +02:00
|
|
|
|
AssignmentShortSerializer,
|
2015-06-14 23:26:06 +02:00
|
|
|
|
)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
|
# Viewsets for the REST API
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
|
class AssignmentViewSet(ModelViewSet):
|
2015-01-17 14:25:05 +01:00
|
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
|
API endpoint for assignments.
|
|
|
|
|
|
2015-08-31 14:07:24 +02:00
|
|
|
|
There are the following views: metadata, list, retrieve, create,
|
|
|
|
|
partial_update, update, destroy, candidature_self, candidature_other,
|
|
|
|
|
mark_elected and create_poll.
|
2015-01-17 14:25:05 +01:00
|
|
|
|
"""
|
|
|
|
|
queryset = Assignment.objects.all()
|
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
|
def check_view_permissions(self):
|
2015-01-17 14:25:05 +01:00
|
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
|
Returns True if the user has required permissions.
|
2015-01-17 14:25:05 +01:00
|
|
|
|
"""
|
2015-08-31 14:07:24 +02:00
|
|
|
|
if self.action in ('metadata', 'list', 'retrieve'):
|
2015-07-01 23:18:48 +02:00
|
|
|
|
result = self.request.user.has_perm('assignments.can_see')
|
|
|
|
|
elif self.action in ('create', 'partial_update', 'update', 'destroy',
|
|
|
|
|
'mark_elected', 'create_poll'):
|
|
|
|
|
result = (self.request.user.has_perm('assignments.can_see') and
|
|
|
|
|
self.request.user.has_perm('assignments.can_manage'))
|
|
|
|
|
elif self.action == 'candidature_self':
|
|
|
|
|
result = (self.request.user.has_perm('assignments.can_see') and
|
|
|
|
|
self.request.user.has_perm('assignments.can_nominate_self'))
|
|
|
|
|
elif self.action == 'candidature_other':
|
|
|
|
|
result = (self.request.user.has_perm('assignments.can_see') and
|
|
|
|
|
self.request.user.has_perm('assignments.can_nominate_other'))
|
|
|
|
|
else:
|
|
|
|
|
result = False
|
|
|
|
|
return result
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
|
"""
|
2015-06-14 23:26:06 +02:00
|
|
|
|
Returns different serializer classes according to users permissions.
|
2015-01-17 14:25:05 +01:00
|
|
|
|
"""
|
2015-03-26 05:36:10 +01:00
|
|
|
|
if self.request.user.has_perm('assignments.can_manage'):
|
2015-01-17 14:25:05 +01:00
|
|
|
|
serializer_class = AssignmentFullSerializer
|
|
|
|
|
else:
|
|
|
|
|
serializer_class = AssignmentShortSerializer
|
|
|
|
|
return serializer_class
|
|
|
|
|
|
2015-03-29 15:49:37 +02:00
|
|
|
|
@detail_route(methods=['post', 'delete'])
|
|
|
|
|
def candidature_self(self, request, pk=None):
|
|
|
|
|
"""
|
2015-06-14 23:26:06 +02:00
|
|
|
|
View to nominate self as candidate (POST) or withdraw own
|
|
|
|
|
candidature (DELETE).
|
2015-03-29 15:49:37 +02:00
|
|
|
|
"""
|
|
|
|
|
assignment = self.get_object()
|
|
|
|
|
if assignment.is_elected(request.user):
|
|
|
|
|
raise ValidationError({'detail': _('You are already elected.')})
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
message = self.nominate_self(request, assignment)
|
|
|
|
|
else:
|
|
|
|
|
# request.method == 'DELETE'
|
|
|
|
|
message = self.withdraw_self(request, assignment)
|
|
|
|
|
return Response({'detail': message})
|
|
|
|
|
|
|
|
|
|
def nominate_self(self, request, assignment):
|
|
|
|
|
if assignment.phase == assignment.PHASE_FINISHED:
|
|
|
|
|
raise ValidationError({'detail': _('You can not candidate to this election because it is finished.')})
|
|
|
|
|
if assignment.phase == assignment.PHASE_VOTING and not request.user.has_perm('assignments.can_manage'):
|
|
|
|
|
# To nominate self during voting you have to be a manager.
|
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
# If the request.user is already a candidate he can nominate himself nevertheless.
|
|
|
|
|
assignment.set_candidate(request.user)
|
|
|
|
|
return _('You were nominated successfully.')
|
|
|
|
|
|
|
|
|
|
def withdraw_self(self, request, assignment):
|
|
|
|
|
# Withdraw candidature and set self blocked.
|
|
|
|
|
if assignment.phase == assignment.PHASE_FINISHED:
|
|
|
|
|
raise ValidationError({'detail': _('You can not withdraw your candidature to this election because it is finished.')})
|
|
|
|
|
if assignment.phase == assignment.PHASE_VOTING and not request.user.has_perm('assignments.can_manage'):
|
|
|
|
|
# To withdraw self during voting you have to be a manager.
|
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
if not assignment.is_candidate(request.user):
|
|
|
|
|
raise ValidationError({'detail': _('You are not a candidate of this election.')})
|
|
|
|
|
assignment.set_blocked(request.user)
|
|
|
|
|
return _(
|
|
|
|
|
'You have withdrawn your candidature successfully. '
|
|
|
|
|
'You can not be nominated by other participants anymore.')
|
|
|
|
|
|
|
|
|
|
def get_user_from_request_data(self, request):
|
|
|
|
|
"""
|
|
|
|
|
Helper method to get a specific user from request data (not the
|
|
|
|
|
request.user) so that the views self.candidature_other or
|
|
|
|
|
self.mark_elected can play with him.
|
|
|
|
|
"""
|
|
|
|
|
if not isinstance(request.data, dict):
|
|
|
|
|
detail = _('Invalid data. Expected dictionary, got %s.') % type(request.data)
|
|
|
|
|
raise ValidationError({'detail': detail})
|
|
|
|
|
user_str = request.data.get('user', '')
|
|
|
|
|
try:
|
|
|
|
|
user_pk = int(user_str)
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise ValidationError({'detail': _('Invalid data. Expected something like {"user": <id>}.')})
|
|
|
|
|
try:
|
2015-09-07 17:09:29 +02:00
|
|
|
|
user = get_user_model().objects.get(pk=user_pk)
|
|
|
|
|
except get_user_model().DoesNotExist:
|
2015-03-29 15:49:37 +02:00
|
|
|
|
raise ValidationError({'detail': _('Invalid data. User %d does not exist.') % user_pk})
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
@detail_route(methods=['post', 'delete'])
|
|
|
|
|
def candidature_other(self, request, pk=None):
|
|
|
|
|
"""
|
|
|
|
|
View to nominate other users (POST) or delete their candidature
|
|
|
|
|
status (DELETE). The client has to send {'user': <id>}.
|
|
|
|
|
"""
|
|
|
|
|
user = self.get_user_from_request_data(request)
|
|
|
|
|
assignment = self.get_object()
|
|
|
|
|
if assignment.is_elected(user):
|
|
|
|
|
raise ValidationError({'detail': _('User %s is already elected.') % user})
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
message = self.nominate_other(request, user, assignment)
|
|
|
|
|
else:
|
|
|
|
|
# request.method == 'DELETE'
|
|
|
|
|
message = self.delete_other(request, user, assignment)
|
|
|
|
|
return Response({'detail': message})
|
|
|
|
|
|
|
|
|
|
def nominate_other(self, request, user, assignment):
|
|
|
|
|
if assignment.phase == assignment.PHASE_FINISHED:
|
|
|
|
|
detail = _('You can not nominate someone to this election because it is finished.')
|
|
|
|
|
raise ValidationError({'detail': detail})
|
|
|
|
|
if assignment.phase == assignment.PHASE_VOTING and not request.user.has_perm('assignments.can_manage'):
|
2015-11-28 21:23:21 +01:00
|
|
|
|
# To nominate another user during voting you have to be a manager.
|
2015-03-29 15:49:37 +02:00
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
if not request.user.has_perm('assignments.can_manage'):
|
|
|
|
|
if assignment.is_blocked(user):
|
2015-11-25 21:31:08 +01:00
|
|
|
|
raise ValidationError({'detail': _('User %s does not want to be a candidate. Only a manager can do this.') % user})
|
2015-03-29 15:49:37 +02:00
|
|
|
|
if assignment.is_elected(user):
|
|
|
|
|
raise ValidationError({'detail': _('User %s is already elected.') % user})
|
2015-11-28 21:23:21 +01:00
|
|
|
|
if assignment.is_candidate(user):
|
|
|
|
|
raise ValidationError({'detail': _('User %s is already nominated.') % user})
|
2015-03-29 15:49:37 +02:00
|
|
|
|
assignment.set_candidate(user)
|
|
|
|
|
return _('User %s was nominated successfully.') % user
|
|
|
|
|
|
|
|
|
|
def delete_other(self, request, user, assignment):
|
|
|
|
|
# To delete candidature status you have to be a manager.
|
|
|
|
|
if not request.user.has_perm('assignments.can_manage'):
|
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
if assignment.phase == assignment.PHASE_FINISHED:
|
|
|
|
|
detail = _('You can not delete someones candidature to this election because it is finished.')
|
|
|
|
|
raise ValidationError({'detail': detail})
|
|
|
|
|
if not assignment.is_candidate(user) and not assignment.is_blocked(user):
|
|
|
|
|
raise ValidationError({'detail': _('User %s has no status in this election.') % user})
|
|
|
|
|
assignment.delete_related_user(user)
|
|
|
|
|
return _('Candidate %s was withdrawn/unblocked successfully.') % user
|
|
|
|
|
|
|
|
|
|
@detail_route(methods=['post', 'delete'])
|
|
|
|
|
def mark_elected(self, request, pk=None):
|
|
|
|
|
"""
|
2015-06-14 23:26:06 +02:00
|
|
|
|
View to mark other users as elected (POST) or undo this (DELETE).
|
|
|
|
|
The client has to send {'user': <id>}.
|
2015-03-29 15:49:37 +02:00
|
|
|
|
"""
|
|
|
|
|
user = self.get_user_from_request_data(request)
|
|
|
|
|
assignment = self.get_object()
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
if not assignment.is_candidate(user):
|
|
|
|
|
raise ValidationError({'detail': _('User %s is not a candidate of this election.') % user})
|
|
|
|
|
assignment.set_elected(user)
|
|
|
|
|
message = _('User %s was successfully elected.') % user
|
|
|
|
|
else:
|
|
|
|
|
# request.method == 'DELETE'
|
|
|
|
|
if not assignment.is_elected(user):
|
|
|
|
|
detail = _('User %s is not an elected candidate of this election.') % user
|
|
|
|
|
raise ValidationError({'detail': detail})
|
|
|
|
|
assignment.set_candidate(user)
|
|
|
|
|
message = _('User %s was successfully unelected.') % user
|
|
|
|
|
return Response({'detail': message})
|
|
|
|
|
|
2015-06-14 23:26:06 +02:00
|
|
|
|
@detail_route(methods=['post'])
|
|
|
|
|
def create_poll(self, request, pk=None):
|
|
|
|
|
"""
|
|
|
|
|
View to create a poll. It is a POST request without any data.
|
|
|
|
|
"""
|
|
|
|
|
assignment = self.get_object()
|
|
|
|
|
if not assignment.candidates.exists():
|
|
|
|
|
raise ValidationError({'detail': _('Can not create poll because there are no candidates.')})
|
|
|
|
|
with transaction.atomic():
|
|
|
|
|
assignment.create_poll()
|
2015-07-22 15:23:57 +02:00
|
|
|
|
return Response({'detail': _('Poll created successfully.')})
|
2015-06-14 23:26:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentPollViewSet(UpdateModelMixin, DestroyModelMixin, GenericViewSet):
|
|
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
|
API endpoint for assignment polls.
|
|
|
|
|
|
|
|
|
|
There are the following views: update and destroy.
|
2015-06-14 23:26:06 +02:00
|
|
|
|
"""
|
|
|
|
|
queryset = AssignmentPoll.objects.all()
|
|
|
|
|
serializer_class = AssignmentAllPollSerializer
|
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
|
def check_view_permissions(self):
|
2015-06-14 23:26:06 +02:00
|
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
|
Returns True if the user has required permissions.
|
2015-06-14 23:26:06 +02:00
|
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
|
return (self.request.user.has_perm('assignments.can_see') and
|
|
|
|
|
self.request.user.has_perm('assignments.can_manage'))
|
|
|
|
|
|
2015-06-14 23:26:06 +02:00
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
|
# Views to generate PDFs
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
2012-07-06 18:00:43 +02:00
|
|
|
|
class AssignmentPDF(PDFView):
|
2015-03-26 05:36:10 +01:00
|
|
|
|
required_permission = 'assignments.can_see'
|
2012-07-06 18:00:43 +02:00
|
|
|
|
top_space = 0
|
|
|
|
|
|
|
|
|
|
def get_filename(self):
|
|
|
|
|
try:
|
2015-01-25 15:10:34 +01:00
|
|
|
|
assignment = Assignment.objects.get(pk=self.kwargs['pk'])
|
2012-11-24 14:01:21 +01:00
|
|
|
|
filename = u'%s-%s' % (
|
|
|
|
|
_("Assignment"),
|
2015-01-25 15:10:34 +01:00
|
|
|
|
assignment.title.replace(' ', '_'))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
except:
|
|
|
|
|
filename = _("Elections")
|
|
|
|
|
return filename
|
|
|
|
|
|
|
|
|
|
def append_to_pdf(self, story):
|
|
|
|
|
try:
|
2015-01-25 15:10:34 +01:00
|
|
|
|
assignment_pk = self.kwargs['pk']
|
2012-07-06 18:00:43 +02:00
|
|
|
|
except KeyError:
|
2015-01-25 15:10:34 +01:00
|
|
|
|
assignment_pk = None
|
|
|
|
|
|
|
|
|
|
if assignment_pk is None: # print all assignments
|
2015-06-16 18:12:59 +02:00
|
|
|
|
title = escape(config["assignments_pdf_title"])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
story.append(Paragraph(title, stylesheet['Heading1']))
|
2015-06-16 18:12:59 +02:00
|
|
|
|
preamble = escape(config["assignments_pdf_preamble"])
|
2012-07-06 18:00:43 +02:00
|
|
|
|
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(
|
2015-01-25 15:10:34 +01:00
|
|
|
|
escape(assignment.title), 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
|
2015-01-25 15:10:34 +01:00
|
|
|
|
assignment = Assignment.objects.get(pk=assignment_pk)
|
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(
|
2015-01-25 15:10:34 +01:00
|
|
|
|
_("Election: %s") % escape(assignment.title), 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 = []
|
2015-01-25 15:10:34 +01:00
|
|
|
|
polls = assignment.polls.filter(published=True)
|
2014-11-28 20:51:24 +01:00
|
|
|
|
# 1. posts
|
|
|
|
|
data.append([
|
|
|
|
|
Paragraph("%s:" %
|
2015-01-25 15:10:34 +01:00
|
|
|
|
_("Number of members to be elected"), stylesheet['Bold']),
|
|
|
|
|
Paragraph(str(assignment.open_posts), stylesheet['Paragraph'])])
|
2014-11-28 20:51:24 +01:00
|
|
|
|
|
|
|
|
|
# 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'])])
|
2015-01-25 15:10:34 +01:00
|
|
|
|
if assignment.phase == assignment.PHASE_SEARCH:
|
2014-11-28 20:51:24 +01:00
|
|
|
|
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 = []
|
2015-11-25 21:31:08 +01:00
|
|
|
|
candidate_string = candidate.get_short_name()
|
2014-11-28 20:51:24 +01:00
|
|
|
|
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(
|
2015-01-21 12:58:46 +01:00
|
|
|
|
Paragraph("%s" % escape(assignment.description).replace('\r\n', '<br/>'),
|
2013-09-08 14:33:43 +02:00
|
|
|
|
stylesheet['Paragraph']))
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentPollPDF(PDFView):
|
2015-03-26 05:36:10 +01:00
|
|
|
|
required_permission = 'assignments.can_manage'
|
2012-07-06 18:00:43 +02:00
|
|
|
|
top_space = 0
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2015-01-25 15:10:34 +01:00
|
|
|
|
self.poll = AssignmentPoll.objects.get(pk=self.kwargs['poll_pk'])
|
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' % (
|
2015-01-25 15:10:34 +01:00
|
|
|
|
_("Election"), self.poll.assignment.title.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(
|
2015-01-25 15:10:34 +01:00
|
|
|
|
_("Election") + ": " + self.poll.assignment.title,
|
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",
|
2015-01-25 15:10:34 +01:00
|
|
|
|
self.poll.assignment.open_posts) % self.poll.assignment.open_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
|
2015-06-16 18:12:59 +02:00
|
|
|
|
ballot_papers_selection = config["assignments_pdf_ballot_papers_selection"]
|
|
|
|
|
ballot_papers_number = config["assignments_pdf_ballot_papers_number"]
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
|
# set number of ballot papers
|
|
|
|
|
if ballot_papers_selection == "NUMBER_OF_DELEGATES":
|
2015-09-07 17:09:29 +02:00
|
|
|
|
if 'openslides.users' in settings.INSTALLED_APPS:
|
|
|
|
|
from openslides.users.models import Group
|
|
|
|
|
try:
|
|
|
|
|
if Group.objects.get(pk=3):
|
|
|
|
|
number = get_user_model().objects.filter(groups__pk=3).count()
|
|
|
|
|
except Group.DoesNotExist:
|
|
|
|
|
number = 0
|
|
|
|
|
else:
|
2013-04-22 19:59:05 +02:00
|
|
|
|
number = 0
|
2012-07-06 18:00:43 +02:00
|
|
|
|
elif ballot_papers_selection == "NUMBER_OF_ALL_PARTICIPANTS":
|
2015-09-07 17:09:29 +02:00
|
|
|
|
number = int(get_user_model().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>" %
|
2015-11-25 21:31:08 +01:00
|
|
|
|
(circle, candidate.get_short_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)
|