[10088] | 1 | ## $Id: browser.py 10275 2013-06-04 16:16:24Z 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 |
---|
[10096] | 21 | import os |
---|
| 22 | from zope.component import getUtility |
---|
| 23 | from waeup.kofa.interfaces import ( |
---|
| 24 | IExtFileStore, IFileStoreNameChooser) |
---|
[10088] | 25 | from zope.formlib.textwidgets import BytesDisplayWidget |
---|
[10096] | 26 | from waeup.kofa.utils.helpers import string_from_bytes, file_size |
---|
[10088] | 27 | from waeup.futminna.applicants.interfaces import ( |
---|
[10096] | 28 | ICustomApplicant, |
---|
[10088] | 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, |
---|
[10224] | 40 | PG_OMIT_EDIT_FIELDS, |
---|
[10206] | 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, |
---|
[10088] | 46 | ) |
---|
[10096] | 47 | from waeup.futminna.interfaces import MessageFactory as _ |
---|
[10088] | 48 | from kofacustom.nigeria.applicants.browser import ( |
---|
| 49 | NigeriaApplicantDisplayFormPage, |
---|
| 50 | NigeriaApplicantManageFormPage, |
---|
| 51 | NigeriaApplicantEditFormPage, |
---|
| 52 | NigeriaPDFApplicationSlip) |
---|
| 53 | |
---|
[10096] | 54 | MAX_FILE_UPLOAD_SIZE = 1024 * 500 |
---|
[10088] | 55 | |
---|
[10096] | 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 | |
---|
[10088] | 79 | class CustomApplicantDisplayFormPage(NigeriaApplicantDisplayFormPage): |
---|
| 80 | """A display view for applicant data. |
---|
| 81 | """ |
---|
| 82 | |
---|
| 83 | grok.template('applicantdisplaypage') |
---|
| 84 | |
---|
| 85 | @property |
---|
[10096] | 86 | def file_links(self): |
---|
| 87 | html = '' |
---|
| 88 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
[10105] | 89 | self.context, attr='extraform.pdf') |
---|
[10096] | 90 | if pdf: |
---|
[10105] | 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: |
---|
[10220] | 95 | html += '<a href="refereeform.pdf">Referee\'s Form</a>, ' |
---|
| 96 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
[10275] | 97 | self.context, attr='credentials.pdf') |
---|
[10220] | 98 | if pdf: |
---|
[10275] | 99 | html += '<a href="credentials.pdf">Credentials</a>' |
---|
[10096] | 100 | return html |
---|
| 101 | |
---|
| 102 | @property |
---|
[10088] | 103 | def form_fields(self): |
---|
[10224] | 104 | if self.target is not None and self.target.startswith('pg'): |
---|
[10088] | 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) |
---|
[10096] | 112 | if form_fields.get('perm_address', None): |
---|
| 113 | form_fields['perm_address'].custom_widget = BytesDisplayWidget |
---|
[10088] | 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') |
---|
[10206] | 123 | if not self.context.low_jamb_score: |
---|
| 124 | form_fields = form_fields.omit('course2') |
---|
[10088] | 125 | return form_fields |
---|
| 126 | |
---|
[10096] | 127 | def update(self): |
---|
| 128 | super(CustomApplicantDisplayFormPage, self).update() |
---|
[10105] | 129 | self.extraform_url = self.url(self.context, 'extraform.pdf') |
---|
| 130 | self.referreeform_url = self.url(self.context, 'refereeform.pdf') |
---|
[10275] | 131 | self.credentials_url = self.url(self.context, 'credentials.pdf') |
---|
[10096] | 132 | return |
---|
| 133 | |
---|
[10088] | 134 | class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip): |
---|
| 135 | |
---|
| 136 | @property |
---|
| 137 | def form_fields(self): |
---|
[10224] | 138 | if self.target is not None and self.target.startswith('pg'): |
---|
[10088] | 139 | form_fields = grok.AutoFields(ICustomPGApplicant) |
---|
| 140 | for field in PG_OMIT_PDF_FIELDS: |
---|
| 141 | form_fields = form_fields.omit(field) |
---|
[10224] | 142 | elif self.target is not None and self.target.startswith('putme'): |
---|
[10206] | 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) |
---|
[10088] | 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') |
---|
[10206] | 159 | if not self.context.low_jamb_score: |
---|
| 160 | form_fields = form_fields.omit('course2') |
---|
[10088] | 161 | return form_fields |
---|
| 162 | |
---|
| 163 | class CustomApplicantManageFormPage(NigeriaApplicantManageFormPage): |
---|
| 164 | """A full edit view for applicant data. |
---|
| 165 | """ |
---|
[10096] | 166 | grok.template('applicanteditpage') |
---|
| 167 | |
---|
[10088] | 168 | @property |
---|
| 169 | def form_fields(self): |
---|
[10224] | 170 | if self.target is not None and self.target.startswith('pg'): |
---|
[10088] | 171 | form_fields = grok.AutoFields(ICustomPGApplicant) |
---|
| 172 | for field in PG_OMIT_MANAGE_FIELDS: |
---|
| 173 | form_fields = form_fields.omit(field) |
---|
[10224] | 174 | elif self.target is not None and self.target.startswith('putme'): |
---|
[10206] | 175 | form_fields = grok.AutoFields(ICustomUGApplicant) |
---|
| 176 | for field in PUTME_OMIT_MANAGE_FIELDS: |
---|
| 177 | form_fields = form_fields.omit(field) |
---|
[10088] | 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 | |
---|
[10096] | 186 | def update(self): |
---|
| 187 | super(CustomApplicantManageFormPage, self).update() |
---|
[10105] | 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 |
---|
[10275] | 206 | upload_credentials = self.request.form.get('form.credentials', None) |
---|
| 207 | if upload_credentials: |
---|
| 208 | # We got a fresh credentials upload |
---|
[10220] | 209 | success = handle_file_upload( |
---|
[10275] | 210 | upload_credentials, self.context, self, attr='credentials.pdf') |
---|
[10220] | 211 | if success: |
---|
[10275] | 212 | self.context.writeLogMessage(self, 'saved: credentials') |
---|
[10220] | 213 | else: |
---|
| 214 | self.upload_success = False |
---|
[10096] | 215 | self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE) |
---|
| 216 | return |
---|
| 217 | |
---|
[10088] | 218 | class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage): |
---|
| 219 | """An applicant-centered edit view for applicant data. |
---|
| 220 | """ |
---|
[10096] | 221 | grok.template('applicanteditpage') |
---|
[10088] | 222 | |
---|
| 223 | @property |
---|
| 224 | def form_fields(self): |
---|
[10224] | 225 | if self.target is not None and self.target.startswith('pg'): |
---|
[10088] | 226 | form_fields = grok.AutoFields(ICustomPGApplicantEdit) |
---|
| 227 | for field in PG_OMIT_EDIT_FIELDS: |
---|
| 228 | form_fields = form_fields.omit(field) |
---|
[10224] | 229 | elif self.target is not None and self.target.startswith('putme'): |
---|
[10206] | 230 | form_fields = grok.AutoFields(ICustomUGApplicantEdit) |
---|
| 231 | for field in PUTME_OMIT_EDIT_FIELDS: |
---|
| 232 | form_fields = form_fields.omit(field) |
---|
[10088] | 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 |
---|
[10206] | 239 | if not self.context.low_jamb_score: |
---|
| 240 | form_fields = form_fields.omit('course2') |
---|
[10088] | 241 | return form_fields |
---|
[10096] | 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.') |
---|
[10105] | 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.') |
---|
[10224] | 251 | if self.target is not None and self.target.startswith('pg') \ |
---|
[10275] | 252 | and not store.getFileByContext(self.context, attr=u'credentials.pdf'): |
---|
| 253 | return _('No credentials pdf file uploaded.') |
---|
[10096] | 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() |
---|
[10105] | 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 |
---|
[10275] | 279 | upload_credentials = self.request.form.get('form.credentials', None) |
---|
| 280 | if upload_credentials: |
---|
| 281 | # We got a fresh credentials upload |
---|
[10220] | 282 | success = handle_file_upload( |
---|
[10275] | 283 | upload_credentials, self.context, self, attr='credentials.pdf') |
---|
[10220] | 284 | if not success: |
---|
| 285 | self.upload_success = False |
---|
[10096] | 286 | self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE) |
---|
| 287 | return |
---|
| 288 | |
---|
[10105] | 289 | class ExtraForm(grok.View): |
---|
[10096] | 290 | """Renders the pdf form extension for applicants. |
---|
| 291 | """ |
---|
[10105] | 292 | grok.name('extraform.pdf') |
---|
[10096] | 293 | grok.context(ICustomApplicant) |
---|
| 294 | grok.require('waeup.viewApplication') |
---|
| 295 | |
---|
| 296 | def render(self): |
---|
| 297 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
[10105] | 298 | self.context, attr='extraform.pdf') |
---|
[10096] | 299 | self.response.setHeader('Content-Type', 'application/pdf') |
---|
[10105] | 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') |
---|
[10220] | 313 | return pdf |
---|
| 314 | |
---|
[10275] | 315 | class Credentials(grok.View): |
---|
[10220] | 316 | """Renders the pdf credential's form for applicants. |
---|
| 317 | """ |
---|
[10275] | 318 | grok.name('credentials.pdf') |
---|
[10220] | 319 | grok.context(ICustomApplicant) |
---|
| 320 | grok.require('waeup.viewApplication') |
---|
| 321 | |
---|
| 322 | def render(self): |
---|
| 323 | pdf = getUtility(IExtFileStore).getFileByContext( |
---|
[10275] | 324 | self.context, attr='credentials.pdf') |
---|
[10220] | 325 | self.response.setHeader('Content-Type', 'application/pdf') |
---|
[10096] | 326 | return pdf |
---|