From 71ac7a1cdb1fb15865464bf103a527c424eaf2ee Mon Sep 17 00:00:00 2001 From: Emanuel Schuetze Date: Mon, 18 Mar 2013 21:17:34 +0100 Subject: [PATCH] New feature: qr-code for system url on participants password pdf. (Note: New dependency! Run 'pip install qrcode') Fixed pep8 issues. Create qr-code image for system url one time only. New feature: qr-code for system url on participants password pdf. (Note: New dependency! Run 'pip install qrcode') Fixed pep8 issues. Create qr-code image for system url one time only. Print qrcode image from StringIO stream. Adapted table stylesheet for better integration of reportlab's Image object. Fixed pep8 issue. Made qrcode import as optional feature. Fixed pep8 issue. --- openslides/participant/views.py | 31 +++++++++++++++++++++++-------- openslides/utils/pdf.py | 23 ++++++++++++++++------- requirements.txt | 2 ++ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/openslides/participant/views.py b/openslides/participant/views.py index b3c890ef9..3e05009a1 100644 --- a/openslides/participant/views.py +++ b/openslides/participant/views.py @@ -10,13 +10,21 @@ :license: GNU GPL, see LICENSE for more details. """ +try: + import qrcode +except ImportError: + draw_qrcode = False +else: + draw_qrcode = True + +from cStringIO import StringIO from urllib import urlencode from urlparse import parse_qs from reportlab.lib import colors from reportlab.lib.units import cm from reportlab.platypus import ( - SimpleDocTemplate, Paragraph, LongTable, Spacer, Table, TableStyle) + SimpleDocTemplate, Paragraph, LongTable, Spacer, Table, TableStyle, Image) from django.contrib import messages from django.contrib.auth.decorators import login_required @@ -229,13 +237,21 @@ class ParticipantsPasswordsPDF(PDFView): sort = 'first_name' else: sort = 'last_name' + # create qrcode image object from system url + if draw_qrcode: + qrcode_img = qrcode.make(participant_pdf_system_url) + img_stream = StringIO() + qrcode_img.save(img_stream, 'PNG') + img_stream.seek(0) + size = 2*cm + I = Image(img_stream, width=size, height=size) for user in User.objects.all().order_by(sort): cell = [] cell.append(Spacer(0, 0.8 * cm)) cell.append(Paragraph(_("Account for OpenSlides"), - stylesheet['Ballot_title'])) + stylesheet['Password_title'])) cell.append(Paragraph(_("for %s") % (user), - stylesheet['Ballot_subtitle'])) + stylesheet['Password_subtitle'])) cell.append(Spacer(0, 0.5 * cm)) cell.append(Paragraph(_("User: %s") % (user.username), stylesheet['Monotype'])) @@ -243,12 +259,10 @@ class ParticipantsPasswordsPDF(PDFView): Paragraph( _("Password: %s") % (user.default_password), stylesheet['Monotype'])) - cell.append(Spacer(0, 0.5 * cm)) cell.append( - Paragraph( - _("URL: %s") % (participant_pdf_system_url), - stylesheet['Ballot_option'])) - cell.append(Spacer(0, 0.5 * cm)) + Paragraph(participant_pdf_system_url, stylesheet['Monotype'])) + if draw_qrcode: + cell.append(I) cell2 = [] cell2.append(Spacer(0, 0.8 * cm)) if participant_pdf_welcometext is not None: @@ -264,6 +278,7 @@ class ParticipantsPasswordsPDF(PDFView): # build table t = Table(data, 10.5 * cm, 7.42 * cm) t.setStyle(TableStyle([ + ('LEFTPADDING', (0, 0), (0, -1), 30), ('LINEBELOW', (0, 0), (-1, 0), 0.25, colors.grey), ('LINEBELOW', (0, 1), (-1, 1), 0.25, colors.grey), ('LINEBELOW', (0, 1), (-1, -1), 0.25, colors.grey), diff --git a/openslides/utils/pdf.py b/openslides/utils/pdf.py index cf6db3c44..772641b16 100755 --- a/openslides/utils/pdf.py +++ b/openslides/utils/pdf.py @@ -147,13 +147,6 @@ stylesheet.add(ParagraphStyle(name='Ballot_option', leading=24, leftIndent=30), ) -stylesheet.add(ParagraphStyle(name='Monotype', - parent=stylesheet['Normal'], - fontName='Courier', - fontSize=12, - leading=24, - leftIndent=30), - ) stylesheet.add(ParagraphStyle(name='Ballot_option_name', parent=stylesheet['Normal'], fontSize=12, @@ -179,6 +172,22 @@ stylesheet.add(ParagraphStyle(name='Ballot_option_group_right', leading=16, leftIndent=49), ) +# Password paper stylesheets +stylesheet.add(ParagraphStyle(name='Password_title', + parent=stylesheet['Ballot_title'], + leftIndent=0), + ) +stylesheet.add(ParagraphStyle(name='Password_subtitle', + parent=stylesheet['Ballot_subtitle'], + leftIndent=0), + ) +stylesheet.add(ParagraphStyle(name='Monotype', + parent=stylesheet['Normal'], + fontName='Courier', + fontSize=12, + leading=24, + leftIndent=0), + ) stylesheet.add(ParagraphStyle(name='Badge_title', parent=stylesheet['Bold'], fontSize=16, diff --git a/requirements.txt b/requirements.txt index 218657ae1..74614ef08 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,5 @@ coverage==3.6 django-discover-runner==0.3 pep8==1.4.4 tornado==2.4.1 +qrcode==2.5.1 +