Add the possibility to keep the actual value to the UploadWidget
It looks ever-more like FileWithPreviewWidget, but I'm a bit afraid to touch this one; seems there is a lot of legacy around it that I would not want to break.
This commit is contained in:
parent
aee0f8e5fe
commit
290221c526
|
@ -314,22 +314,36 @@ class UploadedFile:
|
|||
return self.get_file().read()
|
||||
|
||||
|
||||
class UploadWidget(FileWidget):
|
||||
def __init__(self, name, directory=None, filename=None, validation=None, **kwargs):
|
||||
class UploadWidget(CompositeWidget):
|
||||
def __init__(self, name, value=None, directory=None, filename=None,
|
||||
validation=None, **kwargs):
|
||||
super(CompositeWidget, self).__init__(name, **kwargs)
|
||||
del kwargs['title']
|
||||
kwargs.pop('hint')
|
||||
self.value = value
|
||||
self.directory = directory or 'uploads'
|
||||
self.filename = filename
|
||||
self.validation = validation
|
||||
super(UploadWidget, self).__init__(name, **kwargs)
|
||||
if value:
|
||||
self.add(CheckboxWidget, 'orexisting',
|
||||
title = _('Use previous file.'), value=True);
|
||||
self.widgets[-1].render_br = False
|
||||
self.add(FileWidget, 'file', title = _('Or upload a new one'), **kwargs)
|
||||
else:
|
||||
self.add(FileWidget, 'file', **kwargs)
|
||||
|
||||
def _parse(self, request):
|
||||
super(UploadWidget, self)._parse(request)
|
||||
if not self.value:
|
||||
return
|
||||
if self.validation:
|
||||
valid, msg = self.validation(self.value)
|
||||
if not valid:
|
||||
self.error = msg
|
||||
self.value = UploadedFile(self.directory, self.filename, self.value)
|
||||
if self.value and self.get('orexisting'):
|
||||
pass
|
||||
elif self.get('file'):
|
||||
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
|
||||
else:
|
||||
self.value = None
|
||||
|
||||
class FileWithPreviewWidget(CompositeWidget):
|
||||
"""Widget that proposes a File Upload widget but that stores the file
|
||||
|
|
Loading…
Reference in New Issue