From 5f3c6c4821afa763ba2fbd71693ed32a87a31c05 Mon Sep 17 00:00:00 2001 From: Emanuel Schuetze Date: Fri, 20 Apr 2012 23:23:50 +0200 Subject: [PATCH] #111: Move pdf creation code from utils to participant app. --- openslides/participant/urls.py | 6 ++- openslides/participant/views.py | 93 +++++++++++++++++++++++++++++++-- openslides/utils/pdf.py | 87 ------------------------------ 3 files changed, 92 insertions(+), 94 deletions(-) diff --git a/openslides/participant/urls.py b/openslides/participant/urls.py index 3d1b39e5d..51542c3bf 100644 --- a/openslides/participant/urls.py +++ b/openslides/participant/urls.py @@ -13,6 +13,8 @@ from django.conf.urls.defaults import * from django.core.urlresolvers import reverse +from participant.views import ParticipantsListPDF, ParticipantsPasswordsPDF + urlpatterns = patterns('participant.views', url(r'^$', 'get_overview', @@ -30,7 +32,7 @@ urlpatterns = patterns('participant.views', ), url(r'^print$', - 'print_userlist', + ParticipantsListPDF.as_view(), name='user_print', ), @@ -87,7 +89,7 @@ urlpatterns = patterns('participant.views', ), url(r'^passwords/print$', - 'print_passwords', + ParticipantsPasswordsPDF.as_view(), name='print_passwords', ), ) diff --git a/openslides/participant/views.py b/openslides/participant/views.py index 6d64adb04..f60003683 100644 --- a/openslides/participant/views.py +++ b/openslides/participant/views.py @@ -31,6 +31,11 @@ from django.contrib import messages from django.core.urlresolvers import reverse from django.utils.translation import ugettext as _, ungettext from django.db import transaction +from django.db.models import Avg, Max, Min, Count + +from reportlab.lib import colors +from reportlab.lib.units import cm +from reportlab.platypus import PageBreak, Paragraph, LongTable, Spacer, Table, TableStyle from participant.models import Profile from participant.api import gen_username, gen_password @@ -38,16 +43,16 @@ from participant.forms import (UserNewForm, UserEditForm, ProfileForm, UsersettingsForm, UserImportForm, GroupForm, AdminPasswordChangeForm, ConfigForm) from application.models import Application + +from config.models import config + from utils.utils import (template, permission_required, gen_confirm_form, ajax_request, decodedict, encodedict) -from utils.pdf import print_userlist, print_passwords +from utils.pdf import stylesheet from utils.template import Tab -from utils.views import FormView -from config.models import config +from utils.views import (FormView, PDFView) from utils.utils import delete_default_permissions -from django.db.models import Avg, Max, Min, Count - @permission_required('participant.can_see_participant') @template('participant/overview.html') @@ -487,6 +492,84 @@ def register_tab(request): ) +class ParticipantsListPDF(PDFView): + permission_required = 'participant.can_see_participant' + filename = _("Participant-list") + document_title = _('List of Participants') + + def append_to_pdf(self, story): + data= [['#', _('Last Name'), _('First Name'), _('Group'), _('Type'), _('Committee')]] + sort = 'last_name' + counter = 0 + for user in User.objects.all().order_by(sort): + try: + counter += 1 + user.get_profile() + data.append([counter, + Paragraph(user.last_name, stylesheet['Tablecell']), + Paragraph(user.first_name, stylesheet['Tablecell']), + Paragraph(user.profile.group, stylesheet['Tablecell']), + Paragraph(user.profile.get_type_display(), stylesheet['Tablecell']), + Paragraph(user.profile.committee, stylesheet['Tablecell']), + ]) + except Profile.DoesNotExist: + counter -= 1 + pass + t=LongTable(data, + style=[ + ('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))), + ]) + t._argW[0]=0.75*cm + story.append(t) + + +class ParticipantsPasswordsPDF(PDFView): + permission_required = 'participant.can_manage_participant' + filename = _("Participant-passwords") + top_space = 0 + + def append_to_pdf(self, story): + #doc = SimpleDocTemplate(response, pagesize=A4, topMargin=-6, bottomMargin=-6, leftMargin=0, rightMargin=0, showBoundary=False) + #story = [Spacer(0,0*cm)] + data= [] + participant_pdf_system_url = config["participant_pdf_system_url"] + participant_pdf_welcometext = config["participant_pdf_welcometext"] + for user in User.objects.all().order_by('last_name'): + try: + user.get_profile() + cell = [] + cell.append(Spacer(0,0.8*cm)) + cell.append(Paragraph(_("Your Account for OpenSlides"), stylesheet['Ballot_title'])) + cell.append(Paragraph(_("for %s") % (user.profile), stylesheet['Ballot_subtitle'])) + cell.append(Spacer(0,0.5*cm)) + cell.append(Paragraph(_("User: %s") % (user.username), stylesheet['Ballot_option'])) + cell.append(Paragraph(_("Password: %s") % (user.profile.firstpassword), stylesheet['Ballot_option'])) + 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)) + cell2 = [] + cell2.append(Spacer(0,0.8*cm)) + if participant_pdf_welcometext is not None: + cell2.append(Paragraph(participant_pdf_welcometext.replace('\r\n','
'), stylesheet['Ballot_subtitle'])) + + data.append([cell,cell2]) + except Profile.DoesNotExist: + pass + + t=Table(data, 10.5*cm, 7.42*cm) + t.setStyle(TableStyle([ ('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), + ('VALIGN', (0,0), (-1,-1), 'TOP'), + ])) + story.append(t) + + + class Config(FormView): permission_required = 'config.can_manage_config' form_class = ConfigForm diff --git a/openslides/utils/pdf.py b/openslides/utils/pdf.py index 74a78ed2c..931051c6f 100755 --- a/openslides/utils/pdf.py +++ b/openslides/utils/pdf.py @@ -208,90 +208,3 @@ def laterPages(canvas, doc): canvas.setFillGray(0.4) canvas.drawString(10 * cm, 1 * cm, _("Page") + " %s" % doc.page) canvas.restoreState() - - -@permission_required('participant.can_see_participant') -def print_userlist(request): - response = HttpResponse(mimetype='application/pdf') - filename = u'filename=%s.pdf;' % _("Participant-list") - response['Content-Disposition'] = filename.encode('utf-8') - doc = SimpleDocTemplate(response) - story = [Spacer(1,2*cm)] - - doc.title = _("List of Participants") - # Table - data= [['#', _('Last Name'), _('First Name'), _('Group'), _('Type'), _('Committee')]] - sort = 'last_name' - counter = 0 - for user in User.objects.all().order_by(sort): - try: - counter += 1 - user.get_profile() - data.append([counter, - Paragraph(user.last_name, stylesheet['Tablecell']), - Paragraph(user.first_name, stylesheet['Tablecell']), - Paragraph(user.profile.group, stylesheet['Tablecell']), - Paragraph(user.profile.get_type_display(), stylesheet['Tablecell']), - Paragraph(user.profile.committee, stylesheet['Tablecell']), - ]) - except Profile.DoesNotExist: - counter -= 1 - pass - - t=LongTable(data, - style=[ - ('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))), - ]) - t._argW[0]=0.75*cm - story.append(t) - doc.build(story, onFirstPage=firstPage, onLaterPages=laterPages) - return response - - -@permission_required('participant.can_manage_participant') -def print_passwords(request): - response = HttpResponse(mimetype='application/pdf') - filename = u'filename=%s.pdf;' % _("passwords") - response['Content-Disposition'] = filename.encode('utf-8') - doc = SimpleDocTemplate(response, pagesize=A4, topMargin=-6, bottomMargin=-6, leftMargin=0, rightMargin=0, showBoundary=False) - story = [Spacer(0,0*cm)] - - data= [] - participant_pdf_system_url = config["participant_pdf_system_url"] - participant_pdf_welcometext = config["participant_pdf_welcometext"] - for user in User.objects.all().order_by('last_name'): - try: - user.get_profile() - cell = [] - cell.append(Spacer(0,0.8*cm)) - cell.append(Paragraph(_("Your Account for OpenSlides"), stylesheet['Ballot_title'])) - cell.append(Paragraph(_("for %s") % (user.profile), stylesheet['Ballot_subtitle'])) - cell.append(Spacer(0,0.5*cm)) - cell.append(Paragraph(_("User: %s") % (user.username), stylesheet['Ballot_option'])) - cell.append(Paragraph(_("Password: %s") % (user.profile.firstpassword), stylesheet['Ballot_option'])) - 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)) - cell2 = [] - cell2.append(Spacer(0,0.8*cm)) - if participant_pdf_welcometext is not None: - cell2.append(Paragraph(participant_pdf_welcometext.replace('\r\n','
'), stylesheet['Ballot_subtitle'])) - - data.append([cell,cell2]) - except Profile.DoesNotExist: - pass - - t=Table(data, 10.5*cm, 7.42*cm) - t.setStyle(TableStyle([ ('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), - ('VALIGN', (0,0), (-1,-1), 'TOP'), - ])) - story.append(t) - doc.build(story) - return response -