[Checkins] SVN: grok/branches/faassen-index/src/grok/ Make it
possible to index classes directly too, without interface.
Martijn Faassen
faassen at infrae.com
Tue Apr 17 16:28:17 EDT 2007
Log message for revision 74216:
Make it possible to index classes directly too, without interface.
Changed:
A grok/branches/faassen-index/src/grok/ftests/catalog/indexes_class.py
U grok/branches/faassen-index/src/grok/index.py
-=-
Added: grok/branches/faassen-index/src/grok/ftests/catalog/indexes_class.py
===================================================================
--- grok/branches/faassen-index/src/grok/ftests/catalog/indexes_class.py 2007-04-17 19:04:11 UTC (rev 74215)
+++ grok/branches/faassen-index/src/grok/ftests/catalog/indexes_class.py 2007-04-17 20:28:17 UTC (rev 74216)
@@ -0,0 +1,79 @@
+"""
+Grok allows you to set up catalog indexes in your application with a
+special indexes declaration. This can also be done without explicit interface.
+The context of the indexes applies to a class in this case.
+
+ >>> import grok
+ >>> grok.grok('grok.ftests.catalog.indexes_class')
+
+Let's set up a site in which we manage a couple of objects::
+
+ >>> from grok.ftests.catalog.indexes import Herd, Mammoth
+ >>> herd = Herd()
+ >>> getRootFolder()['herd'] = herd
+ >>> from zope.app.component.hooks import setSite
+ >>> setSite(herd)
+
+Now we add some indexable objects to the site::
+
+ >>> herd['alpha'] = Mammoth('Alpha', 13, 'Hello world!')
+ >>> herd['beta'] = Mammoth('Beta', 14, 'Bye World!')
+
+We are able to query the catalog::
+
+ >>> from zope.app.catalog.interfaces import ICatalog
+ >>> from zope.component import getUtility
+ >>> catalog = getUtility(ICatalog)
+ >>> for obj in catalog.searchResults(name=('Beta', 'Beta')):
+ ... print obj.name
+ Beta
+
+Let's query the text index, which incidentally also indexes a method::
+
+ >>> def sortedResults(catalog, **kw):
+ ... result = list(catalog.searchResults(**kw))
+ ... result.sort(key=lambda x:x.name)
+ ... return [item.name for item in result]
+ >>> sortedResults(catalog, message='world')
+ ['Alpha', 'Beta']
+ >>> sortedResults(catalog, message='hello')
+ ['Alpha']
+ >>> sortedResults(catalog, message='bye')
+ ['Beta']
+
+Nuke the catalog and initds 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.
+"""
+import grok
+from grok import index
+
+class Mammoth(grok.Model):
+ def __init__(self, name, age, message):
+ self.name = name
+ self.age = age
+ self._message = message
+
+ def message(self):
+ return self._message
+
+class MammothIndexes(grok.Indexes):
+ grok.context(Mammoth)
+
+ name = index.Field()
+ age = index.Field()
+ message = index.Text()
+
+class Herd(grok.Container, grok.Application):
+ pass
Modified: grok/branches/faassen-index/src/grok/index.py
===================================================================
--- grok/branches/faassen-index/src/grok/index.py 2007-04-17 19:04:11 UTC (rev 74215)
+++ grok/branches/faassen-index/src/grok/index.py 2007-04-17 20:28:17 UTC (rev 74216)
@@ -1,7 +1,7 @@
import sys
from zope.interface import implements
-from zope.interface.interfaces import IMethod
+from zope.interface.interfaces import IMethod, IInterface
from zope.app.catalog.field import FieldIndex
from zope.app.catalog.text import TextIndex
@@ -24,8 +24,14 @@
self._kw = kw
def setup(self, catalog, name, context):
- call = IMethod.providedBy(context[name])
- catalog[name] = self.index_class(name, context, call,
+ if IInterface.providedBy(context):
+ call = IMethod.providedBy(context[name])
+ else:
+ call = callable(getattr(context, name, None))
+ context = None # no interface lookup
+ catalog[name] = self.index_class(field_name=name,
+ interface=context,
+ field_callable=call,
*self._args, **self._kw)
class Field(IndexDefinition):
More information about the Checkins
mailing list