[Checkins] SVN: z3c.form/trunk/ - Feature: The `DictionaryFieldManager` now allows all mappings. By default,

Stephan Richter srichter at gmail.com
Thu Jun 18 02:33:09 EDT 2009


Log message for revision 101115:
  - Feature: The `DictionaryFieldManager` now allows all mappings. By default,
    however, the field manager is only registered for dict, because it would
    otherwise get picked up in undesired scenarios.
  
  I am using this right now to edit session-stored data. Works 
  beautifull on SessionPkgData.
  
  

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

-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt	2009-06-17 19:07:19 UTC (rev 101114)
+++ z3c.form/trunk/CHANGES.txt	2009-06-18 06:33:08 UTC (rev 101115)
@@ -5,7 +5,9 @@
 Version 2.1.0 (unreleased)
 --------------------------
 
-- ...
+- Feature: The `DictionaryFieldManager` now allows all mappings. By default,
+  however, the field manager is only registered for dict, because it would
+  otherwise get picked up in undesired scenarios.
 
 Version 2.0.0 (2009-06-14)
 --------------------------

Modified: z3c.form/trunk/src/z3c/form/datamanager.py
===================================================================
--- z3c.form/trunk/src/z3c/form/datamanager.py	2009-06-17 19:07:19 UTC (rev 101114)
+++ z3c.form/trunk/src/z3c/form/datamanager.py	2009-06-18 06:33:08 UTC (rev 101115)
@@ -20,6 +20,7 @@
 import zope.interface
 import zope.component
 import zope.schema
+from zope.interface.common import mapping
 from zope.security.interfaces import ForbiddenAttribute
 from zope.security.checker import canAccess, canWrite, Proxy
 
@@ -94,14 +95,15 @@
         dict, zope.schema.interfaces.IField)
 
     def __init__(self, data, field):
-        if not isinstance(data, dict):
+        if (not isinstance(data, dict) and
+            not mapping.IMapping.providedBy(data)):
             raise ValueError("Data are not a dictionary: %s" %type(data))
         self.data = data
         self.field = field
 
     def get(self):
         """See z3c.form.interfaces.IDataManager"""
-        return self.data[self.field.__name__]
+        return self.data.get(self.field.__name__, self.field.missing_value)
 
     def query(self, default=interfaces.NO_VALUE):
         """See z3c.form.interfaces.IDataManager"""

Modified: z3c.form/trunk/src/z3c/form/datamanager.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/datamanager.txt	2009-06-17 19:07:19 UTC (rev 101114)
+++ z3c.form/trunk/src/z3c/form/datamanager.txt	2009-06-18 06:33:08 UTC (rev 101115)
@@ -259,8 +259,14 @@
   >>> personDict = {}
   >>> nameDm = datamanager.DictionaryField(personDict, IPerson['name'])
 
-The datamanager can really only deal with dictionaries and no other types:
+The datamanager can really only deal with dictionaries and mapping types:
 
+  >>> import zope.interface.common.mapping
+  >>> class MyMapping(object):
+  ...     zope.interface.implements(zope.interface.common.mapping.IMapping)
+  >>> datamanager.DictionaryField(MyMapping(), IPerson['name'])
+  <z3c.form.datamanager.DictionaryField object at ...>
+
   >>> datamanager.DictionaryField([], IPerson['name'])
   Traceback (most recent call last):
   ...
@@ -269,9 +275,6 @@
 Let's now access the name:
 
   >>> nameDm.get()
-  Traceback (most recent call last):
-  ...
-  KeyError: 'name'
 
   >>> nameDm.query()
   <NO_VALUE>



More information about the Checkins mailing list