Project

General

Profile

0004-change-signature-for-UploadWidget.validation-paramet.patch

Benjamin Dauvergne, 28 October 2015 01:13 PM

Download (4.12 KB)

View differences:

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(-)
wcs/qommon/form.py
504 504
        return self.get_file().read()
505 505

  
506 506

  
507
class UploadValidationError(Exception):
508
    pass
509

  
510

  
507 511
class UploadWidget(CompositeWidget):
508 512
    def __init__(self, name, value=None, directory=None, filename=None,
509 513
            validation=None, **kwargs):
......
529 533
            upload = self.get('file')
530 534
            self.value = UploadedFile(self.directory, self.filename, upload)
531 535
            if self.validation:
532
                valid, msg = self.validation(upload)
533
                if not valid:
534
                    self.error = msg
536
                try:
537
                    self.validation(upload)
538
                except UploadValidationError, e:
539
                    self.error = str(e)
535 540
        else:
536 541
            self.value = None
537 542

  
wcs/wf/export_to_model.py
25 25
from qommon import ezt
26 26
from qommon.form import (SingleSelectWidget, WidgetList, CheckboxWidget,
27 27
                         StringWidget, UploadWidget, WysiwygTextWidget, Upload,
28
                         UploadedFile)
28
                         UploadedFile, UploadValidationError)
29 29
from qommon.errors import PublishError
30 30
import qommon
31 31

  
......
186 186
        elif hasattr(upload, 'get_file'):
187 187
            fp = upload.get_file()
188 188
        else:
189
            raise Exception('unknown upload %r' % upload)
189
            raise UploadValidationError('unknown upload object %r' % upload)
190 190
        if upload.content_type and upload.content_type == 'application/rtf':
191
            return 'rtf', ''
191
            return 'rtf'
192 192
        if (upload.content_type and upload.content_type == 'application/octet-stream') or \
193 193
                upload.content_type is None:
194 194
            if upload.base_filename and upload.base_filename.endswith('.rtf'):
195
                return 'rtf', ''
195
                return 'rtf'
196 196
        if fp.read(10).startswith('{\\rtf'):
197 197
            fp.seek(0)
198
            return 'rtf', ''
198
            return 'rtf'
199 199
        fp.seek(0)
200 200
        if upload.content_type and upload.content_type.startswith('application/vnd.oasis.opendocument.'):
201
            return 'opendocument', ''
201
            return 'opendocument'
202 202
        if (upload.content_type and upload.content_type == 'application/octet-stream') or \
203 203
                upload.content_type is None:
204 204
            if upload.base_filename and upload.base_filename.rsplit('.', 1) in ('odt', 'ods', 'odc', 'odb'):
205
                return 'opendocument', ''
205
                return 'opendocument'
206 206
        if is_opendocument(fp):
207
            return 'opendocument', ''
208
        return False, _('Only RTF and OpenDocument files can be used')
207
            return 'opendocument'
208
        raise UploadValidationError(_('Only RTF and OpenDocument files can be used'))
209 209

  
210 210
    def add_parameters_widgets(self, form, parameters, prefix='',
211 211
                               formdef=None):
......
267 267

  
268 268
    def apply_template_to_formdata(self, formdata):
269 269
        assert self.model_file
270
        kind, msg = self.model_file_validation(self.model_file)
270
        kind = self.model_file_validation(self.model_file)
271 271
        if kind == 'rtf':
272 272
            return self.apply_rtf_template_to_formdata(formdata)
273 273
        elif kind == 'opendocument':
274
-