Changeset 12448 for main/waeup.kofa/trunk
- Timestamp:
- 12 Jan 2015, 11:00:55 (10 years ago)
- Location:
- main/waeup.kofa/trunk/src/waeup/kofa
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/documents/fileviewlets.py
r12447 r12448 20 20 from waeup.kofa.interfaces import MessageFactory as _ 21 21 from waeup.kofa.browser.layout import UtilityView 22 from waeup.kofa. students.fileviewlets import (22 from waeup.kofa.browser.fileviewlets import ( 23 23 FileDisplay, FileUpload, Image) 24 24 -
main/waeup.kofa/trunk/src/waeup/kofa/students/fileviewlets.py
r12447 r12448 28 28 from waeup.kofa.students.interfaces import IStudent, IStudentsUtils 29 29 30 from waeup.kofa.browser.fileviewlets import ( 31 FileDisplay, FileUpload, Image) 32 30 33 from waeup.kofa.browser.layout import ( 31 34 default_filedisplay_template, … … 37 40 ExportPDFClearanceSlipPage, StudentFilesUploadPage) 38 41 39 from waeup.kofa.utils.helpers import get_fileformat40 41 42 grok.context(IKofaObject) # Make IKofaObject the default context 42 43 grok.templatedir('browser_templates') 43 44 44 ALLOWED_FILE_EXTENSIONS = ('jpg', 'png', 'pdf', 'tif', 'fpm') 45 # File viewlet baseclasses for student base page 45 46 46 def handle_file_delete(context, view, download_name): 47 """Handle deletion of student file. 48 49 """ 50 store = getUtility(IExtFileStore) 51 store.deleteFileByContext(context, attr=download_name) 52 context.writeLogMessage(view, 'deleted: %s' % download_name) 53 view.flash(_('${a} deleted.', mapping = {'a':download_name})) 54 return 55 56 def handle_file_upload(upload, context, view, max_size, download_name=None): 57 """Handle upload of student file. 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 # Check some file requirements first 65 size = file_size(upload) 66 if size > max_size: 67 view.flash(_('Uploaded file is too big.'), type="danger") 68 return False 69 upload.seek(0) # file pointer moved when determining size 70 dummy,ext = os.path.splitext(upload.filename) 71 # fpm files are expected to be fingerprint minutiae, file 72 # format is not yet checked 73 if ext == '.fpm': 74 file_format = 'fpm' 75 else: 76 file_format = get_fileformat(None, upload.read(512)) 77 upload.seek(0) # same here 78 if file_format is None: 79 view.flash(_('Could not determine file type.'), type="danger") 80 return False 81 basename, expected_ext = os.path.splitext(download_name) 82 if expected_ext: 83 if '.' + file_format != expected_ext: 84 view.flash(_('${a} file format expected.', 85 mapping = {'a':expected_ext[1:]}), type="danger") 86 return False 87 else: 88 if not file_format in ALLOWED_FILE_EXTENSIONS: 89 view.flash( 90 _('Only the following extensions are allowed: ${a}', 91 mapping = {'a':', '.join(ALLOWED_FILE_EXTENSIONS)}), 92 type="danger") 93 return False 94 download_name += '.' + file_format 95 store = getUtility(IExtFileStore) 96 file_id = IFileStoreNameChooser(context).chooseName(attr=download_name) 97 store.createFile(file_id, upload) 98 context.writeLogMessage(view, 'uploaded: %s (%s)' % ( 99 download_name,upload.filename)) 100 view.flash(_('File ${a} uploaded.', mapping = {'a':download_name})) 101 return True 102 103 # File viewlets for student base page 104 105 class FileManager(grok.ViewletManager): 106 """Viewlet manager for uploading files, preferably scanned images. 107 """ 108 grok.name('files') 109 110 class FileDisplay(grok.Viewlet): 47 class StudentFileDisplay(FileDisplay): 111 48 """Base file display viewlet. 112 49 """ 113 50 grok.baseclass() 114 51 grok.context(IStudent) 115 grok.viewletmanager(FileManager)116 52 grok.view(StudentClearanceDisplayFormPage) 117 template = default_filedisplay_template118 53 grok.order(1) 119 54 grok.require('waeup.viewStudent') 120 label = _(u'File')121 title = _(u'Scan')122 55 123 @property124 def download_filename(self):125 return self.download_name126 56 127 @property 128 def file_exists(self): 129 image = getUtility(IExtFileStore).getFileByContext( 130 self.context, attr=self.download_name) 131 if image: 132 return True 133 else: 134 return False 135 136 class FileUpload(FileDisplay): 57 class StudentFileUpload(FileUpload): 137 58 """Base upload viewlet. 138 59 """ 139 60 grok.baseclass() 140 61 grok.context(IStudent) 141 grok.viewletmanager(FileManager)142 62 grok.view(StudentClearanceManageFormPage) 143 template = default_fileupload_template144 63 grok.require('waeup.uploadStudentFile') 145 tab_redirect = '#tab2-top'146 mus = 1024 * 150147 upload_button =_('Upload selected file')148 delete_button = _('Delete')149 64 150 65 @property … … 155 70 return True 156 71 157 @property158 def input_name(self):159 return "%s" % self.__name__160 72 161 def update(self): 162 self.max_upload_size = string_from_bytes(self.mus) 163 delete_button = self.request.form.get( 164 'delete_%s' % self.input_name, None) 165 upload_button = self.request.form.get( 166 'upload_%s' % self.input_name, None) 167 if delete_button: 168 handle_file_delete( 169 context=self.context, view=self.view, 170 download_name=self.download_name) 171 self.view.redirect( 172 self.view.url( 173 self.context, self.view.__name__) + self.tab_redirect) 174 return 175 if upload_button: 176 upload = self.request.form.get(self.input_name, None) 177 if upload: 178 # We got a fresh upload 179 handle_file_upload(upload, 180 self.context, self.view, self.mus, self.download_name) 181 self.view.redirect( 182 self.view.url( 183 self.context, self.view.__name__) + self.tab_redirect) 184 else: 185 self.view.flash(_('No local file selected.'), type="danger") 186 self.view.redirect( 187 self.view.url( 188 self.context, self.view.__name__) + self.tab_redirect) 189 return 73 class StudentImage(Image): 74 """Renders images for students. 75 """ 76 grok.baseclass() 77 grok.context(IStudent) 78 grok.require('waeup.viewStudent') 190 79 191 class PassportDisplay(FileDisplay): 80 81 # File viewlets for student base page 82 83 class PassportDisplay(StudentFileDisplay): 192 84 """Passport display viewlet. 193 85 """ … … 200 92 download_name = u'passport.jpg' 201 93 202 class PassportUploadManage(FileUpload): 94 95 class PassportUploadManage(StudentFileUpload): 203 96 """Passport upload viewlet for officers. 204 97 """ … … 213 106 tab_redirect = '#tab2' 214 107 108 215 109 class PassportUploadEdit(PassportUploadManage): 216 110 """Passport upload viewlet for students. … … 219 113 grok.require('waeup.uploadStudentFile') 220 114 221 class BirthCertificateDisplay(FileDisplay): 115 116 class BirthCertificateDisplay(StudentFileDisplay): 222 117 """Birth Certificate display viewlet. 223 118 """ … … 227 122 download_name = u'birth_certificate' 228 123 124 229 125 class BirthCertificateSlip(BirthCertificateDisplay): 230 126 grok.view(ExportPDFClearanceSlipPage) 231 127 232 class BirthCertificateUpload(FileUpload): 128 129 class BirthCertificateUpload(StudentFileUpload): 233 130 """Birth Certificate upload viewlet. 234 131 """ … … 240 137 tab_redirect = '#tab2-top' 241 138 242 class Image(grok.View):243 """Renders images for students.244 """245 grok.baseclass()246 grok.context(IStudent)247 grok.require('waeup.viewStudent')248 139 249 @property 250 def download_filename(self): 251 return self.download_name 252 253 def render(self): 254 # A filename chooser turns a context into a filename suitable 255 # for file storage. 256 image = getUtility(IExtFileStore).getFileByContext( 257 self.context, attr=self.download_name) 258 if image is None: 259 # show placeholder image 260 self.response.setHeader('Content-Type', 'image/jpeg') 261 return open(DEFAULT_IMAGE_PATH, 'rb').read() 262 dummy,ext = os.path.splitext(image.name) 263 if ext == '.jpg': 264 self.response.setHeader('Content-Type', 'image/jpeg') 265 elif ext == '.fpm': 266 self.response.setHeader('Content-Type', 'application/binary') 267 elif ext == '.png': 268 self.response.setHeader('Content-Type', 'image/png') 269 elif ext == '.tif': 270 self.response.setHeader('Content-Type', 'image/tiff') 271 elif ext == '.pdf': 272 self.response.setHeader('Content-Type', 'application/pdf') 273 self.response.setHeader('Content-Disposition', 274 'attachment; filename="%s.pdf' % self.download_filename) 275 return image 276 277 class Passport(Image): 140 class Passport(StudentImage): 278 141 """Renders jpeg passport picture. 279 142 """ … … 282 145 grok.context(IStudent) 283 146 284 class ApplicationSlipImage(Image): 147 148 class ApplicationSlipImage(StudentImage): 285 149 """Renders application slip scan. 286 150 """ … … 288 152 download_name = u'application_slip' 289 153 290 class BirthCertificateImage(Image): 154 155 class BirthCertificateImage(StudentImage): 291 156 """Renders birth certificate scan. 292 157 """ -
main/waeup.kofa/trunk/src/waeup/kofa/students/utils.py
r12421 r12448 281 281 """ 282 282 # XXX: fix circular import problem 283 from waeup.kofa. students.fileviewlets import FileManager283 from waeup.kofa.browser.fileviewlets import FileManager 284 284 from waeup.kofa.browser import DEFAULT_IMAGE_PATH 285 285 style = getSampleStyleSheet()
Note: See TracChangeset for help on using the changeset viewer.