Changeset 8112
- Timestamp:
- 11 Apr 2012, 14:57:34 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/students/utils.py
r8099 r8112 29 29 from reportlab.lib.pagesizes import A4 30 30 from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle 31 from reportlab.platypus import (Frame, Paragraph, Image, 32 Table,Spacer)31 from reportlab.platypus import (Frame, Paragraph, Image, PageBreak, Table, 32 Spacer) 33 33 from reportlab.platypus.tables import TableStyle 34 34 from reportlab.platypus.flowables import PageBreak 35 35 from zope.component import getUtility 36 36 from zope.formlib.form import setUpEditWidgets 37 37 38 from waeup.kofa.interfaces import IExtFileStore, IKofaUtils 38 39 from waeup.kofa.interfaces import MessageFactory as _ … … 57 58 tag1 ='<font color=%s size=%d>' % (color, size) 58 59 return tag1 + '%s:</font>' 60 61 def trans(text, lang): 62 # shortcut 63 return translate(text, 'waeup.kofa', target_language=lang) 59 64 60 65 def formatted_text(text, color=FONT_COLOR, size=FONT_SIZE): … … 125 130 data_right.append([f_label,f_text]) 126 131 table_left = Table(data_left,style=SLIP_STYLE) 127 table_right = Table(data_right,style=SLIP_STYLE, colWidths=[ 6*cm, 8*cm])132 table_right = Table(data_right,style=SLIP_STYLE, colWidths=[5*cm, 6*cm]) 128 133 table = Table([[table_left, table_right],],style=SLIP_STYLE) 129 134 return table … … 152 157 return table 153 158 159 def docs_as_flowables(view, lang='en'): 160 """Create reportlab flowables out of scanned docs. 161 """ 162 # XXX: fix circular import problem 163 from waeup.kofa.students.viewlets import FileManager 164 from waeup.kofa.browser import DEFAULT_IMAGE_PATH 165 from waeup.kofa.browser.pdf import NORMAL_STYLE, ENTRY1_STYLE 166 style = getSampleStyleSheet() 167 data = [] 168 169 # Collect viewlets 170 fm = FileManager(view.context, view.request, view) 171 fm.update() 172 if fm.viewlets: 173 sc_translation = trans(_('Scanned Documents'), lang) 174 data.append(Paragraph(sc_translation, style["Heading3"])) 175 # Insert list of scanned documents 176 table_data = [] 177 for viewlet in fm.viewlets: 178 f_label = Paragraph(trans(viewlet.label, lang), ENTRY1_STYLE) 179 img_path = getattr(getUtility(IExtFileStore).getFileByContext( 180 view.context, attr=viewlet.download_name), 'name', None) 181 f_text = Paragraph('[Yet not provided]', ENTRY1_STYLE) 182 if img_path is None: 183 pass 184 elif not img_path.endswith('.jpg'): 185 # reportlab requires jpg images, I think. 186 f_text = Paragraph('%s (Not displayable)' % ( 187 viewlet.title,), ENTRY1_STYLE) 188 else: 189 f_text = Image(img.name, width=2*cm, height=1*cm, kind='bound') 190 table_data.append([f_label, f_text]) 191 if table_data: 192 # safety belt; empty tables lead to problems. 193 data.append(Table(table_data, style=SLIP_STYLE)) 194 return data 154 195 155 196 def insert_footer(pdf,width,style,text=None, number_of_pages=1): … … 237 278 return 238 279 end_level = certificate.end_level 239 if entry_session == grok.getSite()['configuration'].accommodation_session: 280 if entry_session == grok.getSite()[ 281 'configuration'].accommodation_session: 240 282 bt = 'fr' 241 283 elif current_level >= end_level: … … 263 305 """Render pdf slips for various pages. 264 306 """ 265 # (0,0),(-1,-1) = whole table 266 # (0,0),(0,-1) = first column 267 # (-1,0),(-1,-1) = last column 268 # (0,0),(-1,0) = first row 269 # (0,-1),(-1,-1) = last row 270 271 pdf = canvas.Canvas(filename,pagesize=A4) 272 pdf.setTitle(view.label) 273 pdf.setSubject(view.title) 274 pdf.setAuthor('%s (%s)' % (view.request.principal.title, 275 view.request.principal.id)) 276 pdf.setCreator('WAeUP Kofa') 277 width, height = A4 307 # XXX: we have to fix the import problems here. 308 from waeup.kofa.browser.interfaces import IPDFCreator 309 from waeup.kofa.browser.pdf import NORMAL_STYLE, ENTRY1_STYLE 310 style = getSampleStyleSheet() 311 creator = getUtility(IPDFCreator) 312 data = [] 313 doc_title = view.label 314 author = '%s (%s)' % (view.request.principal.title, 315 view.request.principal.id) 278 316 footer_text = view.label 279 317 if getattr(student, 'student_id', None) is not None: 280 318 footer_text = "%s - %s - " % (student.student_id, footer_text) 281 style = getSampleStyleSheet()282 style.add(ParagraphStyle(name='Right', alignment=TA_RIGHT))283 pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm))284 285 story = []286 frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm))287 header_title = getattr(grok.getSite(), 'name', u'Sample University')288 story.append(Paragraph(header_title, style["Heading1"]))289 frame_header.addFromList(story,pdf)290 319 291 320 # Insert student data table 292 story = []293 frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm))294 story.append(Paragraph(view.label, style["Heading2"]))295 321 portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE 296 322 if student is not None: 297 #story.append(Spacer(1, 12)) 298 bd_translation = translate(_('Base Data'), 299 'waeup.kofa', target_language=portal_language) 300 story.append(Paragraph(bd_translation, style["Heading3"])) 301 studenttable = render_student_data(studentview) 302 story.append(studenttable) 323 bd_translation = trans(_('Base Data'), portal_language) 324 data.append(Paragraph(bd_translation, style["Heading3"])) 325 data.append(render_student_data(studentview)) 303 326 304 327 # Insert widgets 305 story.append(Paragraph(view.title, style["Heading3"])) 306 set_up_widgets(view) 307 data = [] 328 data.append(Paragraph(view.title, style["Heading3"])) 308 329 portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE 309 for widget in view.widgets: 310 f_label = '<font size=12>%s</font>:' % translate( 311 widget.label.strip(), 'waeup.kofa', 312 target_language=portal_language) 313 f_label = Paragraph(f_label, style["Normal"]) 314 f_text = formatted_text(widget(), size=12) 315 f_text = Paragraph(f_text, style["Normal"]) 316 data.append([f_label,f_text]) 317 table = Table(data,style=SLIP_STYLE, colWidths=[5*cm, 13.5*cm]) 318 story.append(table) 319 320 # Import browser components locally 321 # to avoid circular import 322 from waeup.kofa.students.viewlets import FileManager 323 from waeup.kofa.browser import DEFAULT_IMAGE_PATH 324 data = [] 325 # Collect viewlets 326 fm = FileManager(view.context, view.request, view) 327 fm.update() 328 if fm.viewlets: 329 sc_translation = translate(_('Scanned Documents'), 'waeup.kofa', 330 target_language=portal_language) 331 story.append(Paragraph(sc_translation, style["Heading3"])) 332 # Insert list of scanned documents 333 for viewlet in fm.viewlets: 334 f_label = formatted_label(size=12) % translate( 335 viewlet.label, 'waeup.kofa', 336 target_language=portal_language) 337 f_label = Paragraph(f_label, style["Normal"]) 338 if viewlet.template.__grok_name__ == 'imagedisplay': 339 # In case we define FileDisplay viewlets with 340 # an imagedisplay template in the customization package 341 img = getUtility(IExtFileStore).getFileByContext( 342 view.context, attr=viewlet.download_name) 343 if img is None: 344 img = open(DEFAULT_IMAGE_PATH, 'rb') 345 doc_img = Image(img.name, width=2*cm, height=1*cm, kind='bound') 346 data.append([f_label,doc_img]) 347 else: 348 f_text = formatted_text(viewlet.title, size=12) 349 f_text = Paragraph(f_text, style["Normal"]) 350 data.append([f_label,f_text]) 351 table = Table(data,style=SLIP_STYLE, colWidths=[5*cm, 13.5*cm]) 352 story.append(table) 353 330 table = creator.getWidgetsTable( 331 view.form_fields, view.context, None, lang=portal_language) 332 data.append(table) 333 334 # Insert scanned docs 335 data.extend(docs_as_flowables(view, portal_language)) 336 337 # Insert content table on second page 338 if tabledata and tableheader: 339 data.append(PageBreak()) 340 data.append(Paragraph(view.content_title, style["Heading3"])) 341 contenttable = render_table_data(tableheader,tabledata) 342 data.append(contenttable) 343 344 view.response.setHeader( 345 'Content-Type', 'application/pdf') 354 346 try: 355 frame_body.addFromList(story,pdf) 347 pdf_stream = creator.create_pdf( 348 data, None, doc_title, author=author, footer=footer_text) 356 349 except IOError: 357 350 view.flash('Error in image file.') 358 351 return view.redirect(view.url(view.context)) 359 360 if tabledata and tableheader: 361 insert_footer(pdf,width,style,footer_text,number_of_pages=2) 362 else: 363 insert_footer(pdf,width,style,footer_text) 364 365 # Insert content table on second page 366 if tabledata and tableheader: 367 pdf.showPage() 368 frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(2*cm)) 369 story = [] 370 story.append(Paragraph(view.content_title, style["Heading3"])) 371 #story.append(Spacer(1, 12)) 372 contenttable = render_table_data(tableheader,tabledata) 373 story.append(contenttable) 374 frame_body.addFromList(story,pdf) 375 insert_footer(pdf,width,style,footer_text,number_of_pages=2) 376 377 view.response.setHeader( 378 'Content-Type', 'application/pdf') 379 return pdf.getpdfdata() 352 return pdf_stream 380 353 381 354 VERDICTS_DICT = {
Note: See TracChangeset for help on using the changeset viewer.