Skip to content

Commit

Permalink
- Feature: The DictionaryFieldManager now allows all mappings. By d…
Browse files Browse the repository at this point in the history
…efault,

  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.
  • Loading branch information
strichter committed Jun 18, 2009
1 parent afbc31b commit b581754
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 3 additions & 1 deletion CHANGES.txt
Expand Up @@ -5,7 +5,9 @@ CHANGES
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)
--------------------------
Expand Down
6 changes: 4 additions & 2 deletions src/z3c/form/datamanager.py
Expand Up @@ -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

Expand Down Expand Up @@ -94,14 +95,15 @@ class DictionaryField(DataManager):
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"""
Expand Down
11 changes: 7 additions & 4 deletions src/z3c/form/datamanager.txt
Expand Up @@ -259,7 +259,13 @@ modify.
>>> 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):
Expand All @@ -269,9 +275,6 @@ The datamanager can really only deal with dictionaries and no other types:
Let's now access the name:

>>> nameDm.get()
Traceback (most recent call last):
...
KeyError: 'name'

>>> nameDm.query()
<NO_VALUE>
Expand Down

0 comments on commit b581754

Please sign in to comment.