[Checkins] SVN: grok/branches/faassen-index/src/grok/ Multiple index with the same name in the same catalog leads to an error.

Martijn Faassen faassen at infrae.com
Fri Apr 20 10:52:20 EDT 2007


Log message for revision 74263:
  Multiple index with the same name in the same catalog leads to an error.
  

Changed:
  A   grok/branches/faassen-index/src/grok/ftests/catalog/indexes_multiple_conflict.py
  U   grok/branches/faassen-index/src/grok/meta.py

-=-
Added: grok/branches/faassen-index/src/grok/ftests/catalog/indexes_multiple_conflict.py
===================================================================
--- grok/branches/faassen-index/src/grok/ftests/catalog/indexes_multiple_conflict.py	2007-04-20 14:34:33 UTC (rev 74262)
+++ grok/branches/faassen-index/src/grok/ftests/catalog/indexes_multiple_conflict.py	2007-04-20 14:52:20 UTC (rev 74263)
@@ -0,0 +1,69 @@
+"""
+Grok allows you to set up catalog indexes in your application with a
+special indexes declaration. In fact, we have multiple grok.Indexes
+setting up more than one set of indexes in the same catalog. What if these
+indexes define the same names?
+
+  >>> import grok
+  >>> grok.grok('grok.ftests.catalog.indexes_multiple_conflict')
+
+Let's set up a site in which we manage a couple of objects::
+
+  >>> from grok.ftests.catalog.indexes_multiple_conflict import Herd
+  >>> herd = Herd()
+  >>> getRootFolder()['herd'] = herd
+  Traceback (most recent call last):
+    ...
+  GrokError: grok.Indexes in module <module
+  'grok.ftests.catalog.indexes_multiple_conflict' from ...>
+  causes creation of catalog index 'name' in catalog u'', but an index
+  with that name is already present.
+
+  >>> from zope.app.component.hooks import setSite
+  >>> setSite(herd)
+  >>> from zope.app.catalog.interfaces import ICatalog
+  >>> from zope.component import getUtility, queryUtility
+  >>> catalog = getUtility(ICatalog)
+
+Nuke the catalog and intids in the end, so as not to confuse
+other tests::
+
+  >>> sm = herd.getSiteManager()
+  >>> from zope.app.catalog.interfaces import ICatalog
+  >>> sm.unregisterUtility(catalog, provided=ICatalog)
+  True
+  >>> from zope.app.intid.interfaces import IIntIds
+  >>> from zope import component
+  >>> intids = component.getUtility(IIntIds)
+  >>> sm.unregisterUtility(intids, provided=IIntIds)
+  True
+
+Unfortunately ftests don't have good isolation from each other yet.
+"""
+
+from zope.interface import Interface
+from zope import schema
+
+import grok
+from grok import index
+
+class Herd(grok.Container, grok.Application):
+    pass
+
+class IMammoth(Interface):
+    name = schema.TextLine(title=u'Name')
+
+class IMammoth2(Interface):
+    name = schema.TextLine(title=u'Name')
+
+class MammothIndexes(grok.Indexes):
+    grok.site(Herd)
+    grok.context(IMammoth)
+
+    name = index.Field()
+
+class MammothIndexes2(grok.Indexes):
+    grok.site(Herd)
+    grok.context(IMammoth2)
+
+    name = index.Field()

Modified: grok/branches/faassen-index/src/grok/meta.py
===================================================================
--- grok/branches/faassen-index/src/grok/meta.py	2007-04-20 14:34:33 UTC (rev 74262)
+++ grok/branches/faassen-index/src/grok/meta.py	2007-04-20 14:52:20 UTC (rev 74263)
@@ -20,6 +20,8 @@
 from zope.app.catalog.catalog import Catalog
 from zope.app.catalog.interfaces import ICatalog
 
+from zope.exceptions.interfaces import DuplicationError
+
 import grok
 from grok import util, components, formlib
 from grok.error import GrokError
@@ -508,15 +510,17 @@
         context = util.determine_class_context(factory, context)
         catalog_name = util.class_annotation(factory, 'grok.name', u'')
         zope.component.provideHandler(
-            IndexesSetupSubscriber(catalog_name, indexes, context),
+            IndexesSetupSubscriber(catalog_name, indexes,
+                                   context, module_info),
             adapts=(site,
                     grok.IObjectAddedEvent))
         
 class IndexesSetupSubscriber(object):
-    def __init__(self, catalog_name, indexes, context):
+    def __init__(self, catalog_name, indexes, context, module_info):
         self.catalog_name = catalog_name
         self.indexes = indexes
         self.context = context
+        self.module_info = module_info
         
     def __call__(self, site, event):
         # make sure we have an intids
@@ -525,7 +529,15 @@
         catalog = self._createCatalog(site)
         # now install indexes
         for name, index in self.indexes.items():
-            index.setup(catalog, name, self.context)
+            try:
+                index.setup(catalog, name, self.context)
+            except DuplicationError:
+                raise GrokError(
+                    "grok.Indexes in module %r causes "
+                    "creation of catalog index %r in catalog %r, "
+                    "but an index with that name is already present." %
+                    (self.module_info.getModule(), name, self.catalog_name),
+                    None)
 
     def _createCatalog(self, site):
         """Create the catalog if needed and return it.



More information about the Checkins mailing list