Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for 'Keep existing file' #42

Merged
merged 3 commits into from Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions news/41.bugfix
@@ -0,0 +1 @@
set widget value to z3c.form.interfaces.NOT_CHANGED when the user selects 'Keep Existing File' in the widget, so z3c.form understands the field has not changed
6 changes: 5 additions & 1 deletion plone/formwidget/namedfile/converter.py
Expand Up @@ -8,6 +8,7 @@
from plone.namedfile.interfaces import INamedField
from plone.namedfile.utils import safe_basename
from z3c.form.converter import BaseDataConverter
from z3c.form.interfaces import NOT_CHANGED
from zope.component import adapts
from zope.schema.interfaces import IBytes
from ZPublisher.HTTPRequest import FileUpload
Expand All @@ -25,7 +26,10 @@ def toWidgetValue(self, value):
return value

def toFieldValue(self, value):

action = self.widget.request.get("%s.action" % self.widget.name, None)
if action == 'nochange':
return NOT_CHANGED

if value is None or value == '':
return self.field.missing_value

Expand Down
21 changes: 19 additions & 2 deletions plone/formwidget/namedfile/widget.rst
Expand Up @@ -451,12 +451,18 @@ instances and the two named file/image widgets::

>>> from zope.component import getMultiAdapter
>>> from z3c.form.interfaces import IDataConverter

>>> from z3c.form.interfaces import NOT_CHANGED

>>> file_converter = getMultiAdapter((IContent['file_field'], file_widget), IDataConverter)
>>> image_converter = getMultiAdapter((IContent['image_field'], image_widget), IDataConverter)

A value of None or '' results in the field's missing_value being returned::
An initial upload of a file will never include the action field,
so let's remove it from our test requests

>>> del file_widget.request.form['widget.name.file.action']
>>> del image_widget.request.form['widget.name.image.action']

A value of None or '' results in the field's missing_value being returned::
>>> file_converter.toFieldValue(u'') is IContent['file_field'].missing_value
True
>>> file_converter.toFieldValue(None) is IContent['file_field'].missing_value
Expand Down Expand Up @@ -526,6 +532,17 @@ being returned::
>>> field_value is IContent['image_field'].missing_value
True

If the file has already been uploaded and the user selects 'Keep Existing File'
in the widget, the widget will include 'action':'nochange' in the form post,
and the converter will always set the value to z3c.form.interfaces.NOT_CHANGED::

>>> file_widget.request.form['widget.name.file.action'] = 'nochange'
>>> file_converter.toFieldValue(u'') is NOT_CHANGED
True
>>> image_widget.request.form['widget.name.image.action'] = 'nochange'
>>> image_converter.toFieldValue(u'') is NOT_CHANGED
True


The Base64Converter for Bytes fields
------------------------------------
Expand Down