## $Id: pdf.py 8091 2012-04-10 10:43:50Z 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.lib.styles import getSampleStyleSheet from reportlab.lib.units import cm from reportlab.platypus import Paragraph, Image, Table, Spacer from zope.component import getUtility from zope.formlib.form import setUpEditWidgets from zope.i18n import translate from zope.publisher.browser import TestRequest 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.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') 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): style = getSampleStyleSheet() if self.context.state == 'created': comment1 = _( 'Proceed to the login page of the portal' + ' and enter your new credentials:' + ' user name= ${a}, password = ${b}.', mapping = { 'a':self.context.student_id, 'b':self.context.application_number} ) comment2 = _( 'Change your password when you have ' + ' logged in.' ) comment1 = Paragraph(comment1, style["Normal"]) comment2 = Paragraph(comment2, style["Normal"]) data.extend([Spacer(1, 18), comment1, comment2]) return data def __call__(self, view=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('-')]) style = getSampleStyleSheet() data = [] portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE separators = getUtility(IApplicantsUtils).SEPARATORS_DICT creator = getUtility(IPDFCreator) # append history data.extend(creator.fromStringList(self.context.history.messages)) # 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)