From 7a6eaf26606097d1c16917fa5cec5bbc510704b8 Mon Sep 17 00:00:00 2001 From: Emanuel Schuetze Date: Fri, 20 Apr 2012 14:33:40 +0200 Subject: [PATCH] Fixed building AssignmentPollPDF. --- openslides/assignment/urls.py | 4 +- openslides/assignment/views.py | 106 ++++++++++++++++++++++++++++++++- openslides/utils/pdf.py | 90 ---------------------------- 3 files changed, 107 insertions(+), 93 deletions(-) diff --git a/openslides/assignment/urls.py b/openslides/assignment/urls.py index bb62603dd..f118b1902 100644 --- a/openslides/assignment/urls.py +++ b/openslides/assignment/urls.py @@ -12,7 +12,7 @@ from django.conf.urls.defaults import * -from assignment.views import ViewPoll, AssignmentPDF, AssignmentPollDelete +from assignment.views import ViewPoll, AssignmentPDF, AssignmentPollPDF, AssignmentPollDelete urlpatterns = patterns('assignment.views', url(r'^$', @@ -65,7 +65,7 @@ urlpatterns = patterns('assignment.views', ), url(r'^poll/(?P\d+)/print$', - 'print_assignment_poll', + AssignmentPollPDF.as_view(), name='print_assignment_poll', ), diff --git a/openslides/assignment/views.py b/openslides/assignment/views.py index d3bf1a959..68e54233c 100644 --- a/openslides/assignment/views.py +++ b/openslides/assignment/views.py @@ -10,10 +10,12 @@ :license: GNU GPL, see LICENSE for more details. """ +import os from django.shortcuts import redirect from django.core.urlresolvers import reverse from django.contrib import messages from django.contrib.auth.decorators import login_required +from django.contrib.auth.models import User from django.utils.translation import ugettext as _ from reportlab.lib import colors @@ -23,7 +25,6 @@ from reportlab.platypus import PageBreak, Paragraph, Spacer, Table, TableStyle from config.models import config from utils.utils import template, permission_required, gen_confirm_form, del_confirm_form, ajax_request -from utils.pdf import print_assignment_poll from utils.pdf import stylesheet from utils.views import FormView, DeleteView, PDFView from utils.template import Tab @@ -35,6 +36,7 @@ from assignment.forms import AssignmentForm, AssignmentRunForm, ConfigForm from participant.models import Profile +from settings import SITE_ROOT @permission_required('assignment.can_see_assignment') @template('assignment/overview.html') @@ -436,6 +438,108 @@ class AssignmentPDF(PDFView): return votes +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): + filename = u'%s-%s-#%s' % (_("Election"), self.poll.assignment.name.replace(' ','_'), 1)#self.poll.get_ballot()) + return filename + + 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)] + + imgpath = os.path.join(SITE_ROOT, 'static/images/circle.png') + circle = "  " % imgpath + cell = [] + cell.append(Spacer(0,0.8*cm)) + cell.append(Paragraph(_("Election") + ": " + self.poll.assignment.name, stylesheet['Ballot_title'])) + cell.append(Paragraph(self.poll.assignment.description, stylesheet['Ballot_subtitle'])) + + #options = self.poll.assignment.get_options().order_by('user__user__first_name') + options = self.poll.get_options().order_by('candidate') + + #cell.append(Paragraph(str(self.poll.get_ballot())+". "+_("ballot")+", "+str(len(options))+" "+ ungettext("candidate", "candidates", len(options))+", "+str(self.poll.assignment.posts)+" "+_("available posts"), stylesheet['Ballot_description'])) + cell.append(Spacer(0,0.4*cm)) + + data= [] + # get ballot papers config values + number = 1 + ballot_papers_selection = config["assignment_pdf_ballot_papers_selection"] + ballot_papers_number = config["assignment_pdf_ballot_papers_number"] + # TODO: 'optiondecision' + #if self.poll.optiondecision: + # for option in options: + # o = str(option).split("(",1) + # cell.append(Paragraph(o[0], stylesheet['Ballot_option_name'])) + # if len(o) > 1: + # cell.append(Paragraph("("+o[1], stylesheet['Ballot_option_group'])) + # else: + # cell.append(Paragraph(" ", stylesheet['Ballot_option_group'])) + # cell.append(Paragraph(circle+_("Yes")+"      "+circle+_("No")+"      "+circle+_("Abstention"), stylesheet['Ballot_option_YNA'])) + # # set number of ballot papers + # if ballot_papers_selection == "1": + # number = User.objects.filter(profile__type__iexact="delegate").count() + # if ballot_papers_selection == "2": + # number = int(User.objects.count() - 1) + # if ballot_papers_selection == "0": + # number = int(ballot_papers_number) + # # 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: + # t=Table(data, 10.5*cm, 7.42*cm) + # elif len(options) <= 5: + # t=Table(data, 10.5*cm, 14.84*cm) + # else: + # t=Table(data, 10.5*cm, 29.7*cm) + #else: + for option in options: + o = str(option).split("(",1) + cell.append(Paragraph(circle+o[0], stylesheet['Ballot_option_name'])) + if len(o) > 1: + cell.append(Paragraph("("+o[1], stylesheet['Ballot_option_group_right'])) + else: + cell.append(Paragraph(" ", stylesheet['Ballot_option_group_right'])) + # set number of ballot papers + if ballot_papers_selection == "1": + number = User.objects.filter(profile__type__iexact="delegate").count() + if ballot_papers_selection == "2": + number = int(User.objects.count() - 1) + if ballot_papers_selection == "0": + number = int(ballot_papers_number) + if number == 0: + number = 1 + # print ballot papers + if number > 0: + for user in xrange(number/2): + data.append([cell,cell]) + rest = number % 2 + if rest: + data.append([cell,'']) + + if len(options) <= 4: + t=Table(data, 10.5*cm, 7.42*cm) + elif len(options) <= 8: + t=Table(data, 10.5*cm, 14.84*cm) + else: + t=Table(data, 10.5*cm, 29.7*cm) + + t.setStyle(TableStyle([ ('GRID', (0,0), (-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 a48cf7e7b..74a78ed2c 100755 --- a/openslides/utils/pdf.py +++ b/openslides/utils/pdf.py @@ -295,93 +295,3 @@ def print_passwords(request): doc.build(story) return response - - -@permission_required('application.can_manage_application') -def print_assignment_poll(request, poll_id=None): - poll = Poll.objects.get(id=poll_id) - response = HttpResponse(mimetype='application/pdf') - filename = u'filename=%s-%s-#%s.pdf;' % (_("Election"), poll.assignment.name.replace(' ','_'), poll.ballot) - 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)] - - imgpath = os.path.join(SITE_ROOT, 'static/images/circle.png') - circle = "  " % imgpath - cell = [] - cell.append(Spacer(0,0.8*cm)) - cell.append(Paragraph(_("Election") + ": " + poll.assignment.name, stylesheet['Ballot_title'])) - cell.append(Paragraph(poll.description, stylesheet['Ballot_subtitle'])) - options = poll.get_options().order_by('user__user__first_name') - cell.append(Paragraph(str(poll.ballot)+". "+_("ballot")+", "+str(len(options))+" "+ ungettext("candidate", "candidates", len(options))+", "+str(poll.assignment.posts)+" "+_("available posts"), stylesheet['Ballot_description'])) - cell.append(Spacer(0,0.4*cm)) - - data= [] - # get ballot papers config values - number = 1 - ballot_papers_selection = config["assignment_pdf_ballot_papers_selection"] - ballot_papers_number = config["assignment_pdf_ballot_papers_number"] - if poll.optiondecision: - for option in options: - o = str(option).split("(",1) - cell.append(Paragraph(o[0], stylesheet['Ballot_option_name'])) - if len(o) > 1: - cell.append(Paragraph("("+o[1], stylesheet['Ballot_option_group'])) - else: - cell.append(Paragraph(" ", stylesheet['Ballot_option_group'])) - cell.append(Paragraph(circle+_("Yes")+"      "+circle+_("No")+"      "+circle+_("Abstention"), stylesheet['Ballot_option_YNA'])) - # set number of ballot papers - if ballot_papers_selection == "1": - number = User.objects.filter(profile__type__iexact="delegate").count() - if ballot_papers_selection == "2": - number = int(User.objects.count() - 1) - if ballot_papers_selection == "0": - number = int(ballot_papers_number) - # 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: - t=Table(data, 10.5*cm, 7.42*cm) - elif len(options) <= 5: - t=Table(data, 10.5*cm, 14.84*cm) - else: - t=Table(data, 10.5*cm, 29.7*cm) - else: - for option in options: - o = str(option).split("(",1) - cell.append(Paragraph(circle+o[0], stylesheet['Ballot_option_name'])) - if len(o) > 1: - cell.append(Paragraph("("+o[1], stylesheet['Ballot_option_group_right'])) - else: - cell.append(Paragraph(" ", stylesheet['Ballot_option_group_right'])) - # set number of ballot papers - if ballot_papers_selection == "1": - number = User.objects.filter(profile__type__iexact="delegate").count() - if ballot_papers_selection == "2": - number = int(User.objects.count() - 1) - if ballot_papers_selection == "0": - number = int(ballot_papers_number) - # 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: - t=Table(data, 10.5*cm, 7.42*cm) - elif len(options) <= 8: - t=Table(data, 10.5*cm, 14.84*cm) - else: - t=Table(data, 10.5*cm, 29.7*cm) - - t.setStyle(TableStyle([ ('GRID', (0,0), (-1,-1), 0.25, colors.grey), - ('VALIGN', (0,0), (-1,-1), 'TOP'), - ])) - story.append(t) - doc.build(story) - return response