1 | ## $Id: browser.py 10340 2013-06-22 13:19:37Z 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 | """UI components for basic applicants and related components. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | import os |
---|
22 | from zope.component import getUtility |
---|
23 | from waeup.kofa.interfaces import ( |
---|
24 | IExtFileStore, IFileStoreNameChooser) |
---|
25 | from zope.formlib.textwidgets import BytesDisplayWidget |
---|
26 | from waeup.kofa.utils.helpers import string_from_bytes, file_size |
---|
27 | from waeup.imostate.applicants.interfaces import ( |
---|
28 | ICustomApplicant, |
---|
29 | ICustomUGApplicant, |
---|
30 | ICustomPGApplicant, |
---|
31 | ICustomPGApplicantEdit, |
---|
32 | ICustomUGApplicantEdit, |
---|
33 | UG_OMIT_DISPLAY_FIELDS, |
---|
34 | UG_OMIT_PDF_FIELDS, |
---|
35 | UG_OMIT_MANAGE_FIELDS, |
---|
36 | UG_OMIT_EDIT_FIELDS, |
---|
37 | PG_OMIT_DISPLAY_FIELDS, |
---|
38 | PG_OMIT_PDF_FIELDS, |
---|
39 | PG_OMIT_MANAGE_FIELDS, |
---|
40 | PG_OMIT_EDIT_FIELDS, |
---|
41 | PUTME_OMIT_EDIT_FIELDS, |
---|
42 | PUTME_OMIT_DISPLAY_FIELDS, |
---|
43 | PUTME_OMIT_PDF_FIELDS, |
---|
44 | PUTME_OMIT_MANAGE_FIELDS, |
---|
45 | PUTME_OMIT_EDIT_FIELDS, |
---|
46 | ) |
---|
47 | from waeup.imostate.interfaces import MessageFactory as _ |
---|
48 | from kofacustom.nigeria.applicants.browser import ( |
---|
49 | NigeriaApplicantDisplayFormPage, |
---|
50 | NigeriaApplicantManageFormPage, |
---|
51 | NigeriaApplicantEditFormPage, |
---|
52 | NigeriaPDFApplicationSlip) |
---|
53 | |
---|
54 | MAX_FILE_UPLOAD_SIZE = 1024 * 500 |
---|
55 | |
---|
56 | def handle_file_upload(upload, context, view, attr=None): |
---|
57 | """Handle upload of applicant files. |
---|
58 | |
---|
59 | Returns `True` in case of success or `False`. |
---|
60 | |
---|
61 | Please note that file pointer passed in (`upload`) most probably |
---|
62 | points to end of file when leaving this function. |
---|
63 | """ |
---|
64 | size = file_size(upload) |
---|
65 | if size > MAX_FILE_UPLOAD_SIZE: |
---|
66 | view.flash(_('Uploaded file is too big!')) |
---|
67 | return False |
---|
68 | dummy, ext = os.path.splitext(upload.filename) |
---|
69 | ext.lower() |
---|
70 | if ext != '.pdf': |
---|
71 | view.flash(_('pdf file extension expected.')) |
---|
72 | return False |
---|
73 | upload.seek(0) # file pointer moved when determining size |
---|
74 | store = getUtility(IExtFileStore) |
---|
75 | file_id = IFileStoreNameChooser(context).chooseName(attr=attr) |
---|
76 | store.createFile(file_id, upload) |
---|
77 | return True |
---|
78 | |
---|
79 | class CustomApplicantDisplayFormPage(NigeriaApplicantDisplayFormPage): |
---|
80 | """A display view for applicant data. |
---|
81 | """ |
---|
82 | |
---|
83 | grok.template('applicantdisplaypage') |
---|
84 | |
---|
85 | @property |
---|
86 | def file_links(self): |
---|
87 | html = '' |
---|
88 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
89 | self.context, attr='extraform.pdf') |
---|
90 | if pdf: |
---|
91 | html += '<a href="extraform.pdf">Extra Applicant Information Form</a>, ' |
---|
92 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
93 | self.context, attr='refereeform.pdf') |
---|
94 | if pdf: |
---|
95 | html += '<a href="refereeform.pdf">Referee\'s Form</a>, ' |
---|
96 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
97 | self.context, attr='credentials.pdf') |
---|
98 | if pdf: |
---|
99 | html += '<a href="credentials.pdf">Credentials</a>' |
---|
100 | return html |
---|
101 | |
---|
102 | @property |
---|
103 | def form_fields(self): |
---|
104 | if self.target is not None and self.target.startswith('pg'): |
---|
105 | form_fields = grok.AutoFields(ICustomPGApplicant) |
---|
106 | for field in PG_OMIT_DISPLAY_FIELDS: |
---|
107 | form_fields = form_fields.omit(field) |
---|
108 | else: |
---|
109 | form_fields = grok.AutoFields(ICustomUGApplicant) |
---|
110 | for field in UG_OMIT_DISPLAY_FIELDS: |
---|
111 | form_fields = form_fields.omit(field) |
---|
112 | if form_fields.get('perm_address', None): |
---|
113 | form_fields['perm_address'].custom_widget = BytesDisplayWidget |
---|
114 | form_fields['notice'].custom_widget = BytesDisplayWidget |
---|
115 | if not getattr(self.context, 'student_id'): |
---|
116 | form_fields = form_fields.omit('student_id') |
---|
117 | if not getattr(self.context, 'screening_score'): |
---|
118 | form_fields = form_fields.omit('screening_score') |
---|
119 | if not getattr(self.context, 'screening_venue'): |
---|
120 | form_fields = form_fields.omit('screening_venue') |
---|
121 | if not getattr(self.context, 'screening_date'): |
---|
122 | form_fields = form_fields.omit('screening_date') |
---|
123 | if not self.context.low_jamb_score: |
---|
124 | form_fields = form_fields.omit('course2') |
---|
125 | return form_fields |
---|
126 | |
---|
127 | def update(self): |
---|
128 | super(CustomApplicantDisplayFormPage, self).update() |
---|
129 | self.extraform_url = self.url(self.context, 'extraform.pdf') |
---|
130 | self.referreeform_url = self.url(self.context, 'refereeform.pdf') |
---|
131 | self.credentials_url = self.url(self.context, 'credentials.pdf') |
---|
132 | return |
---|
133 | |
---|
134 | class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip): |
---|
135 | |
---|
136 | @property |
---|
137 | def form_fields(self): |
---|
138 | if self.target is not None and self.target.startswith('pg'): |
---|
139 | form_fields = grok.AutoFields(ICustomPGApplicant) |
---|
140 | for field in PG_OMIT_PDF_FIELDS: |
---|
141 | form_fields = form_fields.omit(field) |
---|
142 | elif self.target is not None and self.target.startswith('putme'): |
---|
143 | form_fields = grok.AutoFields(ICustomUGApplicant) |
---|
144 | if self._reduced_slip(): |
---|
145 | for field in PUTME_OMIT_RESULT_SLIP_FIELDS: |
---|
146 | form_fields = form_fields.omit(field) |
---|
147 | else: |
---|
148 | form_fields = grok.AutoFields(ICustomUGApplicant) |
---|
149 | for field in UG_OMIT_PDF_FIELDS: |
---|
150 | form_fields = form_fields.omit(field) |
---|
151 | if not getattr(self.context, 'student_id'): |
---|
152 | form_fields = form_fields.omit('student_id') |
---|
153 | if not getattr(self.context, 'screening_score'): |
---|
154 | form_fields = form_fields.omit('screening_score') |
---|
155 | if not getattr(self.context, 'screening_venue'): |
---|
156 | form_fields = form_fields.omit('screening_venue') |
---|
157 | if not getattr(self.context, 'screening_date'): |
---|
158 | form_fields = form_fields.omit('screening_date') |
---|
159 | if not self.context.low_jamb_score: |
---|
160 | form_fields = form_fields.omit('course2') |
---|
161 | return form_fields |
---|
162 | |
---|
163 | class CustomApplicantManageFormPage(NigeriaApplicantManageFormPage): |
---|
164 | """A full edit view for applicant data. |
---|
165 | """ |
---|
166 | grok.template('applicanteditpage') |
---|
167 | |
---|
168 | @property |
---|
169 | def form_fields(self): |
---|
170 | if self.target is not None and self.target.startswith('pg'): |
---|
171 | form_fields = grok.AutoFields(ICustomPGApplicant) |
---|
172 | for field in PG_OMIT_MANAGE_FIELDS: |
---|
173 | form_fields = form_fields.omit(field) |
---|
174 | elif self.target is not None and self.target.startswith('putme'): |
---|
175 | form_fields = grok.AutoFields(ICustomUGApplicant) |
---|
176 | for field in PUTME_OMIT_MANAGE_FIELDS: |
---|
177 | form_fields = form_fields.omit(field) |
---|
178 | else: |
---|
179 | form_fields = grok.AutoFields(ICustomUGApplicant) |
---|
180 | for field in UG_OMIT_MANAGE_FIELDS: |
---|
181 | form_fields = form_fields.omit(field) |
---|
182 | form_fields['student_id'].for_display = True |
---|
183 | form_fields['applicant_id'].for_display = True |
---|
184 | return form_fields |
---|
185 | |
---|
186 | def update(self): |
---|
187 | super(CustomApplicantManageFormPage, self).update() |
---|
188 | upload_extraform = self.request.form.get('form.extraform', None) |
---|
189 | if upload_extraform: |
---|
190 | # We got a fresh extraform upload |
---|
191 | success = handle_file_upload( |
---|
192 | upload_extraform, self.context, self, attr='extraform.pdf') |
---|
193 | if success: |
---|
194 | self.context.writeLogMessage(self, 'saved: extraform') |
---|
195 | else: |
---|
196 | self.upload_success = False |
---|
197 | upload_refereeform = self.request.form.get('form.refereeform', None) |
---|
198 | if upload_refereeform: |
---|
199 | # We got a fresh refereeform upload |
---|
200 | success = handle_file_upload( |
---|
201 | upload_refereeform, self.context, self, attr='refereeform.pdf') |
---|
202 | if success: |
---|
203 | self.context.writeLogMessage(self, 'saved: refereeform') |
---|
204 | else: |
---|
205 | self.upload_success = False |
---|
206 | upload_credentials = self.request.form.get('form.credentials', None) |
---|
207 | if upload_credentials: |
---|
208 | # We got a fresh credentials upload |
---|
209 | success = handle_file_upload( |
---|
210 | upload_credentials, self.context, self, attr='credentials.pdf') |
---|
211 | if success: |
---|
212 | self.context.writeLogMessage(self, 'saved: credentials') |
---|
213 | else: |
---|
214 | self.upload_success = False |
---|
215 | self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE) |
---|
216 | return |
---|
217 | |
---|
218 | class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage): |
---|
219 | """An applicant-centered edit view for applicant data. |
---|
220 | """ |
---|
221 | grok.template('applicanteditpage') |
---|
222 | |
---|
223 | @property |
---|
224 | def form_fields(self): |
---|
225 | if self.target is not None and self.target.startswith('pg'): |
---|
226 | form_fields = grok.AutoFields(ICustomPGApplicantEdit) |
---|
227 | for field in PG_OMIT_EDIT_FIELDS: |
---|
228 | form_fields = form_fields.omit(field) |
---|
229 | elif self.target is not None and self.target.startswith('putme'): |
---|
230 | form_fields = grok.AutoFields(ICustomUGApplicantEdit) |
---|
231 | for field in PUTME_OMIT_EDIT_FIELDS: |
---|
232 | form_fields = form_fields.omit(field) |
---|
233 | else: |
---|
234 | form_fields = grok.AutoFields(ICustomUGApplicantEdit) |
---|
235 | for field in UG_OMIT_EDIT_FIELDS: |
---|
236 | form_fields = form_fields.omit(field) |
---|
237 | form_fields['applicant_id'].for_display = True |
---|
238 | form_fields['reg_number'].for_display = True |
---|
239 | if not self.context.low_jamb_score: |
---|
240 | form_fields = form_fields.omit('course2') |
---|
241 | return form_fields |
---|
242 | |
---|
243 | def dataNotComplete(self): |
---|
244 | store = getUtility(IExtFileStore) |
---|
245 | if not store.getFileByContext(self.context, attr=u'passport.jpg'): |
---|
246 | return _('No passport picture uploaded.') |
---|
247 | if not store.getFileByContext(self.context, attr=u'extraform.pdf'): |
---|
248 | return _('No extra information form pdf file uploaded.') |
---|
249 | if not store.getFileByContext(self.context, attr=u'refereeform.pdf'): |
---|
250 | return _('No referee form pdf file uploaded.') |
---|
251 | if self.target is not None and self.target.startswith('pg') \ |
---|
252 | and not store.getFileByContext(self.context, attr=u'credentials.pdf'): |
---|
253 | return _('No credentials pdf file uploaded.') |
---|
254 | if not self.request.form.get('confirm_passport', False): |
---|
255 | return _('Passport picture confirmation box not ticked.') |
---|
256 | return False |
---|
257 | |
---|
258 | def update(self): |
---|
259 | if self.context.locked or ( |
---|
260 | self.context.__parent__.expired and |
---|
261 | self.context.__parent__.strict_deadline): |
---|
262 | self.emit_lock_message() |
---|
263 | return |
---|
264 | super(CustomApplicantEditFormPage, self).update() |
---|
265 | upload_extraform = self.request.form.get('form.extraform', None) |
---|
266 | if upload_extraform: |
---|
267 | # We got a fresh extraform upload |
---|
268 | success = handle_file_upload( |
---|
269 | upload_extraform, self.context, self, attr='extraform.pdf') |
---|
270 | if not success: |
---|
271 | self.upload_success = False |
---|
272 | upload_refereeform = self.request.form.get('form.refereeform', None) |
---|
273 | if upload_refereeform: |
---|
274 | # We got a fresh refereeform upload |
---|
275 | success = handle_file_upload( |
---|
276 | upload_refereeform, self.context, self, attr='refereeform.pdf') |
---|
277 | if not success: |
---|
278 | self.upload_success = False |
---|
279 | upload_credentials = self.request.form.get('form.credentials', None) |
---|
280 | if upload_credentials: |
---|
281 | # We got a fresh credentials upload |
---|
282 | success = handle_file_upload( |
---|
283 | upload_credentials, self.context, self, attr='credentials.pdf') |
---|
284 | if not success: |
---|
285 | self.upload_success = False |
---|
286 | self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE) |
---|
287 | return |
---|
288 | |
---|
289 | class ExtraForm(grok.View): |
---|
290 | """Renders the pdf form extension for applicants. |
---|
291 | """ |
---|
292 | grok.name('extraform.pdf') |
---|
293 | grok.context(ICustomApplicant) |
---|
294 | grok.require('waeup.viewApplication') |
---|
295 | |
---|
296 | def render(self): |
---|
297 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
298 | self.context, attr='extraform.pdf') |
---|
299 | self.response.setHeader('Content-Type', 'application/pdf') |
---|
300 | return pdf |
---|
301 | |
---|
302 | class RefereeForm(grok.View): |
---|
303 | """Renders the pdf referee's form for applicants. |
---|
304 | """ |
---|
305 | grok.name('refereeform.pdf') |
---|
306 | grok.context(ICustomApplicant) |
---|
307 | grok.require('waeup.viewApplication') |
---|
308 | |
---|
309 | def render(self): |
---|
310 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
311 | self.context, attr='refereeform.pdf') |
---|
312 | self.response.setHeader('Content-Type', 'application/pdf') |
---|
313 | return pdf |
---|
314 | |
---|
315 | class Credentials(grok.View): |
---|
316 | """Renders the pdf credential's form for applicants. |
---|
317 | """ |
---|
318 | grok.name('credentials.pdf') |
---|
319 | grok.context(ICustomApplicant) |
---|
320 | grok.require('waeup.viewApplication') |
---|
321 | |
---|
322 | def render(self): |
---|
323 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
324 | self.context, attr='credentials.pdf') |
---|
325 | self.response.setHeader('Content-Type', 'application/pdf') |
---|
326 | return pdf |
---|