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