Merge pull request #15 from eleddy/missing_files_patch

Gracefully handle missing schema lookups
This commit is contained in:
Elizabeth Leddy 2013-04-11 16:15:22 -07:00
commit 65e13486d9
3 changed files with 27 additions and 5 deletions

View File

@ -4,7 +4,9 @@ Changelog
2.1.3 (unreleased)
------------------
- Nothing changed yet.
- Fail gracefully when a schema lookup fails due to schema that doesn't
exist or no longer exists for some reason or another.
[eleddy]
2.1.2 (2013-03-05)
@ -14,7 +16,6 @@ Changelog
collective.cmfeditionsdexteritycompat.
[rpatterson]
2.1.1 (2013-01-17)
------------------

View File

@ -1,4 +1,5 @@
import os.path
import logging
from zope.interface import implements
@ -204,12 +205,18 @@ class DexterityFTI(base.DynamicViewTypeInformation):
return not(self.schema)
def lookupSchema(self):
schema = None
# If a specific schema is given, use it
if self.schema:
schema = utils.resolveDottedName(self.schema)
if schema is None:
raise ValueError(u"Schema %s set for type %s cannot be resolved" % (self.schema, self.getId()))
try:
schema = utils.resolveDottedName(self.schema)
except ImportError:
logging.warning(u"Schema %s set for type %s cannot be resolved" % (self.schema, self.getId()))
# fall through to return a fake class with no
# fields so that end user code doesn't break
if schema:
return schema
# Otherwise, look up a dynamic schema. This will query the model for

View File

@ -87,6 +87,20 @@ class TestFTI(MockTestCase):
# cleanup
delattr(plone.dexterity.schema.generated, schemaName)
def test_lookupSchema_with_nonexistant_schema(self):
""" Tests the case where a dexterity type is not removed cleanly
from the fti, but the code has been removed.
"""
fti = DexterityFTI(u"testtype")
fti.schema = 'model.wont.be.imported'
portal = self.create_dummy(getPhysicalPath=lambda:('', 'site'))
self.mock_utility(portal, ISiteRoot)
schemaName = utils.portalTypeToSchemaName(fti.getId())
setattr(plone.dexterity.schema.generated, schemaName, ITestSchema)
self.assertEquals(ITestSchema, fti.lookupSchema())
delattr(plone.dexterity.schema.generated, schemaName)
def test_lookupModel_from_string(self):
fti = DexterityFTI(u"testtype")
fti.schema = None