1 | ## $Id: pdf.py 10310 2013-06-17 07:00:04Z 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 | """ |
---|
19 | Generate PDF docs for applicants. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from reportlab.platypus import Paragraph, Spacer, Table |
---|
23 | from reportlab.lib.units import cm |
---|
24 | from zope.component import getUtility |
---|
25 | from zope.i18n import translate |
---|
26 | from waeup.kofa.applicants.interfaces import IApplicant, IApplicantsUtils |
---|
27 | from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH |
---|
28 | from waeup.kofa.browser.interfaces import IPDFCreator |
---|
29 | from waeup.kofa.browser.pdf import SMALL_PARA_STYLE, ENTRY1_STYLE |
---|
30 | from waeup.kofa.interfaces import IExtFileStore, IPDF, IKofaUtils |
---|
31 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
32 | from waeup.kofa.widgets.datewidget import FriendlyDateDisplayWidget |
---|
33 | |
---|
34 | SLIP_STYLE = [ |
---|
35 | ('VALIGN',(0,0),(-1,-1),'TOP'), |
---|
36 | #('FONT', (0,0), (-1,-1), 'Helvetica', 11), |
---|
37 | ] |
---|
38 | |
---|
39 | class PDFApplicationSlip(grok.Adapter): |
---|
40 | """Create a PDF application slip for applicants. |
---|
41 | """ |
---|
42 | # XXX: Many things in here are reusable. We might want to split |
---|
43 | # things. Possibly move parts to IPDFCreator? |
---|
44 | grok.context(IApplicant) |
---|
45 | grok.implements(IPDF) |
---|
46 | grok.name('application_slip') |
---|
47 | note = None |
---|
48 | |
---|
49 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
50 | 'locked', 'course_admitted', 'suspended', |
---|
51 | ) |
---|
52 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
53 | column_two_fields = ('applicant_id', 'reg_number', |
---|
54 | 'firstname', 'middlename', 'lastname') |
---|
55 | |
---|
56 | @property |
---|
57 | def target(self): |
---|
58 | return getattr(self.context.__parent__, 'prefix', None) |
---|
59 | |
---|
60 | @property |
---|
61 | def title(self): |
---|
62 | container_title = self.context.__parent__.title |
---|
63 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
64 | ar_translation = translate(_('Application Record'), |
---|
65 | 'waeup.kofa', target_language=portal_language) |
---|
66 | return '%s - %s %s' % (container_title, |
---|
67 | ar_translation, self.context.application_number) |
---|
68 | |
---|
69 | def _getCourseAdmittedLink(self, view): |
---|
70 | """Return link, title and code in html format to the certificate |
---|
71 | admitted. |
---|
72 | """ |
---|
73 | course_admitted = self.context.course_admitted |
---|
74 | if view is not None and getattr(course_admitted, '__parent__',None): |
---|
75 | url = view.url(course_admitted) |
---|
76 | title = course_admitted.title |
---|
77 | code = course_admitted.code |
---|
78 | return '<a href="%s">%s - %s</a>' %(url,code,title) |
---|
79 | return '' |
---|
80 | |
---|
81 | def _getDeptAndFaculty(self): |
---|
82 | """Return long titles of department and faculty. |
---|
83 | |
---|
84 | Returns a list [department_title, faculty_title] |
---|
85 | |
---|
86 | If the context applicant has no course admitted or dept. and |
---|
87 | faculty cannot be determined, ``None`` is returned. |
---|
88 | """ |
---|
89 | course_admitted = self.context.course_admitted |
---|
90 | dept = getattr( |
---|
91 | getattr(course_admitted, '__parent__', None), |
---|
92 | '__parent__', None) |
---|
93 | faculty = getattr(dept, '__parent__', None) |
---|
94 | return [x is not None and x.longtitle() or x for x in dept, faculty] |
---|
95 | |
---|
96 | def _addComments(self, data): |
---|
97 | if self.context.state == 'created': |
---|
98 | comment = translate(_( |
---|
99 | 'Proceed to the login page of the portal' + |
---|
100 | ' and enter your new credentials:' + |
---|
101 | ' user name= ${a}, password = ${b}. ' + |
---|
102 | 'Change your password when you have logged in.', |
---|
103 | mapping = { |
---|
104 | 'a':self.context.student_id, |
---|
105 | 'b':self.context.application_number} |
---|
106 | )) |
---|
107 | comment = Paragraph(comment, SMALL_PARA_STYLE) |
---|
108 | data.extend([Spacer(1, 18), comment]) |
---|
109 | return data |
---|
110 | |
---|
111 | def _getPDFCreator(self): |
---|
112 | return getUtility(IPDFCreator) |
---|
113 | |
---|
114 | def __call__(self, view=None, note=None): |
---|
115 | """Return a PDF representation of the context applicant. |
---|
116 | |
---|
117 | If no `view` is given, the course admitted field will be an |
---|
118 | empty string and author will be set as ``'unknown'``. |
---|
119 | |
---|
120 | If a `view` is given, author will be set as the calling |
---|
121 | principal. |
---|
122 | """ |
---|
123 | doc_title = '\n'.join([x.strip() for x in self.title.split(' - ')]) |
---|
124 | data = [] |
---|
125 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
126 | separators = getUtility(IApplicantsUtils).SEPARATORS_DICT |
---|
127 | creator = self._getPDFCreator() |
---|
128 | |
---|
129 | # append history |
---|
130 | data.extend(creator.fromStringList(self.context.history.messages)) |
---|
131 | data.append(Spacer(1, 20)) |
---|
132 | |
---|
133 | # create two-column header table |
---|
134 | data_left = [] |
---|
135 | data_right = [] |
---|
136 | # append photograph to the left |
---|
137 | img_path = getattr( |
---|
138 | getUtility(IExtFileStore).getFileByContext(self.context), |
---|
139 | 'name', DEFAULT_PASSPORT_IMAGE_PATH) |
---|
140 | data_left.append([creator.getImage(img_path)]) |
---|
141 | table_left = Table(data_left,style=SLIP_STYLE) |
---|
142 | # append column-two fields to the right |
---|
143 | fields = [ |
---|
144 | field for field in self.form_fields |
---|
145 | if field.__name__ in self.column_two_fields] |
---|
146 | table_right = creator.getWidgetsTable( |
---|
147 | fields, self.context, None, lang=portal_language, |
---|
148 | separators=None, colWidths=[5*cm, 6*cm]) |
---|
149 | header_table = Table([[table_left, table_right],],style=SLIP_STYLE) |
---|
150 | data.append(header_table) |
---|
151 | |
---|
152 | data.append(Spacer(1, 12)) |
---|
153 | |
---|
154 | # append widgets except those already added in column two |
---|
155 | dept, faculty = self._getDeptAndFaculty() |
---|
156 | fields = [ |
---|
157 | field for field in self.form_fields |
---|
158 | if not field.__name__ in self.column_two_fields] |
---|
159 | data.append(creator.getWidgetsTable( |
---|
160 | fields, self.context, view, lang=portal_language, |
---|
161 | domain='waeup.kofa', separators=separators, |
---|
162 | course_label='Admitted Course of Study:', |
---|
163 | course_link=self._getCourseAdmittedLink(view), |
---|
164 | dept=dept, faculty=faculty)) |
---|
165 | |
---|
166 | # append comments |
---|
167 | data = self._addComments(data) |
---|
168 | |
---|
169 | return creator.create_pdf(data, None, doc_title, note=self.note) |
---|