Changeset 12223
- Timestamp:
- 14 Dec 2014, 08:49:01 (10 years ago)
- Location:
- main/waeup.ikoba/trunk/src/waeup/ikoba
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.ikoba/trunk/src/waeup/ikoba/customers/fileviewlets.py
r12062 r12223 31 31 default_fileupload_template) 32 32 33 from waeup.ikoba.browser.fileviewlets import ( 34 FileDisplay, FileUpload, Image) 35 33 36 from waeup.ikoba.customers.interfaces import ( 34 37 ICustomer, ICustomersUtils) … … 43 46 PDFDocumentSlipPage,) 44 47 45 grok.context(IIkobaObject) # Make IIkobaObject the default context46 48 grok.templatedir('browser_templates') 47 48 ALLOWED_FILE_EXTENSIONS = ('jpg', 'png', 'pdf', 'tif', 'fpm')49 50 51 def handle_file_delete(context, view, download_name):52 """Handle deletion of customer file.53 54 """55 store = getUtility(IExtFileStore)56 store.deleteFileByContext(context, attr=download_name)57 context.writeLogMessage(view, 'deleted: %s' % download_name)58 view.flash(_('${a} deleted.', mapping={'a': download_name}))59 return60 61 62 def handle_file_upload(upload, context, view, max_size, download_name=None):63 """Handle upload of customer file.64 65 Returns `True` in case of success or `False`.66 67 Please note that file pointer passed in (`upload`) most probably68 points to end of file when leaving this function.69 """70 # Check some file requirements first71 size = file_size(upload)72 if size > max_size:73 view.flash(_('Uploaded file is too big.'), type="danger")74 return False75 upload.seek(0) # file pointer moved when determining size76 dummy,ext = os.path.splitext(upload.filename)77 # fpm files are expected to be fingerprint minutiae, file78 # format is not yet checked79 if ext == '.fpm':80 file_format = 'fpm'81 else:82 file_format = get_fileformat(None, upload.read(512))83 upload.seek(0) # same here84 if file_format is None:85 view.flash(_('Could not determine file type.'), type="danger")86 return False87 basename, expected_ext = os.path.splitext(download_name)88 if expected_ext:89 if '.' + file_format != expected_ext:90 view.flash(_('${a} file extension expected.',91 mapping={'a': expected_ext[1:]}), type="danger")92 return False93 else:94 if not file_format in ALLOWED_FILE_EXTENSIONS:95 view.flash(96 _('Only the following extensions are allowed: ${a}',97 mapping={'a': ', '.join(ALLOWED_FILE_EXTENSIONS)}),98 type="danger")99 return False100 download_name += '.' + file_format101 store = getUtility(IExtFileStore)102 file_id = IFileStoreNameChooser(context).chooseName(attr=download_name)103 store.createFile(file_id, upload)104 context.writeLogMessage(view, 'uploaded: %s (%s)' % (105 download_name,upload.filename))106 view.flash(_('File ${a} uploaded.', mapping={'a': download_name}))107 return True108 109 110 class FileManager(grok.ViewletManager):111 """Viewlet manager for uploading files, preferably scanned images.112 """113 grok.name('files')114 115 116 class FileDisplay(grok.Viewlet):117 """Base file display viewlet.118 """119 grok.baseclass()120 grok.viewletmanager(FileManager)121 template = default_filedisplay_template122 grok.order(1)123 label = _(u'File')124 title = _(u'Scan')125 download_name = u'filename.jpg'126 127 @property128 def file_exists(self):129 image = getUtility(IExtFileStore).getFileByContext(130 self.context, attr=self.download_name)131 if image:132 return True133 else:134 return False135 136 137 class FileUpload(FileDisplay):138 """Base upload viewlet.139 """140 grok.baseclass()141 grok.viewletmanager(FileManager)142 template = default_fileupload_template143 tab_redirect = '#tab2-top'144 mus = 1024 * 150145 upload_button =_('Upload selected file')146 delete_button = _('Delete')147 148 @property149 def show_viewlet(self):150 customers_utils = getUtility(ICustomersUtils)151 if self.__name__ in customers_utils.SKIP_UPLOAD_VIEWLETS:152 return False153 return True154 155 @property156 def input_name(self):157 return "%s" % self.__name__158 159 def update(self):160 self.max_upload_size = string_from_bytes(self.mus)161 delete_button = self.request.form.get(162 'delete_%s' % self.input_name, None)163 upload_button = self.request.form.get(164 'upload_%s' % self.input_name, None)165 if delete_button:166 handle_file_delete(167 context=self.context, view=self.view,168 download_name=self.download_name)169 self.view.redirect(170 self.view.url(171 self.context, self.view.__name__) + self.tab_redirect)172 return173 if upload_button:174 upload = self.request.form.get(self.input_name, None)175 if upload:176 # We got a fresh upload177 handle_file_upload(upload,178 self.context, self.view, self.mus, self.download_name)179 self.view.redirect(180 self.view.url(181 self.context, self.view.__name__) + self.tab_redirect)182 else:183 self.view.flash(_('No local file selected.'), type="danger")184 self.view.redirect(185 self.view.url(186 self.context, self.view.__name__) + self.tab_redirect)187 return188 189 190 class Image(grok.View):191 """Renders images for customers.192 """193 grok.baseclass()194 grok.name('none.jpg')195 download_name = u'none.jpg'196 197 def render(self):198 # A filename chooser turns a context into a filename suitable199 # for file storage.200 image = getUtility(IExtFileStore).getFileByContext(201 self.context, attr=self.download_name)202 if image is None:203 # show placeholder image204 self.response.setHeader('Content-Type', 'image/jpeg')205 return open(DEFAULT_IMAGE_PATH, 'rb').read()206 dummy,ext = os.path.splitext(image.name)207 if ext == '.jpg':208 self.response.setHeader('Content-Type', 'image/jpeg')209 elif ext == '.fpm':210 self.response.setHeader('Content-Type', 'application/binary')211 elif ext == '.png':212 self.response.setHeader('Content-Type', 'image/png')213 elif ext == '.pdf':214 self.response.setHeader('Content-Type', 'application/pdf')215 elif ext == '.tif':216 self.response.setHeader('Content-Type', 'image/tiff')217 return image218 49 219 50 # File viewlets for customer base page -
main/waeup.ikoba/trunk/src/waeup/ikoba/customers/utils.py
r12214 r12223 228 228 """ 229 229 # XXX: fix circular import problem 230 from waeup.ikoba. customers.fileviewlets import FileManager230 from waeup.ikoba.browser.fileviewlets import FileManager 231 231 from waeup.ikoba.browser import DEFAULT_IMAGE_PATH 232 232 style = getSampleStyleSheet() … … 297 297 CONMANAGE_CONTRACT_STATES = (CREATED,) 298 298 299 SKIP_UPLOAD_VIEWLETS = ()300 301 299 TRANSLATED_CUSTOMER_STATES = { 302 300 CREATED: _('created'),
Note: See TracChangeset for help on using the changeset viewer.