[7415] | 1 | ## $Id: pdf.py 8013 2012-04-02 09:18:53Z henrik $ |
---|
[7390] | 2 | ## |
---|
[7398] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[7390] | 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
| 18 | """ |
---|
| 19 | Generate PDF docs for applicants. |
---|
| 20 | """ |
---|
| 21 | import grok |
---|
| 22 | from datetime import datetime |
---|
[7714] | 23 | from zope.component import getUtility |
---|
| 24 | from zope.i18n import translate |
---|
[7390] | 25 | from reportlab.pdfgen import canvas |
---|
| 26 | from reportlab.lib.units import cm |
---|
| 27 | from reportlab.lib.pagesizes import A4 |
---|
| 28 | from reportlab.lib.styles import getSampleStyleSheet |
---|
| 29 | from reportlab.platypus import Frame, Paragraph, Image, Table, Spacer |
---|
| 30 | from reportlab.platypus.tables import TableStyle |
---|
| 31 | from zope.component import getUtility |
---|
| 32 | from zope.formlib.form import setUpEditWidgets |
---|
| 33 | from zope.publisher.browser import TestRequest |
---|
[8013] | 34 | from waeup.kofa.widgets.datewidget import FriendlyDateDisplayWidget |
---|
[7811] | 35 | from waeup.kofa.applicants.interfaces import IApplicant |
---|
| 36 | from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH |
---|
[7819] | 37 | from waeup.kofa.interfaces import IExtFileStore, IPDF, IKofaUtils |
---|
[7811] | 38 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7390] | 39 | |
---|
| 40 | SLIP_STYLE = TableStyle( |
---|
| 41 | [('VALIGN',(0,0),(-1,-1),'TOP')] |
---|
| 42 | ) |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | class PDFApplicationSlip(grok.Adapter): |
---|
| 46 | |
---|
| 47 | grok.context(IApplicant) |
---|
| 48 | grok.implements(IPDF) |
---|
| 49 | grok.name('application_slip') |
---|
| 50 | |
---|
| 51 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
| 52 | 'locked', 'course_admitted') |
---|
[8013] | 53 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
[7390] | 54 | |
---|
| 55 | @property |
---|
| 56 | def title(self): |
---|
| 57 | container_title = self.context.__parent__.title |
---|
[7819] | 58 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7714] | 59 | ar_translation = translate(_('Application Record'), |
---|
[7811] | 60 | 'waeup.kofa', target_language=portal_language) |
---|
[7714] | 61 | return '%s - %s %s' % (container_title, |
---|
| 62 | ar_translation, self.context.application_number) |
---|
[7390] | 63 | |
---|
| 64 | def _setUpWidgets(self): |
---|
| 65 | """Setup simple display widgets. |
---|
| 66 | |
---|
| 67 | Returns the list of widgets. |
---|
| 68 | """ |
---|
| 69 | request = TestRequest() |
---|
| 70 | return setUpEditWidgets( |
---|
| 71 | self.form_fields, 'form', self.context, request, {}, |
---|
| 72 | for_display=True, ignore_request=True |
---|
| 73 | ) |
---|
| 74 | |
---|
| 75 | def _getCourseAdmittedLink(self, view): |
---|
| 76 | """Return link, title and code in html format to the certificate |
---|
| 77 | admitted. |
---|
| 78 | """ |
---|
| 79 | course_admitted = self.context.course_admitted |
---|
| 80 | if view is not None and getattr(course_admitted, '__parent__',None): |
---|
| 81 | url = view.url(course_admitted) |
---|
| 82 | title = course_admitted.title |
---|
| 83 | code = course_admitted.code |
---|
| 84 | return '<a href="%s">%s - %s</a>' %(url,code,title) |
---|
| 85 | return '' |
---|
| 86 | |
---|
| 87 | def _addPassportImage(self, data): |
---|
| 88 | """Add passport image to data stream. |
---|
| 89 | |
---|
| 90 | If no image exists yet the default image is added. |
---|
| 91 | """ |
---|
| 92 | img = getUtility(IExtFileStore).getFileByContext(self.context) |
---|
| 93 | if img is None: |
---|
| 94 | img = open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb') |
---|
| 95 | doc_img = Image(img.name, width=4*cm, height=3*cm, kind='bound') |
---|
| 96 | data.append([doc_img]) |
---|
| 97 | data.append([Spacer(1, 18)]) |
---|
| 98 | return data |
---|
| 99 | |
---|
| 100 | def _addDeptAndFaculty(self, data): |
---|
| 101 | """If we have a valid course admitted, add dept. and faculty to data |
---|
| 102 | stream. |
---|
| 103 | """ |
---|
| 104 | style = getSampleStyleSheet() |
---|
| 105 | course_admitted = self.context.course_admitted |
---|
| 106 | dept = getattr( |
---|
| 107 | getattr(course_admitted, '__parent__', None), |
---|
| 108 | '__parent__', None) |
---|
| 109 | if dept is None: |
---|
| 110 | return data |
---|
[7819] | 111 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7714] | 112 | dp_translation = translate(_('Department:'), |
---|
[7811] | 113 | 'waeup.kofa', target_language=portal_language) |
---|
[7714] | 114 | f_label = '<font size=12>%s</font>' % dp_translation |
---|
[7390] | 115 | f_text = '<font size=12>%s</font>' % dept.longtitle() |
---|
| 116 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 117 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 118 | data.append([f_label,f_text]) |
---|
| 119 | |
---|
| 120 | faculty = getattr(dept, '__parent__', None) |
---|
| 121 | if faculty is None: |
---|
| 122 | return data |
---|
[7714] | 123 | fc_translation = translate(_('Faculty:'), |
---|
[7811] | 124 | 'waeup.kofa', target_language=portal_language) |
---|
[7714] | 125 | f_label = '<font size=12>%s</font>' % fc_translation |
---|
[7390] | 126 | f_text = '<font size=12>%s</font>' % faculty.longtitle() |
---|
| 127 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 128 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 129 | data.append([f_label,f_text]) |
---|
| 130 | return data |
---|
| 131 | |
---|
| 132 | def __call__(self, view=None): |
---|
| 133 | """Return a PDF representation of the context applicant. |
---|
| 134 | |
---|
| 135 | If no `view` is given, the course admitted field will be an |
---|
| 136 | empty string and author will be set as ``'unknown'``. |
---|
| 137 | |
---|
| 138 | If a `view` is given, author will be set as the calling |
---|
| 139 | principal. |
---|
| 140 | """ |
---|
| 141 | pdf = canvas.Canvas('application_slip.pdf',pagesize=A4) |
---|
| 142 | pdf.setTitle(self.title) |
---|
| 143 | pdf.setSubject('Application') |
---|
| 144 | if view is None: |
---|
| 145 | pdf.setAuthor('unknown') |
---|
| 146 | if view is not None: |
---|
| 147 | pdf.setAuthor('%s (%s)' % (view.request.principal.title, |
---|
[7714] | 148 | view.request.principal.id)) |
---|
[7819] | 149 | pdf.setCreator('Kofa') |
---|
[7390] | 150 | width, height = A4 |
---|
| 151 | style = getSampleStyleSheet() |
---|
| 152 | pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm)) |
---|
| 153 | |
---|
| 154 | story = [] |
---|
| 155 | frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm)) |
---|
| 156 | header_title = getattr(grok.getSite(), 'name', u'Sample University') |
---|
| 157 | story.append(Paragraph(header_title, style["Heading1"])) |
---|
| 158 | frame_header.addFromList(story,pdf) |
---|
| 159 | |
---|
| 160 | story = [] |
---|
| 161 | frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm)) |
---|
| 162 | story.append(Paragraph(self.title, style["Heading2"])) |
---|
| 163 | story.append(Spacer(1, 18)) |
---|
| 164 | for msg in self.context.history.messages: |
---|
| 165 | f_msg = '<font face="Courier" size=10>%s</font>' % msg |
---|
| 166 | story.append(Paragraph(f_msg, style["Normal"])) |
---|
| 167 | story.append(Spacer(1, 24)) |
---|
| 168 | |
---|
| 169 | # Setup table data |
---|
| 170 | data = [] |
---|
| 171 | # Insert passport photograph |
---|
| 172 | data = self._addPassportImage(data) |
---|
| 173 | # Render widget fields |
---|
| 174 | widgets = self._setUpWidgets() |
---|
[7819] | 175 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7390] | 176 | for widget in widgets: # self.widgets: |
---|
[7714] | 177 | f_label = '<font size=12>%s</font>:' % translate( |
---|
[7811] | 178 | widget.label.strip(), 'waeup.kofa', |
---|
[7714] | 179 | target_language=portal_language) |
---|
[7390] | 180 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 181 | f_text = '<font size=12>%s</font>' % widget() |
---|
[7804] | 182 | # Add br tag if widgets contain div tags |
---|
| 183 | # which are not supported by reportlab |
---|
[7835] | 184 | f_text = f_text.replace('</div>', '</div><br />') |
---|
| 185 | f_text = f_text.replace('\n', '') |
---|
[7390] | 186 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 187 | data.append([f_label,f_text]) |
---|
[7714] | 188 | adm_translation = translate(_('Admitted Course of Study:'), |
---|
[7811] | 189 | 'waeup.kofa', target_language=portal_language) |
---|
[7714] | 190 | f_label = '<font size=12>%s</font>' % adm_translation |
---|
[7390] | 191 | f_text = '<font size=12>%s</font>' % ( |
---|
| 192 | self._getCourseAdmittedLink(view),) |
---|
| 193 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 194 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 195 | data.append([f_label,f_text]) |
---|
| 196 | |
---|
| 197 | # Add dept. and faculty if applicable |
---|
| 198 | data = self._addDeptAndFaculty(data) |
---|
| 199 | |
---|
| 200 | # Create table |
---|
| 201 | table = Table(data,style=SLIP_STYLE) |
---|
| 202 | story.append(table) |
---|
[7409] | 203 | |
---|
| 204 | # Add some comments |
---|
| 205 | if self.context.state == 'created': |
---|
[7714] | 206 | comment1 = _( |
---|
[7409] | 207 | '<font size=10>Proceed to the login page of the portal' + |
---|
| 208 | ' and enter your new credentials:' + |
---|
[7714] | 209 | ' user name= ${a}, password = ${b}.</font>', mapping = { |
---|
| 210 | 'a':self.context.student_id, 'b':self.context.application_number} |
---|
[7409] | 211 | ) |
---|
[7714] | 212 | comment2 = _( |
---|
[7409] | 213 | '<font size=10>Change your password when you have ' + |
---|
| 214 | ' logged in.</font>' |
---|
| 215 | ) |
---|
| 216 | comment1 = Paragraph(comment1, style["Normal"]) |
---|
| 217 | comment2 = Paragraph(comment2, style["Normal"]) |
---|
| 218 | story.append(Spacer(1, 18)) |
---|
| 219 | story.append(comment1) |
---|
| 220 | story.append(comment2) |
---|
| 221 | |
---|
[7390] | 222 | frame_body.addFromList(story,pdf) |
---|
| 223 | |
---|
| 224 | story = [] |
---|
| 225 | frame_footer = Frame(1*cm,0,width-(2*cm),1*cm) |
---|
| 226 | timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") |
---|
| 227 | f_text = '<font size=10>%s</font>' % timestamp |
---|
| 228 | story.append(Paragraph(f_text, style["Normal"])) |
---|
| 229 | frame_footer.addFromList(story,pdf) |
---|
| 230 | return pdf.getpdfdata() |
---|