[7415] | 1 | ## $Id: pdf.py 8313 2012-04-29 20:40:39Z 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 |
---|
[8103] | 22 | from reportlab.platypus import Paragraph, Spacer |
---|
[7390] | 23 | from zope.component import getUtility |
---|
[8059] | 24 | from zope.i18n import translate |
---|
[8046] | 25 | from waeup.kofa.applicants.interfaces import IApplicant, IApplicantsUtils |
---|
[7811] | 26 | from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH |
---|
[8059] | 27 | from waeup.kofa.browser.interfaces import IPDFCreator |
---|
[8103] | 28 | from waeup.kofa.browser.pdf import SMALL_PARA_STYLE |
---|
[7819] | 29 | from waeup.kofa.interfaces import IExtFileStore, IPDF, IKofaUtils |
---|
[7811] | 30 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[8059] | 31 | from waeup.kofa.widgets.datewidget import FriendlyDateDisplayWidget |
---|
[7390] | 32 | |
---|
| 33 | class PDFApplicationSlip(grok.Adapter): |
---|
[8059] | 34 | """Create a PDF application slip for applicants. |
---|
| 35 | """ |
---|
| 36 | # XXX: Many things in here are reusable. We might want to split |
---|
| 37 | # things. Possibly move parts to IPDFCreator? |
---|
[7390] | 38 | grok.context(IApplicant) |
---|
| 39 | grok.implements(IPDF) |
---|
| 40 | grok.name('application_slip') |
---|
| 41 | |
---|
| 42 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
| 43 | 'locked', 'course_admitted') |
---|
[8013] | 44 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
[7390] | 45 | |
---|
| 46 | @property |
---|
| 47 | def title(self): |
---|
| 48 | container_title = self.context.__parent__.title |
---|
[7819] | 49 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7714] | 50 | ar_translation = translate(_('Application Record'), |
---|
[7811] | 51 | 'waeup.kofa', target_language=portal_language) |
---|
[7714] | 52 | return '%s - %s %s' % (container_title, |
---|
| 53 | ar_translation, self.context.application_number) |
---|
[7390] | 54 | |
---|
| 55 | def _getCourseAdmittedLink(self, view): |
---|
| 56 | """Return link, title and code in html format to the certificate |
---|
| 57 | admitted. |
---|
| 58 | """ |
---|
| 59 | course_admitted = self.context.course_admitted |
---|
| 60 | if view is not None and getattr(course_admitted, '__parent__',None): |
---|
| 61 | url = view.url(course_admitted) |
---|
| 62 | title = course_admitted.title |
---|
| 63 | code = course_admitted.code |
---|
| 64 | return '<a href="%s">%s - %s</a>' %(url,code,title) |
---|
| 65 | return '' |
---|
| 66 | |
---|
[8091] | 67 | def _getDeptAndFaculty(self): |
---|
| 68 | """Return long titles of department and faculty. |
---|
[7390] | 69 | |
---|
[8091] | 70 | Returns a list [department_title, faculty_title] |
---|
[7390] | 71 | |
---|
[8091] | 72 | If the context applicant has no course admitted or dept. and |
---|
| 73 | faculty cannot be determined, ``None`` is returned. |
---|
[7390] | 74 | """ |
---|
| 75 | course_admitted = self.context.course_admitted |
---|
| 76 | dept = getattr( |
---|
| 77 | getattr(course_admitted, '__parent__', None), |
---|
| 78 | '__parent__', None) |
---|
| 79 | faculty = getattr(dept, '__parent__', None) |
---|
[8091] | 80 | return [x is not None and x.longtitle() or x for x in dept, faculty] |
---|
[7390] | 81 | |
---|
[8059] | 82 | def _addComments(self, data): |
---|
[8286] | 83 | if self.context.state == 'created': |
---|
[8313] | 84 | comment = translate(_( |
---|
[8103] | 85 | 'Proceed to the login page of the portal' + |
---|
[7409] | 86 | ' and enter your new credentials:' + |
---|
[8313] | 87 | ' user name= ${a}, password = ${b}. ' + |
---|
| 88 | 'Change your password when you have logged in.', |
---|
| 89 | mapping = { |
---|
[8059] | 90 | 'a':self.context.student_id, |
---|
| 91 | 'b':self.context.application_number} |
---|
[8313] | 92 | )) |
---|
| 93 | comment = Paragraph(comment, SMALL_PARA_STYLE) |
---|
| 94 | data.extend([Spacer(1, 18), comment]) |
---|
[8059] | 95 | return data |
---|
[7409] | 96 | |
---|
[8257] | 97 | def __call__(self, view=None, note=None): |
---|
[8059] | 98 | """Return a PDF representation of the context applicant. |
---|
[7390] | 99 | |
---|
[8059] | 100 | If no `view` is given, the course admitted field will be an |
---|
| 101 | empty string and author will be set as ``'unknown'``. |
---|
| 102 | |
---|
| 103 | If a `view` is given, author will be set as the calling |
---|
| 104 | principal. |
---|
| 105 | """ |
---|
[8095] | 106 | doc_title = '\n'.join([x.strip() for x in self.title.split(' - ')]) |
---|
[8059] | 107 | data = [] |
---|
[8091] | 108 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 109 | separators = getUtility(IApplicantsUtils).SEPARATORS_DICT |
---|
| 110 | creator = getUtility(IPDFCreator) |
---|
[8059] | 111 | |
---|
| 112 | # append history |
---|
[8091] | 113 | data.extend(creator.fromStringList(self.context.history.messages)) |
---|
[8059] | 114 | |
---|
| 115 | # append photograph |
---|
| 116 | img_path = getattr( |
---|
| 117 | getUtility(IExtFileStore).getFileByContext(self.context), |
---|
| 118 | 'name', DEFAULT_PASSPORT_IMAGE_PATH) |
---|
[8091] | 119 | data.append(creator.getImage(img_path)) |
---|
| 120 | data.append(Spacer(1, 12)) |
---|
[8059] | 121 | |
---|
| 122 | # append widgets |
---|
[8091] | 123 | dept, faculty = self._getDeptAndFaculty() |
---|
| 124 | data.append(creator.getWidgetsTable( |
---|
| 125 | self.form_fields, self.context, view, lang=portal_language, |
---|
| 126 | domain='waeup.kofa', separators=separators, |
---|
| 127 | course_label='Admitted Course of Study:', |
---|
| 128 | course_link=self._getCourseAdmittedLink(view), |
---|
| 129 | dept=dept, faculty=faculty)) |
---|
[8059] | 130 | |
---|
| 131 | # append comments |
---|
| 132 | data = self._addComments(data) |
---|
[8257] | 133 | |
---|
| 134 | return creator.create_pdf(data, None, doc_title, note=note) |
---|