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