[Checkins] SVN: z3c.form/trunk/ - Feature: When no file is specified in the file upload widget, instead of

Stephan Richter srichter at cosmos.phy.tufts.edu
Fri Oct 3 20:42:33 EDT 2008


Log message for revision 91719:
  - Feature: When no file is specified in the file upload widget, instead of
    overwriting the value with a missing one, the old data is retained.
  

Changed:
  U   z3c.form/trunk/CHANGES.txt
  U   z3c.form/trunk/src/z3c/form/converter.py
  U   z3c.form/trunk/src/z3c/form/converter.txt
  U   z3c.form/trunk/src/z3c/form/form.py
  U   z3c.form/trunk/src/z3c/form/form.txt
  U   z3c.form/trunk/src/z3c/form/interfaces.py

-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt	2008-10-03 22:08:28 UTC (rev 91718)
+++ z3c.form/trunk/CHANGES.txt	2008-10-04 00:42:32 UTC (rev 91719)
@@ -5,8 +5,8 @@
 Version 2.0.0 (2008-??-??)
 --------------------------
 
-- Updated package to work with ``z3c.pt`` version 1.0b1 (which is
-  now based on the Chameleon template compiler). [malthe]
+- Feature: When no file is specified in the file upload widget, instead of
+  overwriting the value with a missing one, the old data is retained.
 
 - Feature: Added support in the file upload widget's testing flavor to specify
   'base64'-encoded strings in the hidden text area, so that binary data can be

Modified: z3c.form/trunk/src/z3c/form/converter.py
===================================================================
--- z3c.form/trunk/src/z3c/form/converter.py	2008-10-03 22:08:28 UTC (rev 91718)
+++ z3c.form/trunk/src/z3c/form/converter.py	2008-10-04 00:42:32 UTC (rev 91719)
@@ -219,7 +219,9 @@
     def toFieldValue(self, value):
         """See interfaces.IDataConverter"""
         if value is None or value == '':
-            return self.field.missing_value
+            # When no new file is uploaded, send a signal that we do not want
+            # to do anything special.
+            return interfaces.NOT_CHANGED
 
         if isinstance(value, zope.publisher.browser.FileUpload):
             # By default a IBytes field is used for get a file upload widget.

Modified: z3c.form/trunk/src/z3c/form/converter.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/converter.txt	2008-10-03 22:08:28 UTC (rev 91718)
+++ z3c.form/trunk/src/z3c/form/converter.txt	2008-10-04 00:42:32 UTC (rev 91719)
@@ -424,15 +424,18 @@
   >>> fudc.toFieldValue(myUpload)
   'File upload contents.'
 
-By default the converter converts missing input to ``missing_input`` value:
+By default the converter converts missing input to the ``NOT_CHANGED`` value:
 
-  >>> fudc.toFieldValue('') is None
-  True
+  >>> fudc.toFieldValue('')
+  <NOT_CHANGED>
 
-If we get a emtpy filename for a fileupload, we also get the missing_value,
-but this means there was a error somewhere in the upload, normaly yo are
-not able to uppload a file without a filename:
+This allows machinery later to ignore the field without sending all the data
+around.
 
+If we get an emtpy filename in a ``FileUpload`` obejct, we also get the
+``missing_value``. But this really means that there was an error somewhere in
+the upload, since you are normaly not able to upload a file without a filename:
+
   >>> class EmptyFilenameFieldStorageStub:
   ...     def __init__(self, file):
   ...         self.file = file
@@ -446,7 +449,7 @@
   >>> fudc.toFieldValue(myUpload) is None
   True
 
-There is also a ValueError if we don't get a seekable file from the
+There is also a ``ValueError`` if we don't get a seekable file from the
 ``FieldStorage`` during the upload:
 
   >>> myfile = ''

Modified: z3c.form/trunk/src/z3c/form/form.py
===================================================================
--- z3c.form/trunk/src/z3c/form/form.py	2008-10-03 22:08:28 UTC (rev 91718)
+++ z3c.form/trunk/src/z3c/form/form.py	2008-10-04 00:42:32 UTC (rev 91719)
@@ -36,10 +36,13 @@
         # If the field is not in the data, then go on to the next one
         if name not in data:
             continue
+        # If the value is NOT_CHANGED, ignore it, since the widget/converter
+        # sent a strong message not to do so.
+        if data[name] == interfaces.NOT_CHANGED:
+            continue
         # Get the datamanager and get the original value
         dm = zope.component.getMultiAdapter(
             (content, field.field), interfaces.IDataManager)
-        oldValue = dm.get()
         # Only update the data, if it is different
         if dm.get() != data[name]:
             dm.set(data[name])

Modified: z3c.form/trunk/src/z3c/form/form.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/form.txt	2008-10-03 22:08:28 UTC (rev 91718)
+++ z3c.form/trunk/src/z3c/form/form.txt	2008-10-04 00:42:32 UTC (rev 91719)
@@ -1397,11 +1397,54 @@
     >>> form.handleActionError(event)
 
 
+Applying Changes
+----------------
+
+When applying the data of a form to a content component, the function
+``applyChanges()`` is called. It simply iterates through the fields of the
+form and uses the data managers to store the values. The output of the
+function is a list of changes:
+
+  >>> roger = Person(u'roger', u'Roger')
+  >>> roger
+  <Person u'Roger'>
+
+  >>> class BaseForm(form.Form):
+  ...     fields = field.Fields(IPerson).select('name')
+  >>> myForm = BaseForm(roger, TestRequest())
+
+  >>> form.applyChanges(myForm, roger, {'name': u'Roger Ineichen'})
+  {<InterfaceClass __builtin__.IPerson>: ['name']}
+
+  >>> roger
+  <Person u'Roger Ineichen'>
+
+When a field is missing from the data, it is simply skipped:
+
+  >>> form.applyChanges(myForm, roger, {})
+  {}
+
+If the new and old value are identical, storing the data is skipped as well:
+
+  >>> form.applyChanges(myForm, roger, {'name': u'Roger Ineichen'})
+  {}
+
+In some cases the data converter for a field-widget pair returns the
+``NOT_CHANGED`` value. In this case, the field is skipped as well:
+
+  >>> form.applyChanges(myForm, roger, {'name': interfaces.NOT_CHANGED})
+  {}
+
+  >>> roger
+  <Person u'Roger Ineichen'>
+
+
 Integration tests
 -----------------
 
-Identify the different forms can be important if it comes to layout template
-lookup. Ensure that we support the right interfaces for the different forms.
+Identifying the different forms can be important if it comes to layout
+template lookup. Let's ensure that we support the right interfaces for the
+different forms.
 
 
 Form
@@ -1477,7 +1520,6 @@
   False
 
 
-
 AddForm
 ~~~~~~~
 

Modified: z3c.form/trunk/src/z3c/form/interfaces.py
===================================================================
--- z3c.form/trunk/src/z3c/form/interfaces.py	2008-10-03 22:08:28 UTC (rev 91718)
+++ z3c.form/trunk/src/z3c/form/interfaces.py	2008-10-04 00:42:32 UTC (rev 91719)
@@ -25,11 +25,15 @@
 
 from z3c.form.i18n import MessageFactory as _
 
-
 INPUT_MODE = 'input'
 DISPLAY_MODE = 'display'
 HIDDEN_MODE = 'hidden'
 
+class NOT_CHANGED(object):
+    def __repr__(self):
+        return '<NOT_CHANGED>'
+NOT_CHANGED = NOT_CHANGED()
+
 class NOVALUE(object):
     def __repr__(self):
         return '<NOVALUE>'



More information about the Checkins mailing list