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