source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/pdf.py @ 10226

Last change on this file since 10226 was 10222, checked in by Henrik Bettermann, 11 years ago

Add target property also to PDFApplicationSlip.

  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1## $Id: pdf.py 10222 2013-05-24 07:36:28Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
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"""
19Generate PDF docs for applicants.
20"""
21import grok
22from reportlab.platypus import Paragraph, Spacer
23from zope.component import getUtility
24from zope.i18n import translate
25from waeup.kofa.applicants.interfaces import IApplicant, IApplicantsUtils
26from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH
27from waeup.kofa.browser.interfaces import IPDFCreator
28from waeup.kofa.browser.pdf import SMALL_PARA_STYLE
29from waeup.kofa.interfaces import IExtFileStore, IPDF, IKofaUtils
30from waeup.kofa.interfaces import MessageFactory as _
31from waeup.kofa.widgets.datewidget import FriendlyDateDisplayWidget
32
33class PDFApplicationSlip(grok.Adapter):
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?
38    grok.context(IApplicant)
39    grok.implements(IPDF)
40    grok.name('application_slip')
41    note = None
42
43    form_fields =  grok.AutoFields(IApplicant).omit(
44        'locked', 'course_admitted')
45    form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le')
46
47    @property
48    def target(self):
49        return getattr(self.context.__parent__, 'prefix', None)
50
51    @property
52    def title(self):
53        container_title = self.context.__parent__.title
54        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
55        ar_translation = translate(_('Application Record'),
56            'waeup.kofa', target_language=portal_language)
57        return '%s - %s %s' % (container_title,
58            ar_translation, self.context.application_number)
59
60    def _getCourseAdmittedLink(self, view):
61        """Return link, title and code in html format to the certificate
62           admitted.
63        """
64        course_admitted = self.context.course_admitted
65        if view is not None and getattr(course_admitted, '__parent__',None):
66            url = view.url(course_admitted)
67            title = course_admitted.title
68            code = course_admitted.code
69            return '<a href="%s">%s - %s</a>' %(url,code,title)
70        return ''
71
72    def _getDeptAndFaculty(self):
73        """Return long titles of department and faculty.
74
75        Returns a list [department_title, faculty_title]
76
77        If the context applicant has no course admitted or dept. and
78        faculty cannot be determined, ``None`` is returned.
79        """
80        course_admitted = self.context.course_admitted
81        dept = getattr(
82                getattr(course_admitted, '__parent__', None),
83                '__parent__', None)
84        faculty = getattr(dept, '__parent__', None)
85        return [x is not None and x.longtitle() or x for x in dept, faculty]
86
87    def _addComments(self, data):
88        if self.context.state == 'created':
89            comment = translate(_(
90                'Proceed to the login page of the portal' +
91                ' and enter your new credentials:' +
92                ' user name= ${a}, password = ${b}. ' +
93                'Change your password when you have logged in.',
94                mapping = {
95                    'a':self.context.student_id,
96                    'b':self.context.application_number}
97                ))
98            comment = Paragraph(comment, SMALL_PARA_STYLE)
99            data.extend([Spacer(1, 18), comment])
100        return data
101
102    def _getPDFCreator(self):
103        return getUtility(IPDFCreator)
104
105    def __call__(self, view=None, note=None):
106        """Return a PDF representation of the context applicant.
107
108        If no `view` is given, the course admitted field will be an
109        empty string and author will be set as ``'unknown'``.
110
111        If a `view` is given, author will be set as the calling
112        principal.
113        """
114        doc_title = '\n'.join([x.strip() for x in self.title.split(' - ')])
115        data = []
116        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
117        separators = getUtility(IApplicantsUtils).SEPARATORS_DICT
118        creator = self._getPDFCreator()
119
120        # append history
121        data.extend(creator.fromStringList(self.context.history.messages))
122        data.append(Spacer(1, 20))
123
124        # append photograph
125        img_path = getattr(
126            getUtility(IExtFileStore).getFileByContext(self.context),
127            'name', DEFAULT_PASSPORT_IMAGE_PATH)
128        data.append(creator.getImage(img_path))
129        data.append(Spacer(1, 12))
130
131        # append widgets
132        dept, faculty = self._getDeptAndFaculty()
133        data.append(creator.getWidgetsTable(
134            self.form_fields, self.context, view, lang=portal_language,
135            domain='waeup.kofa', separators=separators,
136            course_label='Admitted Course of Study:',
137            course_link=self._getCourseAdmittedLink(view),
138            dept=dept, faculty=faculty))
139
140        # append comments
141        data = self._addComments(data)
142
143        return creator.create_pdf(data, None, doc_title, note=self.note)
Note: See TracBrowser for help on using the repository browser.