Changeset 8059 for main/waeup.kofa/trunk/src/waeup/kofa/applicants
- Timestamp:
- 7 Apr 2012, 08:15:16 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/applicants/pdf.py
r8050 r8059 20 20 """ 21 21 import grok 22 from datetime import datetime 23 from reportlab.pdfgen import canvas 22 from reportlab.lib.styles import getSampleStyleSheet 24 23 from reportlab.lib.units import cm 25 from reportlab.lib.pagesizes import A4 26 from reportlab.lib.styles import getSampleStyleSheet 27 from reportlab.platypus import Frame, Paragraph, Image, Table, Spacer 28 from reportlab.platypus.tables import TableStyle 29 from zope.i18n import translate 24 from reportlab.platypus import Paragraph, Image, Table, Spacer 30 25 from zope.component import getUtility 31 26 from zope.formlib.form import setUpEditWidgets 27 from zope.i18n import translate 32 28 from zope.publisher.browser import TestRequest 33 from waeup.kofa.widgets.datewidget import FriendlyDateDisplayWidget34 29 from waeup.kofa.applicants.interfaces import IApplicant, IApplicantsUtils 35 30 from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH 31 from waeup.kofa.browser.interfaces import IPDFCreator 36 32 from waeup.kofa.interfaces import IExtFileStore, IPDF, IKofaUtils 37 33 from waeup.kofa.interfaces import MessageFactory as _ 38 39 SLIP_STYLE = TableStyle( 40 [('VALIGN',(0,0),(-1,-1),'TOP')] 41 ) 42 34 from waeup.kofa.widgets.datewidget import FriendlyDateDisplayWidget 43 35 44 36 class PDFApplicationSlip(grok.Adapter): 45 37 """Create a PDF application slip for applicants. 38 """ 39 # XXX: Many things in here are reusable. We might want to split 40 # things. Possibly move parts to IPDFCreator? 46 41 grok.context(IApplicant) 47 42 grok.implements(IPDF) … … 129 124 return data 130 125 131 def __call__(self, view=None): 132 """Return a PDF representation of the context applicant. 133 134 If no `view` is given, the course admitted field will be an 135 empty string and author will be set as ``'unknown'``. 136 137 If a `view` is given, author will be set as the calling 138 principal. 139 """ 140 pdf = canvas.Canvas('application_slip.pdf',pagesize=A4) 141 pdf.setTitle(self.title) 142 pdf.setSubject('Application') 143 if view is None: 144 pdf.setAuthor('unknown') 145 if view is not None: 146 pdf.setAuthor('%s (%s)' % (view.request.principal.title, 147 view.request.principal.id)) 148 pdf.setCreator('Kofa') 149 width, height = A4 150 style = getSampleStyleSheet() 151 pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm)) 152 153 story = [] 154 frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm)) 155 header_title = getattr(grok.getSite(), 'name', u'Sample University') 156 story.append(Paragraph(header_title, style["Heading1"])) 157 frame_header.addFromList(story,pdf) 158 159 story = [] 160 frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm)) 161 story.append(Paragraph(self.title, style["Heading2"])) 162 story.append(Spacer(1, 18)) 163 for msg in self.context.history.messages: 164 f_msg = '<font face="Courier" size=10>%s</font>' % msg 165 story.append(Paragraph(f_msg, style["Normal"])) 166 story.append(Spacer(1, 24)) 167 168 # Setup table data 169 data = [] 170 # Insert passport photograph 171 data = self._addPassportImage(data) 172 # Render widget fields 126 def _getWidgetsTable(self, view): 127 """Return a reportlab table buildt of widgets. 128 """ 129 style = getSampleStyleSheet() 130 table_data = [] 131 table_style = [('LEFTPADDING', (0,0), (0,-1), 0), 132 ('VALIGN', (0,0), (-1,-1), 'TOP'), 133 ] 134 row_num = 0 173 135 widgets = self._setUpWidgets() 174 136 portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE 175 for widget in widgets: # self.widgets:137 for widget in widgets: 176 138 separators = getUtility(IApplicantsUtils).SEPARATORS_DICT 177 139 if separators and separators.get(widget.name): … … 180 142 target_language=portal_language)) 181 143 f_headline = Paragraph(f_headline, style["Normal"]) 182 data.append([f_headline ]) 144 table_data.append([f_headline ]) 145 table_style.append(('SPAN', (0,row_num), (-1,row_num)),) 146 table_style.append( 147 ('TOPPADDING', (0,row_num), (-1,row_num), 12),) 148 row_num += 1 183 149 f_label = '<font size=12>%s</font>:' % translate( 184 150 widget.label.strip(), 'waeup.kofa', … … 191 157 f_text = f_text.replace('\n', '') 192 158 f_text = Paragraph(f_text, style["Normal"]) 193 data.append([f_label,f_text]) 159 table_data.append([f_label,f_text]) 160 row_num += 1 194 161 adm_translation = translate(_('Admitted Course of Study:'), 195 162 'waeup.kofa', target_language=portal_language) … … 199 166 f_label = Paragraph(f_label, style["Normal"]) 200 167 f_text = Paragraph(f_text, style["Normal"]) 201 data.append([f_label,f_text]) 168 table_data.append([f_label,f_text]) 169 row_num += 1 202 170 203 171 # Add dept. and faculty if applicable 204 data = self._addDeptAndFaculty(data)172 table_data = self._addDeptAndFaculty(table_data) 205 173 206 174 # Create table 207 table = Table(data,style=SLIP_STYLE, colWidths=[10*cm, 8.5*cm]) 208 story.append(table) 209 210 # Add some comments 175 table = Table(table_data,style=table_style) 176 table.hAlign = 'LEFT' 177 return table 178 179 def _addComments(self, data): 180 style = getSampleStyleSheet() 211 181 if self.context.state == 'created': 212 182 comment1 = _( … … 214 184 ' and enter your new credentials:' + 215 185 ' user name= ${a}, password = ${b}.</font>', mapping = { 216 'a':self.context.student_id, 'b':self.context.application_number} 186 'a':self.context.student_id, 187 'b':self.context.application_number} 217 188 ) 218 189 comment2 = _( … … 222 193 comment1 = Paragraph(comment1, style["Normal"]) 223 194 comment2 = Paragraph(comment2, style["Normal"]) 224 story.append(Spacer(1, 18)) 225 story.append(comment1) 226 story.append(comment2) 227 228 frame_body.addFromList(story,pdf) 229 230 story = [] 231 frame_footer = Frame(1*cm,0,width-(2*cm),1*cm) 232 timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") 233 f_text = '<font size=10>%s</font>' % timestamp 234 story.append(Paragraph(f_text, style["Normal"])) 235 frame_footer.addFromList(story,pdf) 236 return pdf.getpdfdata() 195 data.extend([Spacer(1, 18), comment1, comment2]) 196 return data 197 198 def __call__(self, view=None): 199 """Return a PDF representation of the context applicant. 200 201 If no `view` is given, the course admitted field will be an 202 empty string and author will be set as ``'unknown'``. 203 204 If a `view` is given, author will be set as the calling 205 principal. 206 """ 207 doc_title = self.title.replace('-', '\n') 208 style = getSampleStyleSheet() 209 data = [] 210 211 # append history 212 for msg in self.context.history.messages: 213 f_msg = '<font face="Courier" size=10>%s</font>' % msg 214 data.append(Paragraph(f_msg, style["Normal"])) 215 data.append(Spacer(1, 20)) 216 217 # append photograph 218 img_path = getattr( 219 getUtility(IExtFileStore).getFileByContext(self.context), 220 'name', DEFAULT_PASSPORT_IMAGE_PATH) 221 doc_img = Image(img_path, width=4*cm, height=3*cm, kind='bound') 222 doc_img.hAlign='LEFT' 223 data.append(doc_img) 224 225 # append widgets 226 data.append(self._getWidgetsTable(view)) 227 228 # append comments 229 data = self._addComments(data) 230 231 creator = getUtility(IPDFCreator) 232 return creator.create_pdf(data, None, doc_title)
Note: See TracChangeset for help on using the changeset viewer.