Generate Agenda-PDF in a generic View. Added PDFView in utils.views.py

This commit is contained in:
Oskar Hahn 2012-02-21 13:17:42 +01:00
parent c55f240e84
commit 898cab4aa8
5 changed files with 65 additions and 37 deletions

View File

@ -31,6 +31,8 @@ from agenda.api import is_summary
class Item(MPTTModel, Slide):
"""
An Agenda Item
MPTT-model. See http://django-mptt.github.com/django-mptt/
"""
prefix = 'item'

View File

@ -11,7 +11,7 @@
"""
from django.conf.urls.defaults import *
from agenda.views import Overview, View, SetActive, SetClosed, ItemUpdate, ItemCreate, ItemDelete
from agenda.views import Overview, View, SetActive, SetClosed, ItemUpdate, ItemCreate, ItemDelete, ItemPDF
urlpatterns = patterns('agenda.views',
url(r'^$',
@ -64,7 +64,7 @@ urlpatterns = patterns('agenda.views',
),
url(r'^print/$',
'print_agenda',
ItemPDF.as_view(),
name='print_agenda',
),
)

View File

@ -9,14 +9,16 @@
:copyright: 2011 by the OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
from reportlab.platypus import Paragraph
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.core.context_processors import csrf
from django.views.generic.detail import SingleObjectMixin
from utils.pdf import print_agenda
from utils.views import TemplateView, RedirectView, UpdateView, CreateView, DeleteView
from utils.pdf import stylesheet
from utils.views import TemplateView, RedirectView, UpdateView, CreateView, DeleteView, PDFView
from system import config
@ -181,3 +183,17 @@ class ItemDelete(DeleteView):
self.gen_confirm_form(request, _('Do you really want to delete <b>%s</b>?') % name, object.get_absolute_url('delete'), False)
else:
self.gen_confirm_form(request, _('Do you really want to delete <b>%s</b>?') % name, object.get_absolute_url('delete'), True)
class ItemPDF(PDFView):
permission_required = 'agenda.can_see_agenda'
filename = _('Agenda')
def append_to_pdf(self, story):
for item in Item.objects.all():
ancestors = item.get_ancestors()
if ancestors:
space = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" * ancestors.count()
story.append(Paragraph("%s%s" % (space, item.title), stylesheet['Subitem']))
else:
story.append(Paragraph(item.title, stylesheet['Item']))

View File

@ -183,8 +183,6 @@ event_date = config["event_date"]
event_location = config["event_location"]
event_organizer = config["event_organizer"]
# set print time
time = datetime.now().strftime(str(_("%Y-%m-%d %H:%Mh")))
def firstPage(canvas, doc):
@ -197,6 +195,7 @@ def firstPage(canvas, doc):
canvas.drawString(2.75*cm, 27.6*cm, "%s, %s" % (event_date, event_location))
# time
canvas.setFont('Ubuntu',7)
time = datetime.now().strftime(str(_("%Y-%m-%d %H:%Mh")))
canvas.drawString(15*cm, 28*cm, _("Printed")+": %s" % time)
# title
if doc.title:
@ -217,36 +216,7 @@ def laterPages(canvas, doc):
canvas.setFont('Ubuntu',7)
canvas.setFillGray(0.4)
canvas.drawString(10*cm, 1*cm, _("Page")+" %s" % doc.page)
canvas.restoreState()
@permission_required('agenda.can_see_agenda')
def print_agenda(request):
response = HttpResponse(mimetype='application/pdf')
filename = u'filename=%s.pdf;' % _("Agenda")
response['Content-Disposition'] = filename.encode('utf-8')
doc = SimpleDocTemplate(response)
story = [Spacer(1,3*cm)]
doc.title = _("Agenda")
# print item list
items = Item.objects.all()
for item in items:
if item.hidden is False:
# print all items"
if item.parents:
space = ""
counter = 0
for p in item.parents:
if counter != 0:
space += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
counter += 1
story.append(Paragraph(space+item.title, stylesheet['Subitem']))
else:
story.append(Paragraph(item.title, stylesheet['Item']))
doc.build(story, onFirstPage=firstPage, onLaterPages=laterPages)
return response
canvas.restoreState()s
@permission_required('participant.can_see_participant')

View File

@ -3,6 +3,14 @@ try:
except ImportError:
import simplejson as json
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from reportlab.platypus import SimpleDocTemplate, Paragraph, Frame, PageBreak, Spacer, Table, LongTable, TableStyle, Image
from reportlab.lib.units import cm
from django.conf import settings
from django.http import HttpResponseServerError, HttpResponse
from django.core.urlresolvers import reverse
@ -13,10 +21,12 @@ from django.utils.decorators import method_decorator
from django.views.generic import (TemplateView as _TemplateView,
RedirectView as _RedirectView,
UpdateView as _UpdateView,
CreateView as _CreateView,)
CreateView as _CreateView,
View,)
from django.views.generic.detail import SingleObjectMixin
from utils import render_to_forbitten
from pdf import firstPage, laterPages
FREE_TO_GO = 'free to go'
@ -98,6 +108,36 @@ class DeleteView(RedirectView, SingleObjectMixin):
def pre_post_redirect(self, request, *args, **kwargs):
pass
class PDFView(View, PermissionMixin):
filename = 'No_Name'
def render_to_response(self, filename):
response = HttpResponse(mimetype='application/pdf')
filename = u'filename=%s.pdf;' % filename
response['Content-Disposition'] = filename.encode('utf-8')
buffer = StringIO()
pdf_document = SimpleDocTemplate(buffer)
story = [Spacer(1,3*cm)]
self.append_to_pdf(story)
pdf_document.build(story, onFirstPage=firstPage, onLaterPages=laterPages)
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response
def append_to_pdf(self, story):
pass
def get_filename(self):
return self.filename
def get(self, request, *args, **kwargs):
return self.render_to_response(self.get_filename())
def server_error(request, template_name='500.html'):
"""