1 | ## $Id: browser.py 15942 2020-01-20 17:23:31Z 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 zope.formlib.textwidgets import BytesDisplayWidget |
---|
24 | from waeup.kofa.interfaces import ( |
---|
25 | IExtFileStore, IFileStoreNameChooser) |
---|
26 | from waeup.kofa.utils.helpers import string_from_bytes, file_size, now |
---|
27 | from waeup.kofa.applicants.browser import ( |
---|
28 | ApplicantRegistrationPage, ApplicantsContainerPage) |
---|
29 | from waeup.kofa.applicants.interfaces import ( |
---|
30 | ISpecialApplicant, IApplicantsContainer) |
---|
31 | from kofacustom.nigeria.applicants.browser import ( |
---|
32 | NigeriaApplicantDisplayFormPage, |
---|
33 | NigeriaApplicantManageFormPage, |
---|
34 | NigeriaApplicantEditFormPage, |
---|
35 | NigeriaPDFApplicationSlip) |
---|
36 | from waeup.kofa.widgets.datewidget import ( |
---|
37 | FriendlyDateDisplayWidget, |
---|
38 | FriendlyDatetimeDisplayWidget) |
---|
39 | from kofacustom.nigeria.applicants.interfaces import ( |
---|
40 | OMIT_DISPLAY_FIELDS, |
---|
41 | #UG_OMIT_DISPLAY_FIELDS, |
---|
42 | #UG_OMIT_PDF_FIELDS, |
---|
43 | #UG_OMIT_MANAGE_FIELDS, |
---|
44 | #UG_OMIT_EDIT_FIELDS, |
---|
45 | PG_OMIT_DISPLAY_FIELDS, |
---|
46 | PG_OMIT_PDF_FIELDS, |
---|
47 | PG_OMIT_MANAGE_FIELDS, |
---|
48 | PG_OMIT_EDIT_FIELDS, |
---|
49 | ) |
---|
50 | from kofacustom.iuokada.applicants.interfaces import ( |
---|
51 | ICustomPGApplicant, ICustomUGApplicant, ICustomApplicant, |
---|
52 | ICustomPGApplicantEdit, ICustomUGApplicantEdit, |
---|
53 | ICustomApplicantOnlinePayment |
---|
54 | ) |
---|
55 | from kofacustom.iuokada.interfaces import MessageFactory as _ |
---|
56 | |
---|
57 | MAX_FILE_UPLOAD_SIZE = 1024 * 500 |
---|
58 | |
---|
59 | # UG students are all undergraduate students. |
---|
60 | UG_OMIT_DISPLAY_FIELDS = OMIT_DISPLAY_FIELDS + ( |
---|
61 | #'jamb_subjects_list', |
---|
62 | 'programme_type',) |
---|
63 | UG_OMIT_PDF_FIELDS = UG_OMIT_DISPLAY_FIELDS + ('phone',) |
---|
64 | UG_OMIT_MANAGE_FIELDS = ( |
---|
65 | 'special_application', |
---|
66 | #'jamb_subjects_list', |
---|
67 | 'programme_type') |
---|
68 | UG_OMIT_EDIT_FIELDS = UG_OMIT_MANAGE_FIELDS + OMIT_DISPLAY_FIELDS + ( |
---|
69 | 'student_id', |
---|
70 | 'notice', |
---|
71 | 'screening_score', |
---|
72 | 'screening_venue', |
---|
73 | 'screening_date', |
---|
74 | #'jamb_age', |
---|
75 | #'jamb_subjects', |
---|
76 | #'jamb_score', |
---|
77 | #'jamb_reg_number', |
---|
78 | 'aggregate') |
---|
79 | |
---|
80 | def handle_file_upload(upload, context, view, attr=None): |
---|
81 | """Handle upload of applicant files. |
---|
82 | |
---|
83 | Returns `True` in case of success or `False`. |
---|
84 | |
---|
85 | Please note that file pointer passed in (`upload`) most probably |
---|
86 | points to end of file when leaving this function. |
---|
87 | """ |
---|
88 | size = file_size(upload) |
---|
89 | if size > MAX_FILE_UPLOAD_SIZE: |
---|
90 | view.flash(_('Uploaded file is too big!')) |
---|
91 | return False |
---|
92 | dummy, ext = os.path.splitext(upload.filename) |
---|
93 | ext.lower() |
---|
94 | if ext != '.pdf': |
---|
95 | view.flash(_('pdf file extension expected.')) |
---|
96 | return False |
---|
97 | upload.seek(0) # file pointer moved when determining size |
---|
98 | store = getUtility(IExtFileStore) |
---|
99 | file_id = IFileStoreNameChooser(context).chooseName(attr=attr) |
---|
100 | store.createFile(file_id, upload) |
---|
101 | return True |
---|
102 | |
---|
103 | class CustomApplicantsContainerPage(ApplicantsContainerPage): |
---|
104 | """The standard view for regular applicant containers. |
---|
105 | """ |
---|
106 | |
---|
107 | @property |
---|
108 | def form_fields(self): |
---|
109 | form_fields = grok.AutoFields(IApplicantsContainer).omit( |
---|
110 | 'title', 'description') |
---|
111 | if self.request.principal.id == 'zope.anybody': |
---|
112 | form_fields = form_fields.omit( |
---|
113 | 'code', 'prefix', 'year', 'mode', 'hidden', |
---|
114 | 'strict_deadline', 'application_category', |
---|
115 | 'application_slip_notice', |
---|
116 | 'application_fee', 'with_picture', |
---|
117 | 'startdate', 'enddate') |
---|
118 | return form_fields |
---|
119 | |
---|
120 | class CustomApplicantDisplayFormPage(NigeriaApplicantDisplayFormPage): |
---|
121 | """A display view for applicant data. |
---|
122 | """ |
---|
123 | |
---|
124 | @property |
---|
125 | def file_links(self): |
---|
126 | html = '' |
---|
127 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
128 | self.context, attr='res_stat.pdf') |
---|
129 | if pdf: |
---|
130 | html += '<a href="%s">Statement of Result</a>' % self.url( |
---|
131 | self.context, 'res_stat.pdf') |
---|
132 | return html |
---|
133 | |
---|
134 | @property |
---|
135 | def form_fields(self): |
---|
136 | if self.target is not None and self.target.startswith('pg'): |
---|
137 | form_fields = grok.AutoFields(ICustomPGApplicant) |
---|
138 | for field in PG_OMIT_DISPLAY_FIELDS: |
---|
139 | form_fields = form_fields.omit(field) |
---|
140 | else: |
---|
141 | form_fields = grok.AutoFields(ICustomUGApplicant) |
---|
142 | for field in UG_OMIT_DISPLAY_FIELDS: |
---|
143 | form_fields = form_fields.omit(field) |
---|
144 | #form_fields['perm_address'].custom_widget = BytesDisplayWidget |
---|
145 | form_fields['notice'].custom_widget = BytesDisplayWidget |
---|
146 | if not getattr(self.context, 'student_id'): |
---|
147 | form_fields = form_fields.omit('student_id') |
---|
148 | if not getattr(self.context, 'screening_score'): |
---|
149 | form_fields = form_fields.omit('screening_score') |
---|
150 | if not getattr(self.context, 'screening_venue') or self._not_paid(): |
---|
151 | form_fields = form_fields.omit('screening_venue') |
---|
152 | if not getattr(self.context, 'screening_date') or self._not_paid(): |
---|
153 | form_fields = form_fields.omit('screening_date') |
---|
154 | return form_fields |
---|
155 | |
---|
156 | class CustomApplicantManageFormPage(NigeriaApplicantManageFormPage): |
---|
157 | """A full edit view for applicant data. |
---|
158 | """ |
---|
159 | |
---|
160 | @property |
---|
161 | def form_fields(self): |
---|
162 | if self.target is not None and self.target.startswith('pg'): |
---|
163 | form_fields = grok.AutoFields(ICustomPGApplicant) |
---|
164 | for field in PG_OMIT_MANAGE_FIELDS: |
---|
165 | form_fields = form_fields.omit(field) |
---|
166 | else: |
---|
167 | form_fields = grok.AutoFields(ICustomUGApplicant) |
---|
168 | for field in UG_OMIT_MANAGE_FIELDS: |
---|
169 | form_fields = form_fields.omit(field) |
---|
170 | form_fields['student_id'].for_display = True |
---|
171 | form_fields['applicant_id'].for_display = True |
---|
172 | return form_fields |
---|
173 | |
---|
174 | def update(self): |
---|
175 | super(CustomApplicantManageFormPage, self).update() |
---|
176 | upload_res_stat = self.request.form.get('form.res_stat', None) |
---|
177 | if upload_res_stat: |
---|
178 | # We got a fresh res_stat upload |
---|
179 | success = handle_file_upload( |
---|
180 | upload_res_stat, self.context, self, attr='res_stat.pdf') |
---|
181 | if success: |
---|
182 | self.context.writeLogMessage(self, 'saved: res_stat') |
---|
183 | else: |
---|
184 | self.upload_success = False |
---|
185 | self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE) |
---|
186 | return |
---|
187 | |
---|
188 | class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage): |
---|
189 | """An applicant-centered edit view for applicant data. |
---|
190 | """ |
---|
191 | |
---|
192 | def dataNotComplete(self, data): |
---|
193 | store = getUtility(IExtFileStore) |
---|
194 | if self.context.__parent__.with_picture: |
---|
195 | store = getUtility(IExtFileStore) |
---|
196 | if not store.getFileByContext(self.context, attr=u'passport.jpg'): |
---|
197 | return _('No passport picture uploaded.') |
---|
198 | if not self.request.form.get('confirm_passport', False): |
---|
199 | return _('Passport picture confirmation box not ticked.') |
---|
200 | if self.context.subtype == 'transfer' and \ |
---|
201 | not store.getFileByContext(self.context, attr=u'res_stat.pdf'): |
---|
202 | return _('No statement of result pdf file uploaded.') |
---|
203 | return False |
---|
204 | |
---|
205 | @property |
---|
206 | def form_fields(self): |
---|
207 | if self.target is not None and self.target.startswith('pg'): |
---|
208 | form_fields = grok.AutoFields(ICustomPGApplicantEdit) |
---|
209 | for field in PG_OMIT_EDIT_FIELDS: |
---|
210 | form_fields = form_fields.omit(field) |
---|
211 | else: |
---|
212 | form_fields = grok.AutoFields(ICustomUGApplicantEdit) |
---|
213 | for field in UG_OMIT_EDIT_FIELDS: |
---|
214 | form_fields = form_fields.omit(field) |
---|
215 | form_fields['applicant_id'].for_display = True |
---|
216 | form_fields['reg_number'].for_display = True |
---|
217 | return form_fields |
---|
218 | |
---|
219 | def update(self): |
---|
220 | if self.context.locked or ( |
---|
221 | self.context.__parent__.expired and |
---|
222 | self.context.__parent__.strict_deadline): |
---|
223 | self.emit_lock_message() |
---|
224 | return |
---|
225 | super(CustomApplicantEditFormPage, self).update() |
---|
226 | upload_res_stat = self.request.form.get('form.res_stat', None) |
---|
227 | if upload_res_stat: |
---|
228 | # We got a fresh res_stat upload |
---|
229 | success = handle_file_upload( |
---|
230 | upload_res_stat, self.context, self, attr='res_stat.pdf') |
---|
231 | if not success: |
---|
232 | self.upload_success = False |
---|
233 | self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE) |
---|
234 | return |
---|
235 | |
---|
236 | class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip): |
---|
237 | |
---|
238 | @property |
---|
239 | def form_fields(self): |
---|
240 | if self.target is not None and self.target.startswith('pg'): |
---|
241 | form_fields = grok.AutoFields(ICustomPGApplicant) |
---|
242 | for field in PG_OMIT_PDF_FIELDS: |
---|
243 | form_fields = form_fields.omit(field) |
---|
244 | else: |
---|
245 | form_fields = grok.AutoFields(ICustomUGApplicant) |
---|
246 | for field in UG_OMIT_PDF_FIELDS: |
---|
247 | form_fields = form_fields.omit(field) |
---|
248 | if not getattr(self.context, 'student_id'): |
---|
249 | form_fields = form_fields.omit('student_id') |
---|
250 | if not getattr(self.context, 'screening_score'): |
---|
251 | form_fields = form_fields.omit('screening_score') |
---|
252 | if not getattr(self.context, 'screening_venue'): |
---|
253 | form_fields = form_fields.omit('screening_venue') |
---|
254 | if not getattr(self.context, 'screening_date'): |
---|
255 | form_fields = form_fields.omit('screening_date') |
---|
256 | return form_fields |
---|
257 | |
---|
258 | class ResultStatement(grok.View): |
---|
259 | """Renders the pdf form extension for applicants. |
---|
260 | """ |
---|
261 | grok.name('res_stat.pdf') |
---|
262 | grok.context(ICustomApplicant) |
---|
263 | grok.require('waeup.viewApplication') |
---|
264 | |
---|
265 | def render(self): |
---|
266 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
267 | self.context, attr='res_stat.pdf') |
---|
268 | self.response.setHeader('Content-Type', 'application/pdf') |
---|
269 | return pdf |
---|