[Checkins] SVN: grok/trunk/src/grok/ Apply patch constructed by myself and Jan-Wijbrand to allow an optional

Martijn Faassen faassen at infrae.com
Fri Apr 27 07:57:27 EDT 2007


Log message for revision 74854:
  Apply patch constructed by myself and Jan-Wijbrand to allow an optional
  'attribute' argument for catalog index definitions.
  

Changed:
  A   grok/trunk/src/grok/ftests/catalog/indexes_attribute.py
  U   grok/trunk/src/grok/index.py

-=-
Added: grok/trunk/src/grok/ftests/catalog/indexes_attribute.py
===================================================================
--- grok/trunk/src/grok/ftests/catalog/indexes_attribute.py	2007-04-27 11:48:36 UTC (rev 74853)
+++ grok/trunk/src/grok/ftests/catalog/indexes_attribute.py	2007-04-27 11:57:27 UTC (rev 74854)
@@ -0,0 +1,71 @@
+"""
+Grok allows you to set up catalog indexes in your application with a
+special indexes declaration. If you want to name the index differently
+from the attribute, you can do so, by passing an explicit `attribute`
+keyword argument to the field.
+
+  >>> import grok
+  >>> grok.grok('grok.ftests.catalog.indexes_attribute')
+
+Let's set up a site in which we manage a couple of objects::
+
+  >>> from grok.ftests.catalog.indexes_attribute 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)
+  >>> herd['beta'] = Mammoth('Beta', 14)
+
+We are able to query the catalog::
+
+  >>> from zope.app.catalog.interfaces import ICatalog
+  >>> from zope.component import getUtility, queryUtility
+  >>> catalog = getUtility(ICatalog)
+  >>> for obj in catalog.searchResults(how_old=(13, 13)):
+  ...   print obj.name
+  Alpha
+
+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
+"""
+
+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')
+    age = schema.Int(title=u'Age')
+
+class MammothIndexes(grok.Indexes):
+    grok.site(Herd)
+    grok.context(IMammoth)
+
+    name = index.Field()
+    how_old = index.Field(attribute='age')
+
+class Mammoth(grok.Model):
+    grok.implements(IMammoth)
+
+    def __init__(self, name, age):
+        self.name = name
+        self.age = age

Modified: grok/trunk/src/grok/index.py
===================================================================
--- grok/trunk/src/grok/index.py	2007-04-27 11:48:36 UTC (rev 74853)
+++ grok/trunk/src/grok/index.py	2007-04-27 11:57:27 UTC (rev 74854)
@@ -14,7 +14,7 @@
     implements(IIndexDefinition)
 
     index_class = None
-    
+
     def __init__(self, *args, **kw):
         frame = sys._getframe(1)
         if not frame_is_class(frame):
@@ -22,22 +22,28 @@
                 "%r can only be instantiated on class level." % self.__class__)
         # store any extra parameters to pass to index later
         self._args = args
+        self._attribute = kw.pop('attribute', None)
         self._kw = kw
 
     def setup(self, catalog, name, context, module_info):
+        if self._attribute is not None:
+            field_name = self._attribute
+        else:
+            field_name = name
+
         if IInterface.providedBy(context):
             try:
-                method = context[name]
+                method = context[field_name]
             except KeyError:
                 raise GrokError("grok.Indexes in %r refers to an attribute or "
                                 "method %r on interface %r, but this does not "
                                 "exist." % (module_info.getModule(),
-                                            name, context), None)
+                                            field_name, context), None)
             call = IMethod.providedBy(method)
         else:
-            call = callable(getattr(context, name, None))
+            call = callable(getattr(context, field_name, None))
             context = None # no interface lookup
-        catalog[name] = self.index_class(field_name=name,
+        catalog[name] = self.index_class(field_name=field_name,
                                          interface=context,
                                          field_callable=call,
                                          *self._args, **self._kw)



More information about the Checkins mailing list