2015-01-21 12:58:46 +01:00
|
|
|
from cgi import escape
|
2015-01-23 02:33:17 +01:00
|
|
|
|
2014-01-14 21:44:48 +01:00
|
|
|
from operator import attrgetter
|
2013-09-25 10:01:01 +02:00
|
|
|
import random
|
2013-04-07 19:46:28 +02:00
|
|
|
|
2013-09-25 10:01:01 +02:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from django.utils.translation import ugettext as _
|
2014-01-14 21:44:48 +01:00
|
|
|
from natsort import natsorted
|
2013-02-03 18:18:29 +01:00
|
|
|
from reportlab.lib import colors
|
|
|
|
from reportlab.lib.units import cm
|
2013-09-25 10:01:01 +02:00
|
|
|
from reportlab.platypus import PageBreak, Paragraph, Spacer, Table, TableStyle
|
2013-02-03 18:18:29 +01:00
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
from openslides.config.api import config
|
2014-10-11 14:34:49 +02:00
|
|
|
from openslides.users.models import Group, User # TODO: remove this line
|
2013-02-03 18:18:29 +01:00
|
|
|
from openslides.utils.pdf import stylesheet
|
2013-09-25 10:01:01 +02:00
|
|
|
|
2014-12-20 20:13:30 +01:00
|
|
|
from .models import Category
|
2013-02-03 18:18:29 +01:00
|
|
|
|
2013-04-21 19:12:50 +02:00
|
|
|
# Needed to count the delegates
|
|
|
|
# TODO: find another way to do this.
|
2013-02-03 18:18:29 +01:00
|
|
|
|
2013-02-05 18:46:46 +01:00
|
|
|
|
2014-12-20 20:13:30 +01:00
|
|
|
def motions_to_pdf(pdf, motions):
|
2013-04-21 19:12:50 +02:00
|
|
|
"""
|
|
|
|
Create a PDF with all motions.
|
|
|
|
"""
|
2014-01-14 21:44:48 +01:00
|
|
|
motions = natsorted(motions, key=attrgetter('identifier'))
|
2013-02-03 18:18:29 +01:00
|
|
|
all_motion_cover(pdf, motions)
|
|
|
|
for motion in motions:
|
|
|
|
pdf.append(PageBreak())
|
|
|
|
motion_to_pdf(pdf, motion)
|
|
|
|
|
2013-02-03 20:12:44 +01:00
|
|
|
|
2013-02-03 18:18:29 +01:00
|
|
|
def motion_to_pdf(pdf, motion):
|
2013-04-21 19:12:50 +02:00
|
|
|
"""
|
|
|
|
Create a PDF for one motion.
|
|
|
|
"""
|
2014-05-19 20:58:09 +02:00
|
|
|
identifier = ''
|
2013-06-26 23:39:49 +02:00
|
|
|
if motion.identifier:
|
2014-05-19 20:58:09 +02:00
|
|
|
identifier = ' %s' % motion.identifier
|
2015-01-21 12:58:46 +01:00
|
|
|
pdf.append(Paragraph('%s%s: %s' % (_('Motion'), identifier, escape(motion.title)), stylesheet['Heading1']))
|
2013-02-03 18:18:29 +01:00
|
|
|
|
|
|
|
motion_data = []
|
|
|
|
|
|
|
|
# submitter
|
|
|
|
cell1a = []
|
|
|
|
cell1a.append(Spacer(0, 0.2 * cm))
|
2013-02-03 20:12:44 +01:00
|
|
|
cell1a.append(Paragraph("<font name='Ubuntu-Bold'>%s:</font>" % _("Submitter"),
|
|
|
|
stylesheet['Heading4']))
|
2013-02-03 18:18:29 +01:00
|
|
|
cell1b = []
|
|
|
|
cell1b.append(Spacer(0, 0.2 * cm))
|
2015-04-30 19:13:28 +02:00
|
|
|
for submitter in motion.submitters.all():
|
2014-08-16 09:25:18 +02:00
|
|
|
cell1b.append(Paragraph(str(submitter), stylesheet['Normal']))
|
2013-02-03 18:18:29 +01:00
|
|
|
motion_data.append([cell1a, cell1b])
|
|
|
|
|
|
|
|
# TODO: choose this in workflow
|
2013-02-19 23:43:20 +01:00
|
|
|
if motion.state.allow_submitter_edit:
|
2013-02-03 18:18:29 +01:00
|
|
|
# Cell for the signature
|
|
|
|
cell2a = []
|
|
|
|
cell2b = []
|
2013-02-03 20:12:44 +01:00
|
|
|
cell2a.append(Paragraph("<font name='Ubuntu-Bold'>%s:</font>" %
|
|
|
|
_("Signature"), stylesheet['Heading4']))
|
2013-02-03 18:18:29 +01:00
|
|
|
cell2b.append(Paragraph(42 * "_", stylesheet['Signaturefield']))
|
|
|
|
cell2b.append(Spacer(0, 0.1 * cm))
|
|
|
|
cell2b.append(Spacer(0, 0.2 * cm))
|
|
|
|
motion_data.append([cell2a, cell2b])
|
|
|
|
|
|
|
|
# supporters
|
|
|
|
if config['motion_min_supporters']:
|
|
|
|
cell3a = []
|
|
|
|
cell3b = []
|
2013-02-03 20:12:44 +01:00
|
|
|
cell3a.append(Paragraph("<font name='Ubuntu-Bold'>%s:</font><seqreset id='counter'>"
|
|
|
|
% _("Supporters"), stylesheet['Heading4']))
|
2015-04-30 19:13:28 +02:00
|
|
|
supporters = motion.supporters.all()
|
2013-02-03 18:18:29 +01:00
|
|
|
for supporter in supporters:
|
2014-08-16 09:25:18 +02:00
|
|
|
cell3b.append(Paragraph("<seq id='counter'/>. %s" % str(supporter),
|
2013-09-02 20:35:52 +02:00
|
|
|
stylesheet['Normal']))
|
2013-02-03 18:18:29 +01:00
|
|
|
cell3b.append(Spacer(0, 0.2 * cm))
|
|
|
|
motion_data.append([cell3a, cell3b])
|
|
|
|
|
|
|
|
# Motion state
|
|
|
|
cell4a = []
|
|
|
|
cell4b = []
|
2013-02-03 20:12:44 +01:00
|
|
|
cell4a.append(Paragraph("<font name='Ubuntu-Bold'>%s:</font>" % _("State"),
|
|
|
|
stylesheet['Heading4']))
|
2013-05-28 20:31:01 +02:00
|
|
|
cell4b.append(Paragraph(_(motion.state.name), stylesheet['Normal']))
|
2013-02-03 18:18:29 +01:00
|
|
|
motion_data.append([cell4a, cell4b])
|
|
|
|
|
2013-06-14 20:24:48 +02:00
|
|
|
# Version number
|
2013-02-03 18:18:29 +01:00
|
|
|
if motion.versions.count() > 1:
|
2013-06-14 20:24:48 +02:00
|
|
|
version = motion.get_active_version()
|
2013-02-03 18:18:29 +01:00
|
|
|
cell5a = []
|
|
|
|
cell5b = []
|
2013-02-03 20:12:44 +01:00
|
|
|
cell5a.append(Paragraph("<font name='Ubuntu-Bold'>%s:</font>" % _("Version"),
|
|
|
|
stylesheet['Heading4']))
|
2013-06-14 20:24:48 +02:00
|
|
|
cell5b.append(Paragraph("%s" % version.version_number, stylesheet['Normal']))
|
2013-02-03 18:18:29 +01:00
|
|
|
motion_data.append([cell5a, cell5b])
|
|
|
|
|
2013-10-17 11:34:54 +02:00
|
|
|
# voting result
|
2013-02-03 18:18:29 +01:00
|
|
|
polls = []
|
|
|
|
for poll in motion.polls.all():
|
|
|
|
if not poll.has_votes():
|
|
|
|
continue
|
|
|
|
polls.append(poll)
|
|
|
|
|
|
|
|
if polls:
|
|
|
|
cell6a = []
|
|
|
|
cell6b = []
|
2013-02-03 20:12:44 +01:00
|
|
|
cell6a.append(Paragraph("<font name='Ubuntu-Bold'>%s:</font>" %
|
2013-10-17 11:34:54 +02:00
|
|
|
_("Vote result"), stylesheet['Heading4']))
|
2013-02-03 18:18:29 +01:00
|
|
|
ballotcounter = 0
|
|
|
|
for poll in polls:
|
|
|
|
ballotcounter += 1
|
|
|
|
option = poll.get_options()[0]
|
2014-04-10 20:18:22 +02:00
|
|
|
yes, no, abstain = (option['Yes'], option['No'], option['Abstain'])
|
|
|
|
valid, invalid, votescast = ('', '', '')
|
|
|
|
if poll.votesvalid is not None:
|
|
|
|
valid = "<br/>%s: %s" % (_("Valid votes"), poll.print_votesvalid())
|
|
|
|
if poll.votesinvalid is not None:
|
|
|
|
invalid = "<br/>%s: %s" % (_("Invalid votes"), poll.print_votesinvalid())
|
|
|
|
if poll.votescast is not None:
|
|
|
|
votescast = "<br/>%s: %s" % (_("Votes cast"), poll.print_votescast())
|
2013-02-03 18:18:29 +01:00
|
|
|
if len(polls) > 1:
|
2013-02-03 20:12:44 +01:00
|
|
|
cell6b.append(Paragraph("%s. %s" % (ballotcounter, _("Vote")),
|
|
|
|
stylesheet['Bold']))
|
2013-02-03 18:18:29 +01:00
|
|
|
cell6b.append(Paragraph(
|
2014-04-10 20:18:22 +02:00
|
|
|
"%s: %s <br/> %s: %s <br/> %s: %s <br/> %s %s %s" %
|
|
|
|
(_("Yes"), yes, _("No"), no, _("Abstention"), abstain, valid, invalid, votescast),
|
|
|
|
stylesheet['Normal']))
|
2013-02-03 18:18:29 +01:00
|
|
|
cell6b.append(Spacer(0, 0.2 * cm))
|
|
|
|
motion_data.append([cell6a, cell6b])
|
|
|
|
|
|
|
|
# Creating Table
|
|
|
|
table = Table(motion_data)
|
|
|
|
table._argW[0] = 4.5 * cm
|
|
|
|
table._argW[1] = 11 * cm
|
|
|
|
table.setStyle(TableStyle([('BOX', (0, 0), (-1, -1), 1, colors.black),
|
2013-02-03 20:12:44 +01:00
|
|
|
('VALIGN', (0, 0), (-1, -1), 'TOP')]))
|
2013-02-03 18:18:29 +01:00
|
|
|
pdf.append(table)
|
|
|
|
pdf.append(Spacer(0, 1 * cm))
|
|
|
|
|
2013-04-07 19:46:28 +02:00
|
|
|
# motion title
|
2015-01-21 12:58:46 +01:00
|
|
|
pdf.append(Paragraph(escape(motion.title), stylesheet['Heading3']))
|
2013-04-07 19:46:28 +02:00
|
|
|
|
|
|
|
# motion text
|
|
|
|
convert_html_to_reportlab(pdf, motion.text)
|
|
|
|
pdf.append(Spacer(0, 1 * cm))
|
|
|
|
|
|
|
|
# motion reason
|
2013-02-03 18:18:29 +01:00
|
|
|
if motion.reason:
|
2013-06-26 23:39:49 +02:00
|
|
|
pdf.append(Paragraph(_("Reason") + ":", stylesheet['Heading3']))
|
2013-04-07 19:46:28 +02:00
|
|
|
convert_html_to_reportlab(pdf, motion.reason)
|
2013-02-03 18:18:29 +01:00
|
|
|
return pdf
|
|
|
|
|
|
|
|
|
2013-04-07 19:46:28 +02:00
|
|
|
def convert_html_to_reportlab(pdf, text):
|
|
|
|
# parsing and replacing not supported html tags for reportlab...
|
|
|
|
soup = BeautifulSoup(text)
|
|
|
|
# read all list elements...
|
|
|
|
for element in soup.find_all('li'):
|
2013-04-15 21:12:29 +02:00
|
|
|
# ... and replace ul list elements with <para><bullet>•</bullet>...<para>
|
|
|
|
if element.parent.name == "ul":
|
2014-03-31 22:07:14 +02:00
|
|
|
# nested lists
|
|
|
|
if element.ul or element.ol:
|
|
|
|
for i in element.find_all('li'):
|
|
|
|
element.insert_before(i)
|
2013-04-15 21:12:29 +02:00
|
|
|
element.clear()
|
|
|
|
else:
|
|
|
|
element.name = "para"
|
|
|
|
bullet_tag = soup.new_tag("bullet")
|
2014-03-31 22:07:14 +02:00
|
|
|
bullet_tag.string = u"•"
|
2013-04-15 21:12:29 +02:00
|
|
|
element.insert(0, bullet_tag)
|
|
|
|
# ... and replace ol list elements with <para><bullet><seq id="%id"></seq>.</bullet>...</para>
|
|
|
|
if element.parent.name == "ol":
|
|
|
|
# set list id if element is the first of numbered list
|
|
|
|
if not element.find_previous_sibling():
|
|
|
|
id = random.randrange(0, 101)
|
2014-03-31 22:07:14 +02:00
|
|
|
# nested lists
|
|
|
|
if element.ul or element.ol:
|
|
|
|
for i in element.find_all('li'):
|
|
|
|
element.insert_before(i)
|
2013-04-15 21:12:29 +02:00
|
|
|
element.clear()
|
|
|
|
else:
|
|
|
|
element.name = "para"
|
|
|
|
element.insert(0, soup.new_tag("bullet"))
|
|
|
|
element.bullet.insert(0, soup.new_tag("seq"))
|
|
|
|
element.bullet.seq['id'] = id
|
|
|
|
element.bullet.insert(1, ".")
|
2013-04-07 19:46:28 +02:00
|
|
|
# remove tags which are not supported by reportlab (replace tags with their children tags)
|
|
|
|
for tag in soup.find_all('ul'):
|
|
|
|
tag.unwrap()
|
|
|
|
for tag in soup.find_all('ol'):
|
|
|
|
tag.unwrap()
|
|
|
|
for tag in soup.find_all('li'):
|
|
|
|
tag.unwrap()
|
2013-11-09 22:32:52 +01:00
|
|
|
for tag in soup.find_all('span'):
|
|
|
|
tag.unwrap()
|
2013-04-07 19:46:28 +02:00
|
|
|
# print paragraphs with numbers
|
|
|
|
text = soup.body.contents
|
|
|
|
paragraph_number = 1
|
|
|
|
for paragraph in text:
|
2014-08-16 09:25:18 +02:00
|
|
|
paragraph = str(paragraph)
|
2013-04-07 19:46:28 +02:00
|
|
|
# ignore empty paragraphs (created by newlines/tabs of ckeditor)
|
|
|
|
if paragraph == '\n' or paragraph == '\n\n' or paragraph == '\n\t':
|
|
|
|
continue
|
|
|
|
if "<pre>" in paragraph:
|
2013-10-29 14:50:06 +01:00
|
|
|
txt = paragraph.replace('\n', '<br/>').replace(' ', ' ')
|
2013-09-02 21:03:27 +02:00
|
|
|
if config["motion_pdf_paragraph_numbering"]:
|
2013-10-29 14:50:06 +01:00
|
|
|
pdf.append(Paragraph(txt, stylesheet['InnerMonotypeParagraph'], str(paragraph_number)))
|
2013-09-02 21:03:27 +02:00
|
|
|
paragraph_number += 1
|
|
|
|
else:
|
2013-10-29 14:50:06 +01:00
|
|
|
pdf.append(Paragraph(txt, stylesheet['InnerMonotypeParagraph']))
|
2013-04-07 19:46:28 +02:00
|
|
|
elif "<para>" in paragraph:
|
|
|
|
pdf.append(Paragraph(paragraph, stylesheet['InnerListParagraph']))
|
|
|
|
elif "<seqreset" in paragraph:
|
|
|
|
pass
|
|
|
|
elif "<h1>" in paragraph:
|
|
|
|
pdf.append(Paragraph(paragraph, stylesheet['InnerH1Paragraph']))
|
|
|
|
elif "<h2>" in paragraph:
|
|
|
|
pdf.append(Paragraph(paragraph, stylesheet['InnerH2Paragraph']))
|
|
|
|
elif "<h3>" in paragraph:
|
|
|
|
pdf.append(Paragraph(paragraph, stylesheet['InnerH3Paragraph']))
|
|
|
|
else:
|
2013-09-02 21:03:27 +02:00
|
|
|
if config["motion_pdf_paragraph_numbering"]:
|
|
|
|
pdf.append(Paragraph(paragraph, stylesheet['InnerParagraph'], str(paragraph_number)))
|
|
|
|
paragraph_number += 1
|
|
|
|
else:
|
|
|
|
pdf.append(Paragraph(paragraph, stylesheet['InnerParagraph']))
|
2013-04-07 19:46:28 +02:00
|
|
|
|
|
|
|
|
2013-02-03 18:18:29 +01:00
|
|
|
def all_motion_cover(pdf, motions):
|
2013-04-21 19:12:50 +02:00
|
|
|
"""
|
|
|
|
Create a coverpage for all motions.
|
|
|
|
"""
|
2015-01-21 12:58:46 +01:00
|
|
|
pdf.append(Paragraph(escape(config["motion_pdf_title"]), stylesheet['Heading1']))
|
2013-02-03 18:18:29 +01:00
|
|
|
|
2015-01-21 12:58:46 +01:00
|
|
|
preamble = escape(config["motion_pdf_preamble"])
|
2013-02-03 18:18:29 +01:00
|
|
|
if preamble:
|
2013-02-03 20:12:44 +01:00
|
|
|
pdf.append(Paragraph("%s" % preamble.replace('\r\n', '<br/>'), stylesheet['Paragraph']))
|
2013-02-03 18:18:29 +01:00
|
|
|
|
|
|
|
pdf.append(Spacer(0, 0.75 * cm))
|
|
|
|
|
2013-06-26 23:39:49 +02:00
|
|
|
# list of categories
|
|
|
|
categories = False
|
|
|
|
for i, category in enumerate(Category.objects.all()):
|
|
|
|
categories = True
|
|
|
|
if i == 0:
|
|
|
|
pdf.append(Paragraph(_("Categories"), stylesheet['Heading2']))
|
2015-01-21 12:58:46 +01:00
|
|
|
pdf.append(Paragraph("%s %s" % (escape(category.prefix), escape(category.name)), stylesheet['Paragraph']))
|
2013-06-26 23:39:49 +02:00
|
|
|
if categories:
|
|
|
|
pdf.append(PageBreak())
|
|
|
|
|
|
|
|
# list of motions
|
2013-02-03 18:18:29 +01:00
|
|
|
if not motions:
|
|
|
|
pdf.append(Paragraph(_("No motions available."), stylesheet['Heading3']))
|
|
|
|
else:
|
|
|
|
for motion in motions:
|
2014-05-19 20:58:09 +02:00
|
|
|
identifier = ''
|
2013-06-26 23:39:49 +02:00
|
|
|
if motion.identifier:
|
2014-05-19 20:58:09 +02:00
|
|
|
identifier = ' %s' % motion.identifier
|
2015-01-21 12:58:46 +01:00
|
|
|
pdf.append(Paragraph('%s%s: %s' % (_('Motion'), identifier, escape(motion.title)), stylesheet['Heading3']))
|
2013-04-21 19:12:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
def motion_poll_to_pdf(pdf, poll):
|
2014-03-07 00:17:25 +01:00
|
|
|
circle = "*" # = Unicode Character 'HEAVY LARGE CIRCLE' (U+2B55)
|
2013-04-21 19:12:50 +02:00
|
|
|
cell = []
|
|
|
|
cell.append(Spacer(0, 0.8 * cm))
|
|
|
|
cell.append(Paragraph(_("Motion No. %s") % poll.motion.identifier, stylesheet['Ballot_title']))
|
|
|
|
cell.append(Paragraph(poll.motion.title, stylesheet['Ballot_subtitle']))
|
|
|
|
cell.append(Paragraph(_("%d. Vote") % poll.poll_number, stylesheet['Ballot_description']))
|
|
|
|
cell.append(Spacer(0, 0.5 * cm))
|
2014-03-07 00:17:25 +01:00
|
|
|
cell.append(Paragraph("<font name='circlefont' size='15'>%s</font> <font name='Ubuntu'>%s</font>"
|
2014-08-16 09:25:18 +02:00
|
|
|
% (circle, _("Yes")), stylesheet['Ballot_option']))
|
2014-03-07 00:17:25 +01:00
|
|
|
cell.append(Paragraph("<font name='circlefont' size='15'>%s</font> <font name='Ubuntu'>%s</font>"
|
2014-08-16 09:25:18 +02:00
|
|
|
% (circle, _("No")), stylesheet['Ballot_option']))
|
2014-03-07 00:17:25 +01:00
|
|
|
cell.append(Paragraph("<font name='circlefont' size='15'>%s</font> <font name='Ubuntu'>%s</font>"
|
2014-08-16 09:25:18 +02:00
|
|
|
% (circle, _("Abstention")), stylesheet['Ballot_option']))
|
2013-04-21 19:12:50 +02:00
|
|
|
data = []
|
|
|
|
# get ballot papers config values
|
|
|
|
ballot_papers_selection = config["motion_pdf_ballot_papers_selection"]
|
|
|
|
ballot_papers_number = config["motion_pdf_ballot_papers_number"]
|
|
|
|
|
|
|
|
# set number of ballot papers
|
|
|
|
if ballot_papers_selection == "NUMBER_OF_DELEGATES":
|
2014-10-11 14:34:49 +02:00
|
|
|
# TODO: get this number from users
|
2013-04-24 15:07:39 +02:00
|
|
|
try:
|
2014-10-11 14:34:49 +02:00
|
|
|
if Group.objects.get(pk=3): # TODO: Find a better way
|
2013-04-24 15:07:39 +02:00
|
|
|
number = User.objects.filter(groups__pk=3).count()
|
|
|
|
except Group.DoesNotExist:
|
|
|
|
number = 0
|
2013-04-21 19:12:50 +02:00
|
|
|
elif ballot_papers_selection == "NUMBER_OF_ALL_PARTICIPANTS":
|
|
|
|
# TODO: get the number from the persons
|
|
|
|
number = int(User.objects.count())
|
|
|
|
else: # ballot_papers_selection == "CUSTOM_NUMBER"
|
|
|
|
number = int(ballot_papers_number)
|
|
|
|
number = max(1, number)
|
|
|
|
|
|
|
|
# print ballot papers
|
|
|
|
if number > 0:
|
|
|
|
# TODO: try [cell, cell] * (number / 2)
|
2014-08-16 09:25:18 +02:00
|
|
|
for user in range(number / 2):
|
2013-04-21 19:12:50 +02:00
|
|
|
data.append([cell, cell])
|
|
|
|
rest = number % 2
|
|
|
|
if rest:
|
|
|
|
data.append([cell, ''])
|
|
|
|
t = Table(data, 10.5 * cm, 7.42 * cm)
|
|
|
|
t.setStyle(TableStyle(
|
|
|
|
[('GRID', (0, 0), (-1, -1), 0.25, colors.grey),
|
|
|
|
('VALIGN', (0, 0), (-1, -1), 'TOP')]))
|
|
|
|
pdf.append(t)
|