1 | ## $Id: pdf.py 7835 2012-03-11 15:57:55Z 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 datetime import datetime |
---|
23 | from zope.component import getUtility |
---|
24 | from zope.i18n import translate |
---|
25 | from reportlab.pdfgen import canvas |
---|
26 | from reportlab.lib.units import cm |
---|
27 | from reportlab.lib.pagesizes import A4 |
---|
28 | from reportlab.lib.styles import getSampleStyleSheet |
---|
29 | from reportlab.platypus import Frame, Paragraph, Image, Table, Spacer |
---|
30 | from reportlab.platypus.tables import TableStyle |
---|
31 | from zope.component import getUtility |
---|
32 | from zope.formlib.form import setUpEditWidgets |
---|
33 | from zope.publisher.browser import TestRequest |
---|
34 | from waeup.kofa.applicants.interfaces import IApplicant |
---|
35 | from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH |
---|
36 | from waeup.kofa.interfaces import IExtFileStore, IPDF, IKofaUtils |
---|
37 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
38 | |
---|
39 | SLIP_STYLE = TableStyle( |
---|
40 | [('VALIGN',(0,0),(-1,-1),'TOP')] |
---|
41 | ) |
---|
42 | |
---|
43 | |
---|
44 | class PDFApplicationSlip(grok.Adapter): |
---|
45 | |
---|
46 | grok.context(IApplicant) |
---|
47 | grok.implements(IPDF) |
---|
48 | grok.name('application_slip') |
---|
49 | |
---|
50 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
51 | 'locked', 'course_admitted') |
---|
52 | |
---|
53 | @property |
---|
54 | def title(self): |
---|
55 | container_title = self.context.__parent__.title |
---|
56 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
57 | ar_translation = translate(_('Application Record'), |
---|
58 | 'waeup.kofa', target_language=portal_language) |
---|
59 | return '%s - %s %s' % (container_title, |
---|
60 | ar_translation, self.context.application_number) |
---|
61 | |
---|
62 | def _setUpWidgets(self): |
---|
63 | """Setup simple display widgets. |
---|
64 | |
---|
65 | Returns the list of widgets. |
---|
66 | """ |
---|
67 | request = TestRequest() |
---|
68 | return setUpEditWidgets( |
---|
69 | self.form_fields, 'form', self.context, request, {}, |
---|
70 | for_display=True, ignore_request=True |
---|
71 | ) |
---|
72 | |
---|
73 | def _getCourseAdmittedLink(self, view): |
---|
74 | """Return link, title and code in html format to the certificate |
---|
75 | admitted. |
---|
76 | """ |
---|
77 | course_admitted = self.context.course_admitted |
---|
78 | if view is not None and getattr(course_admitted, '__parent__',None): |
---|
79 | url = view.url(course_admitted) |
---|
80 | title = course_admitted.title |
---|
81 | code = course_admitted.code |
---|
82 | return '<a href="%s">%s - %s</a>' %(url,code,title) |
---|
83 | return '' |
---|
84 | |
---|
85 | def _addPassportImage(self, data): |
---|
86 | """Add passport image to data stream. |
---|
87 | |
---|
88 | If no image exists yet the default image is added. |
---|
89 | """ |
---|
90 | img = getUtility(IExtFileStore).getFileByContext(self.context) |
---|
91 | if img is None: |
---|
92 | img = open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb') |
---|
93 | doc_img = Image(img.name, width=4*cm, height=3*cm, kind='bound') |
---|
94 | data.append([doc_img]) |
---|
95 | data.append([Spacer(1, 18)]) |
---|
96 | return data |
---|
97 | |
---|
98 | def _addDeptAndFaculty(self, data): |
---|
99 | """If we have a valid course admitted, add dept. and faculty to data |
---|
100 | stream. |
---|
101 | """ |
---|
102 | style = getSampleStyleSheet() |
---|
103 | course_admitted = self.context.course_admitted |
---|
104 | dept = getattr( |
---|
105 | getattr(course_admitted, '__parent__', None), |
---|
106 | '__parent__', None) |
---|
107 | if dept is None: |
---|
108 | return data |
---|
109 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
110 | dp_translation = translate(_('Department:'), |
---|
111 | 'waeup.kofa', target_language=portal_language) |
---|
112 | f_label = '<font size=12>%s</font>' % dp_translation |
---|
113 | f_text = '<font size=12>%s</font>' % dept.longtitle() |
---|
114 | f_label = Paragraph(f_label, style["Normal"]) |
---|
115 | f_text = Paragraph(f_text, style["Normal"]) |
---|
116 | data.append([f_label,f_text]) |
---|
117 | |
---|
118 | faculty = getattr(dept, '__parent__', None) |
---|
119 | if faculty is None: |
---|
120 | return data |
---|
121 | fc_translation = translate(_('Faculty:'), |
---|
122 | 'waeup.kofa', target_language=portal_language) |
---|
123 | f_label = '<font size=12>%s</font>' % fc_translation |
---|
124 | f_text = '<font size=12>%s</font>' % faculty.longtitle() |
---|
125 | f_label = Paragraph(f_label, style["Normal"]) |
---|
126 | f_text = Paragraph(f_text, style["Normal"]) |
---|
127 | data.append([f_label,f_text]) |
---|
128 | return data |
---|
129 | |
---|
130 | def __call__(self, view=None): |
---|
131 | """Return a PDF representation of the context applicant. |
---|
132 | |
---|
133 | If no `view` is given, the course admitted field will be an |
---|
134 | empty string and author will be set as ``'unknown'``. |
---|
135 | |
---|
136 | If a `view` is given, author will be set as the calling |
---|
137 | principal. |
---|
138 | """ |
---|
139 | pdf = canvas.Canvas('application_slip.pdf',pagesize=A4) |
---|
140 | pdf.setTitle(self.title) |
---|
141 | pdf.setSubject('Application') |
---|
142 | if view is None: |
---|
143 | pdf.setAuthor('unknown') |
---|
144 | if view is not None: |
---|
145 | pdf.setAuthor('%s (%s)' % (view.request.principal.title, |
---|
146 | view.request.principal.id)) |
---|
147 | pdf.setCreator('Kofa') |
---|
148 | width, height = A4 |
---|
149 | style = getSampleStyleSheet() |
---|
150 | pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm)) |
---|
151 | |
---|
152 | story = [] |
---|
153 | frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm)) |
---|
154 | header_title = getattr(grok.getSite(), 'name', u'Sample University') |
---|
155 | story.append(Paragraph(header_title, style["Heading1"])) |
---|
156 | frame_header.addFromList(story,pdf) |
---|
157 | |
---|
158 | story = [] |
---|
159 | frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm)) |
---|
160 | story.append(Paragraph(self.title, style["Heading2"])) |
---|
161 | story.append(Spacer(1, 18)) |
---|
162 | for msg in self.context.history.messages: |
---|
163 | f_msg = '<font face="Courier" size=10>%s</font>' % msg |
---|
164 | story.append(Paragraph(f_msg, style["Normal"])) |
---|
165 | story.append(Spacer(1, 24)) |
---|
166 | |
---|
167 | # Setup table data |
---|
168 | data = [] |
---|
169 | # Insert passport photograph |
---|
170 | data = self._addPassportImage(data) |
---|
171 | # Render widget fields |
---|
172 | widgets = self._setUpWidgets() |
---|
173 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
174 | for widget in widgets: # self.widgets: |
---|
175 | f_label = '<font size=12>%s</font>:' % translate( |
---|
176 | widget.label.strip(), 'waeup.kofa', |
---|
177 | target_language=portal_language) |
---|
178 | f_label = Paragraph(f_label, style["Normal"]) |
---|
179 | f_text = '<font size=12>%s</font>' % widget() |
---|
180 | # Add br tag if widgets contain div tags |
---|
181 | # which are not supported by reportlab |
---|
182 | f_text = f_text.replace('</div>', '</div><br />') |
---|
183 | f_text = f_text.replace('\n', '') |
---|
184 | f_text = Paragraph(f_text, style["Normal"]) |
---|
185 | data.append([f_label,f_text]) |
---|
186 | adm_translation = translate(_('Admitted Course of Study:'), |
---|
187 | 'waeup.kofa', target_language=portal_language) |
---|
188 | f_label = '<font size=12>%s</font>' % adm_translation |
---|
189 | f_text = '<font size=12>%s</font>' % ( |
---|
190 | self._getCourseAdmittedLink(view),) |
---|
191 | f_label = Paragraph(f_label, style["Normal"]) |
---|
192 | f_text = Paragraph(f_text, style["Normal"]) |
---|
193 | data.append([f_label,f_text]) |
---|
194 | |
---|
195 | # Add dept. and faculty if applicable |
---|
196 | data = self._addDeptAndFaculty(data) |
---|
197 | |
---|
198 | # Create table |
---|
199 | table = Table(data,style=SLIP_STYLE) |
---|
200 | story.append(table) |
---|
201 | |
---|
202 | # Add some comments |
---|
203 | if self.context.state == 'created': |
---|
204 | comment1 = _( |
---|
205 | '<font size=10>Proceed to the login page of the portal' + |
---|
206 | ' and enter your new credentials:' + |
---|
207 | ' user name= ${a}, password = ${b}.</font>', mapping = { |
---|
208 | 'a':self.context.student_id, 'b':self.context.application_number} |
---|
209 | ) |
---|
210 | comment2 = _( |
---|
211 | '<font size=10>Change your password when you have ' + |
---|
212 | ' logged in.</font>' |
---|
213 | ) |
---|
214 | comment1 = Paragraph(comment1, style["Normal"]) |
---|
215 | comment2 = Paragraph(comment2, style["Normal"]) |
---|
216 | story.append(Spacer(1, 18)) |
---|
217 | story.append(comment1) |
---|
218 | story.append(comment2) |
---|
219 | |
---|
220 | frame_body.addFromList(story,pdf) |
---|
221 | |
---|
222 | story = [] |
---|
223 | frame_footer = Frame(1*cm,0,width-(2*cm),1*cm) |
---|
224 | timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") |
---|
225 | f_text = '<font size=10>%s</font>' % timestamp |
---|
226 | story.append(Paragraph(f_text, style["Normal"])) |
---|
227 | frame_footer.addFromList(story,pdf) |
---|
228 | return pdf.getpdfdata() |
---|