From 5803de314e2027e0869013042dbb2918699d02c9 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 28 Oct 2015 13:10:14 +0100 Subject: [PATCH 4/4] change signature for UploadWidget.validation parameter (#4292) It should be a method raising qommon.forms.UploadValidationError if the validation fails. Return value is ignored. --- wcs/qommon/form.py | 11 ++++++++--- wcs/wf/export_to_model.py | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index f4bb350..db493d4 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -504,6 +504,10 @@ class UploadedFile: #pylint: disable=C1001 return self.get_file().read() +class UploadValidationError(Exception): + pass + + class UploadWidget(CompositeWidget): def __init__(self, name, value=None, directory=None, filename=None, validation=None, **kwargs): @@ -529,9 +533,10 @@ class UploadWidget(CompositeWidget): upload = self.get('file') self.value = UploadedFile(self.directory, self.filename, upload) if self.validation: - valid, msg = self.validation(upload) - if not valid: - self.error = msg + try: + self.validation(upload) + except UploadValidationError, e: + self.error = str(e) else: self.value = None diff --git a/wcs/wf/export_to_model.py b/wcs/wf/export_to_model.py index 66bff3b..ea026cb 100644 --- a/wcs/wf/export_to_model.py +++ b/wcs/wf/export_to_model.py @@ -25,7 +25,7 @@ from qommon import get_logger from qommon import ezt from qommon.form import (SingleSelectWidget, WidgetList, CheckboxWidget, StringWidget, UploadWidget, WysiwygTextWidget, Upload, - UploadedFile) + UploadedFile, UploadValidationError) from qommon.errors import PublishError import qommon @@ -186,26 +186,26 @@ class ExportToModel(WorkflowStatusItem): elif hasattr(upload, 'get_file'): fp = upload.get_file() else: - raise Exception('unknown upload %r' % upload) + raise UploadValidationError('unknown upload object %r' % upload) if upload.content_type and upload.content_type == 'application/rtf': - return 'rtf', '' + return 'rtf' if (upload.content_type and upload.content_type == 'application/octet-stream') or \ upload.content_type is None: if upload.base_filename and upload.base_filename.endswith('.rtf'): - return 'rtf', '' + return 'rtf' if fp.read(10).startswith('{\\rtf'): fp.seek(0) - return 'rtf', '' + return 'rtf' fp.seek(0) if upload.content_type and upload.content_type.startswith('application/vnd.oasis.opendocument.'): - return 'opendocument', '' + return 'opendocument' if (upload.content_type and upload.content_type == 'application/octet-stream') or \ upload.content_type is None: if upload.base_filename and upload.base_filename.rsplit('.', 1) in ('odt', 'ods', 'odc', 'odb'): - return 'opendocument', '' + return 'opendocument' if is_opendocument(fp): - return 'opendocument', '' - return False, _('Only RTF and OpenDocument files can be used') + return 'opendocument' + raise UploadValidationError(_('Only RTF and OpenDocument files can be used')) def add_parameters_widgets(self, form, parameters, prefix='', formdef=None): @@ -267,7 +267,7 @@ class ExportToModel(WorkflowStatusItem): def apply_template_to_formdata(self, formdata): assert self.model_file - kind, msg = self.model_file_validation(self.model_file) + kind = self.model_file_validation(self.model_file) if kind == 'rtf': return self.apply_rtf_template_to_formdata(formdata) elif kind == 'opendocument': -- 2.1.4