Default attribute accessor now returns a field default for a field provided by a subtype.

This commit is contained in:
Malthe Borch 2012-10-18 10:29:18 +02:00
parent 26d27cb66f
commit dd491480b8
3 changed files with 16 additions and 3 deletions

View File

@ -4,6 +4,10 @@ Changelog
2.0.1 (unreleased)
------------------
- The default attribute accessor now also looks through subtypes
(behaviors) to find a field default.
[malthe]
- Added support in the FTI to look up behaviors by utility name when
getting additional schemata (i.e. fields provided by behaviors).

View File

@ -185,7 +185,13 @@ class DexterityContent(DAVResourceMixin, PortalContent, DefaultDublinCoreImpl, C
field = schema.get(name, None)
if field is not None:
return deepcopy(field.default)
# do the same for each subtype
for schema in SCHEMA_CACHE.subtypes(self.portal_type):
field = schema.get(name, None)
if field is not None:
return deepcopy(field.default)
raise AttributeError(name)
# Let __name__ and id be identical. Note that id must be ASCII in Zope 2,

View File

@ -231,7 +231,7 @@ class TestContent(MockTestCase):
pass
class ISubtype(Interface):
pass
baz = zope.schema.TextLine(title=u"baz", default=u"baz")
behavior1 = BehaviorRegistration(u"Behavior1", "", IBehavior1, None, None)
behavior2 = BehaviorRegistration(u"Behavior2", "", IBehavior2, ISubtype, None)
@ -258,7 +258,10 @@ class TestContent(MockTestCase):
# the cache. This is not the case, as evidenced by .count(1) above.
self.assertEquals(True, ISubtype.providedBy(item))
self.assertEquals(True, ISchema.providedBy(item))
# Subtypes provide field defaults.
self.assertEquals(u"baz", getattr(item, "baz", None))
# We also need to ensure that the _v_ attribute doesn't hide any
# interface set directly on the instance with alsoProvides() or
# directlyProvides(). This is done by clearing the cache when these