From 12f24e4ea26df67e8dc124c8fff8964a2443f605 Mon Sep 17 00:00:00 2001 From: Elizabeth Leddy Date: Mon, 18 Mar 2013 16:13:10 -0700 Subject: [PATCH] fail gracefully when a schema lookup fails do to import error in lookupSchema --- docs/HISTORY.txt | 14 ++++++++++++-- plone/dexterity/fti.py | 13 ++++++++++--- plone/dexterity/tests/test_fti.py | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt index 4fc7052..dd11820 100644 --- a/docs/HISTORY.txt +++ b/docs/HISTORY.txt @@ -1,14 +1,24 @@ Changelog ========= -2.1.2 (unreleased) +2.1.3 (unreleased) +------------------ +- Fail gracefully when a schema lookup fails due to schema that doesn't + exist or no longer exists for some reason or another. + [eleddy] + +- Add convenience functions (aka sanity) for getting + dexterity fields and behavior fields in a consistent way + [eleddy] + + +2.1.2 (2013-03-05) ------------------ - Merged Rafael Oliveira's (@rafaelbco) @content-core views from collective.cmfeditionsdexteritycompat. [rpatterson] - 2.1.1 (2013-01-17) ------------------ diff --git a/plone/dexterity/fti.py b/plone/dexterity/fti.py index 31285a5..070f622 100644 --- a/plone/dexterity/fti.py +++ b/plone/dexterity/fti.py @@ -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 diff --git a/plone/dexterity/tests/test_fti.py b/plone/dexterity/tests/test_fti.py index 556abb2..f12092a 100644 --- a/plone/dexterity/tests/test_fti.py +++ b/plone/dexterity/tests/test_fti.py @@ -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