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

Last change on this file since 8142 was 8103, checked in by uli, 13 years ago

pyflakes and usage of w.k. styles.

  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1## $Id: pdf.py 8103 2012-04-11 08:28:23Z uli $
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
42    form_fields =  grok.AutoFields(IApplicant).omit(
43        'locked', 'course_admitted')
44    form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le')
45
46    @property
47    def title(self):
48        container_title = self.context.__parent__.title
49        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
50        ar_translation = translate(_('Application Record'),
51            'waeup.kofa', target_language=portal_language)
52        return '%s - %s %s' % (container_title,
53            ar_translation, self.context.application_number)
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
67    def _getDeptAndFaculty(self):
68        """Return long titles of department and faculty.
69
70        Returns a list [department_title, faculty_title]
71
72        If the context applicant has no course admitted or dept. and
73        faculty cannot be determined, ``None`` is returned.
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)
80        return [x is not None and x.longtitle() or x for x in dept, faculty]
81
82    def _addComments(self, data):
83        if self.context.state == 'created':
84            comment1 = _(
85                'Proceed to the login page of the portal' +
86                ' and enter your new credentials:' +
87                ' user name= ${a}, password = ${b}.', mapping = {
88                    'a':self.context.student_id,
89                    'b':self.context.application_number}
90                )
91            comment2 = _(
92                'Change your password when you have logged in.'
93                )
94            comment1 = Paragraph(comment1, SMALL_PARA_STYLE)
95            comment2 = Paragraph(comment2, SMALL_PARA_STYLE)
96            data.extend([Spacer(1, 18), comment1, comment2])
97        return data
98
99    def __call__(self, view=None):
100        """Return a PDF representation of the context applicant.
101
102        If no `view` is given, the course admitted field will be an
103        empty string and author will be set as ``'unknown'``.
104
105        If a `view` is given, author will be set as the calling
106        principal.
107        """
108        doc_title = '\n'.join([x.strip() for x in self.title.split(' - ')])
109        data = []
110        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
111        separators = getUtility(IApplicantsUtils).SEPARATORS_DICT
112        creator = getUtility(IPDFCreator)
113
114        # append history
115        data.extend(creator.fromStringList(self.context.history.messages))
116
117        # append photograph
118        img_path = getattr(
119            getUtility(IExtFileStore).getFileByContext(self.context),
120            'name', DEFAULT_PASSPORT_IMAGE_PATH)
121        data.append(creator.getImage(img_path))
122        data.append(Spacer(1, 12))
123
124        # append widgets
125        dept, faculty = self._getDeptAndFaculty()
126        data.append(creator.getWidgetsTable(
127            self.form_fields, self.context, view, lang=portal_language,
128            domain='waeup.kofa', separators=separators,
129            course_label='Admitted Course of Study:',
130            course_link=self._getCourseAdmittedLink(view),
131            dept=dept, faculty=faculty))
132
133        # append comments
134        data = self._addComments(data)
135        return creator.create_pdf(data, None, doc_title)
Note: See TracBrowser for help on using the repository browser.