- Timestamp:
- 9 Dec 2019, 21:39:51 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/kofacustom.iuokada/trunk/src/kofacustom/iuokada/applicants/browser.py
r15809 r15878 19 19 """ 20 20 import grok 21 import os 22 from zope.component import getUtility 21 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 22 27 from waeup.kofa.applicants.browser import ( 23 28 ApplicantRegistrationPage, ApplicantsContainerPage) … … 43 48 ) 44 49 from kofacustom.iuokada.applicants.interfaces import ( 45 ICustomPGApplicant, ICustomUGApplicant, 50 ICustomPGApplicant, ICustomUGApplicant, ICustomApplicant, 46 51 ICustomPGApplicantEdit, ICustomUGApplicantEdit, 47 52 ICustomApplicantOnlinePayment 48 53 ) 49 54 from kofacustom.iuokada.interfaces import MessageFactory as _ 55 56 MAX_FILE_UPLOAD_SIZE = 1024 * 500 57 58 def handle_file_upload(upload, context, view, attr=None): 59 """Handle upload of applicant files. 60 61 Returns `True` in case of success or `False`. 62 63 Please note that file pointer passed in (`upload`) most probably 64 points to end of file when leaving this function. 65 """ 66 size = file_size(upload) 67 if size > MAX_FILE_UPLOAD_SIZE: 68 view.flash(_('Uploaded file is too big!')) 69 return False 70 dummy, ext = os.path.splitext(upload.filename) 71 ext.lower() 72 if ext != '.pdf': 73 view.flash(_('pdf file extension expected.')) 74 return False 75 upload.seek(0) # file pointer moved when determining size 76 store = getUtility(IExtFileStore) 77 file_id = IFileStoreNameChooser(context).chooseName(attr=attr) 78 store.createFile(file_id, upload) 79 return True 50 80 51 81 class CustomApplicantsContainerPage(ApplicantsContainerPage): … … 73 103 74 104 @property 105 def file_links(self): 106 html = '' 107 pdf = getUtility(IExtFileStore).getFileByContext( 108 self.context, attr='stateresult.pdf') 109 if pdf: 110 html += '<a href="%s">Statement of Result</a>' % self.url( 111 self.context, 'stateresult.pdf') 112 return html 113 114 @property 75 115 def form_fields(self): 76 116 if self.target is not None and self.target.startswith('pg'): … … 112 152 return form_fields 113 153 154 def update(self): 155 super(CustomApplicantManageFormPage, self).update() 156 upload_stateresult = self.request.form.get('form.stateresult', None) 157 if upload_stateresult: 158 # We got a fresh stateresult upload 159 success = handle_file_upload( 160 upload_stateresult, self.context, self, attr='stateresult.pdf') 161 if success: 162 self.context.writeLogMessage(self, 'saved: stateresult') 163 else: 164 self.upload_success = False 165 self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE) 166 return 167 114 168 class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage): 115 169 """An applicant-centered edit view for applicant data. 116 170 """ 117 171 172 def dataNotComplete(self, data): 173 store = getUtility(IExtFileStore) 174 if self.context.__parent__.with_picture: 175 store = getUtility(IExtFileStore) 176 if not store.getFileByContext(self.context, attr=u'passport.jpg'): 177 return _('No passport picture uploaded.') 178 if not self.request.form.get('confirm_passport', False): 179 return _('Passport picture confirmation box not ticked.') 180 if self.context.subtype == 'transfer' and \ 181 not store.getFileByContext(self.context, attr=u'stateresult.pdf'): 182 return _('No statement of result pdf file uploaded.') 183 return False 184 118 185 @property 119 186 def form_fields(self): … … 129 196 form_fields['reg_number'].for_display = True 130 197 return form_fields 198 199 def update(self): 200 if self.context.locked or ( 201 self.context.__parent__.expired and 202 self.context.__parent__.strict_deadline): 203 self.emit_lock_message() 204 return 205 super(CustomApplicantEditFormPage, self).update() 206 upload_stateresult = self.request.form.get('form.stateresult', None) 207 if upload_stateresult: 208 # We got a fresh stateresult upload 209 success = handle_file_upload( 210 upload_stateresult, self.context, self, attr='stateresult.pdf') 211 if not success: 212 self.upload_success = False 213 self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE) 214 return 131 215 132 216 class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip): … … 152 236 return form_fields 153 237 238 class StateResult(grok.View): 239 """Renders the pdf form extension for applicants. 240 """ 241 grok.name('stateresult.pdf') 242 grok.context(ICustomApplicant) 243 grok.require('waeup.viewApplication') 244 245 def render(self): 246 pdf = getUtility(IExtFileStore).getFileByContext( 247 self.context, attr='stateresult.pdf') 248 self.response.setHeader('Content-Type', 'application/pdf') 249 return pdf
Note: See TracChangeset for help on using the changeset viewer.