## $Id: pdf.py 9949 2013-02-14 16:32:40Z uli $ ## ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """ Generate PDF docs for applicants. """ import grok from reportlab.platypus import Paragraph, Spacer from zope.component import getUtility from zope.i18n import translate from waeup.kofa.applicants.interfaces import IApplicant, IApplicantsUtils from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH from waeup.kofa.browser.interfaces import IPDFCreator from waeup.kofa.browser.pdf import SMALL_PARA_STYLE from waeup.kofa.interfaces import IExtFileStore, IPDF, IKofaUtils from waeup.kofa.interfaces import MessageFactory as _ from waeup.kofa.widgets.datewidget import FriendlyDateDisplayWidget class PDFApplicationSlip(grok.Adapter): """Create a PDF application slip for applicants. """ # XXX: Many things in here are reusable. We might want to split # things. Possibly move parts to IPDFCreator? grok.context(IApplicant) grok.implements(IPDF) grok.name('application_slip') note = None form_fields = grok.AutoFields(IApplicant).omit( 'locked', 'course_admitted') form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') @property def title(self): container_title = self.context.__parent__.title portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE ar_translation = translate(_('Application Record'), 'waeup.kofa', target_language=portal_language) return '%s - %s %s' % (container_title, ar_translation, self.context.application_number) def _getCourseAdmittedLink(self, view): """Return link, title and code in html format to the certificate admitted. """ course_admitted = self.context.course_admitted if view is not None and getattr(course_admitted, '__parent__',None): url = view.url(course_admitted) title = course_admitted.title code = course_admitted.code return '%s - %s' %(url,code,title) return '' def _getDeptAndFaculty(self): """Return long titles of department and faculty. Returns a list [department_title, faculty_title] If the context applicant has no course admitted or dept. and faculty cannot be determined, ``None`` is returned. """ course_admitted = self.context.course_admitted dept = getattr( getattr(course_admitted, '__parent__', None), '__parent__', None) faculty = getattr(dept, '__parent__', None) return [x is not None and x.longtitle() or x for x in dept, faculty] def _addComments(self, data): if self.context.state == 'created': comment = translate(_( 'Proceed to the login page of the portal' + ' and enter your new credentials:' + ' user name= ${a}, password = ${b}. ' + 'Change your password when you have logged in.', mapping = { 'a':self.context.student_id, 'b':self.context.application_number} )) comment = Paragraph(comment, SMALL_PARA_STYLE) data.extend([Spacer(1, 18), comment]) return data def _getPDFCreator(self): return getUtility(IPDFCreator) def __call__(self, view=None, note=None): """Return a PDF representation of the context applicant. If no `view` is given, the course admitted field will be an empty string and author will be set as ``'unknown'``. If a `view` is given, author will be set as the calling principal. """ doc_title = '\n'.join([x.strip() for x in self.title.split(' - ')]) data = [] portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE separators = getUtility(IApplicantsUtils).SEPARATORS_DICT creator = self._getPDFCreator() # append history data.extend(creator.fromStringList(self.context.history.messages)) data.append(Spacer(1, 20)) # append photograph img_path = getattr( getUtility(IExtFileStore).getFileByContext(self.context), 'name', DEFAULT_PASSPORT_IMAGE_PATH) data.append(creator.getImage(img_path)) data.append(Spacer(1, 12)) # append widgets dept, faculty = self._getDeptAndFaculty() data.append(creator.getWidgetsTable( self.form_fields, self.context, view, lang=portal_language, domain='waeup.kofa', separators=separators, course_label='Admitted Course of Study:', course_link=self._getCourseAdmittedLink(view), dept=dept, faculty=faculty)) # append comments data = self._addComments(data) return creator.create_pdf(data, None, doc_title, note=self.note)