[7415] | 1 | ## $Id: pdf.py 14065 2016-08-09 19:17:24Z 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 |
---|
[10310] | 22 | from reportlab.platypus import Paragraph, Spacer, Table |
---|
| 23 | from reportlab.lib.units import cm |
---|
[7390] | 24 | from zope.component import getUtility |
---|
[8059] | 25 | from zope.i18n import translate |
---|
[8046] | 26 | from waeup.kofa.applicants.interfaces import IApplicant, IApplicantsUtils |
---|
[7811] | 27 | from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH |
---|
[8059] | 28 | from waeup.kofa.browser.interfaces import IPDFCreator |
---|
[10596] | 29 | from waeup.kofa.browser.pdf import SMALL_PARA_STYLE, ENTRY1_STYLE, get_qrcode |
---|
[7819] | 30 | from waeup.kofa.interfaces import IExtFileStore, IPDF, IKofaUtils |
---|
[7811] | 31 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[8059] | 32 | from waeup.kofa.widgets.datewidget import FriendlyDateDisplayWidget |
---|
[7390] | 33 | |
---|
[10310] | 34 | SLIP_STYLE = [ |
---|
| 35 | ('VALIGN',(0,0),(-1,-1),'TOP'), |
---|
| 36 | #('FONT', (0,0), (-1,-1), 'Helvetica', 11), |
---|
| 37 | ] |
---|
| 38 | |
---|
[7390] | 39 | class PDFApplicationSlip(grok.Adapter): |
---|
[8059] | 40 | """Create a PDF application slip for applicants. |
---|
| 41 | """ |
---|
| 42 | # XXX: Many things in here are reusable. We might want to split |
---|
| 43 | # things. Possibly move parts to IPDFCreator? |
---|
[7390] | 44 | grok.context(IApplicant) |
---|
| 45 | grok.implements(IPDF) |
---|
| 46 | grok.name('application_slip') |
---|
| 47 | |
---|
| 48 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
[10310] | 49 | 'locked', 'course_admitted', 'suspended', |
---|
| 50 | ) |
---|
[8013] | 51 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
[10310] | 52 | column_two_fields = ('applicant_id', 'reg_number', |
---|
| 53 | 'firstname', 'middlename', 'lastname') |
---|
[10319] | 54 | two_columns_design_fields = [] |
---|
[7390] | 55 | |
---|
| 56 | @property |
---|
[11869] | 57 | def note(self): |
---|
[11871] | 58 | note = getattr(self.context.__parent__, 'application_slip_notice', None) |
---|
[11872] | 59 | if note: |
---|
| 60 | return '<br /><br />' + note |
---|
| 61 | return |
---|
[11869] | 62 | |
---|
| 63 | @property |
---|
[10222] | 64 | def target(self): |
---|
| 65 | return getattr(self.context.__parent__, 'prefix', None) |
---|
| 66 | |
---|
| 67 | @property |
---|
[7390] | 68 | def title(self): |
---|
| 69 | container_title = self.context.__parent__.title |
---|
[7819] | 70 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7714] | 71 | ar_translation = translate(_('Application Record'), |
---|
[7811] | 72 | 'waeup.kofa', target_language=portal_language) |
---|
[7714] | 73 | return '%s - %s %s' % (container_title, |
---|
| 74 | ar_translation, self.context.application_number) |
---|
[7390] | 75 | |
---|
| 76 | def _getCourseAdmittedLink(self, view): |
---|
| 77 | """Return link, title and code in html format to the certificate |
---|
| 78 | admitted. |
---|
| 79 | """ |
---|
| 80 | course_admitted = self.context.course_admitted |
---|
| 81 | if view is not None and getattr(course_admitted, '__parent__',None): |
---|
| 82 | url = view.url(course_admitted) |
---|
| 83 | title = course_admitted.title |
---|
| 84 | code = course_admitted.code |
---|
| 85 | return '<a href="%s">%s - %s</a>' %(url,code,title) |
---|
| 86 | return '' |
---|
| 87 | |
---|
[8091] | 88 | def _getDeptAndFaculty(self): |
---|
| 89 | """Return long titles of department and faculty. |
---|
[7390] | 90 | |
---|
[8091] | 91 | Returns a list [department_title, faculty_title] |
---|
[7390] | 92 | |
---|
[8091] | 93 | If the context applicant has no course admitted or dept. and |
---|
| 94 | faculty cannot be determined, ``None`` is returned. |
---|
[7390] | 95 | """ |
---|
| 96 | course_admitted = self.context.course_admitted |
---|
| 97 | dept = getattr( |
---|
| 98 | getattr(course_admitted, '__parent__', None), |
---|
| 99 | '__parent__', None) |
---|
| 100 | faculty = getattr(dept, '__parent__', None) |
---|
[10650] | 101 | return [x is not None and x.longtitle or x for x in dept, faculty] |
---|
[7390] | 102 | |
---|
[10329] | 103 | def _addLoginInformation(self): |
---|
[8286] | 104 | if self.context.state == 'created': |
---|
[8313] | 105 | comment = translate(_( |
---|
[10329] | 106 | '\n Proceed to the login page of the portal' + |
---|
[7409] | 107 | ' and enter your new credentials:' + |
---|
[8313] | 108 | ' user name= ${a}, password = ${b}. ' + |
---|
| 109 | 'Change your password when you have logged in.', |
---|
| 110 | mapping = { |
---|
[8059] | 111 | 'a':self.context.student_id, |
---|
| 112 | 'b':self.context.application_number} |
---|
[8313] | 113 | )) |
---|
[10330] | 114 | return comment |
---|
| 115 | return |
---|
[7409] | 116 | |
---|
[9949] | 117 | def _getPDFCreator(self): |
---|
| 118 | return getUtility(IPDFCreator) |
---|
| 119 | |
---|
[8552] | 120 | def __call__(self, view=None, note=None): |
---|
[8059] | 121 | """Return a PDF representation of the context applicant. |
---|
[7390] | 122 | |
---|
[8059] | 123 | If no `view` is given, the course admitted field will be an |
---|
| 124 | empty string and author will be set as ``'unknown'``. |
---|
| 125 | |
---|
| 126 | If a `view` is given, author will be set as the calling |
---|
| 127 | principal. |
---|
| 128 | """ |
---|
[8095] | 129 | doc_title = '\n'.join([x.strip() for x in self.title.split(' - ')]) |
---|
[8059] | 130 | data = [] |
---|
[14065] | 131 | topMargin = 1.5 |
---|
[14063] | 132 | if len(self.title.split(' - ')) > 2: |
---|
[14065] | 133 | topMargin = 1.8 |
---|
[8091] | 134 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 135 | separators = getUtility(IApplicantsUtils).SEPARATORS_DICT |
---|
[9949] | 136 | creator = self._getPDFCreator() |
---|
[8059] | 137 | |
---|
| 138 | # append history |
---|
[8091] | 139 | data.extend(creator.fromStringList(self.context.history.messages)) |
---|
[10333] | 140 | data.append(Spacer(1, 5)) |
---|
[8059] | 141 | |
---|
[10596] | 142 | # create three-column header table |
---|
[10310] | 143 | # append photograph to the left |
---|
[8059] | 144 | img_path = getattr( |
---|
| 145 | getUtility(IExtFileStore).getFileByContext(self.context), |
---|
| 146 | 'name', DEFAULT_PASSPORT_IMAGE_PATH) |
---|
[10319] | 147 | data_left = [[creator.getImage(img_path)]] |
---|
[10310] | 148 | table_left = Table(data_left,style=SLIP_STYLE) |
---|
[10319] | 149 | |
---|
[10596] | 150 | # append base data table to the middle |
---|
[10310] | 151 | fields = [ |
---|
| 152 | field for field in self.form_fields |
---|
| 153 | if field.__name__ in self.column_two_fields] |
---|
[10596] | 154 | table_middle = creator.getWidgetsTable( |
---|
[10310] | 155 | fields, self.context, None, lang=portal_language, |
---|
[10596] | 156 | separators=None, colWidths=[5*cm, 4.5*cm]) |
---|
| 157 | |
---|
| 158 | # append QR code to the right |
---|
| 159 | if view is not None: |
---|
| 160 | url = view.url(self.context, 'application_slip.pdf') |
---|
| 161 | data_right = [[get_qrcode(url, width=70.0)]] |
---|
| 162 | table_right = Table(data_right,style=SLIP_STYLE) |
---|
| 163 | else: |
---|
| 164 | table_right = None |
---|
| 165 | |
---|
| 166 | header_table = Table( |
---|
| 167 | [[table_left, table_middle, table_right],],style=SLIP_STYLE) |
---|
[10310] | 168 | data.append(header_table) |
---|
| 169 | |
---|
| 170 | # append widgets except those already added in column two |
---|
[10329] | 171 | # and those in two_columns_design_fields |
---|
[8091] | 172 | dept, faculty = self._getDeptAndFaculty() |
---|
[10310] | 173 | fields = [ |
---|
| 174 | field for field in self.form_fields |
---|
[10319] | 175 | if not field.__name__ in self.column_two_fields and |
---|
| 176 | not field.__name__ in self.two_columns_design_fields] |
---|
[8091] | 177 | data.append(creator.getWidgetsTable( |
---|
[10310] | 178 | fields, self.context, view, lang=portal_language, |
---|
[8091] | 179 | domain='waeup.kofa', separators=separators, |
---|
| 180 | course_label='Admitted Course of Study:', |
---|
| 181 | course_link=self._getCourseAdmittedLink(view), |
---|
| 182 | dept=dept, faculty=faculty)) |
---|
[8059] | 183 | |
---|
[10329] | 184 | # append two-column table of widgets of those fields defined in |
---|
| 185 | # two_columns_design_fields |
---|
| 186 | if len(self.two_columns_design_fields): |
---|
| 187 | fields = [ |
---|
| 188 | field for field in self.form_fields |
---|
| 189 | if not field.__name__ in self.column_two_fields and |
---|
| 190 | field.__name__ in self.two_columns_design_fields] |
---|
| 191 | if fields: |
---|
| 192 | data.append(creator.getWidgetsTable( |
---|
| 193 | fields, self.context, view, lang=portal_language, |
---|
| 194 | domain='waeup.kofa', separators=separators, |
---|
| 195 | twoDataCols=True)) |
---|
[9948] | 196 | |
---|
[10329] | 197 | # append login information |
---|
[10332] | 198 | note = None |
---|
| 199 | login_information = self._addLoginInformation() |
---|
| 200 | if not None in (self.note, login_information): |
---|
| 201 | note = self.note + login_information |
---|
| 202 | elif self.note: |
---|
| 203 | note = self.note |
---|
| 204 | elif login_information: |
---|
| 205 | note = login_information |
---|
[10329] | 206 | |
---|
[14065] | 207 | return creator.create_pdf(data, None, doc_title, note=note, |
---|
| 208 | topMargin=topMargin) |
---|