RelationsRegistry is now using the correct FieldIndex adapting relation objects to IIndexableRelation

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@655 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2005-11-03 10:48:53 +00:00
parent e9cee1f052
commit c04f84ed8f
3 changed files with 59 additions and 24 deletions

View file

@ -115,6 +115,15 @@ an utility (using a stub/dummy implementation for testing purposes):
>>> from zope.app.testing import ztapi >>> from zope.app.testing import ztapi
>>> ztapi.provideUtility(IIntIds, IntIdsStub()) >>> ztapi.provideUtility(IIntIds, IntIdsStub())
We also have to provide an adapter for the Relation objects that provides
the attributes needed for indexing:
>>> from cybertools.relation.registry import IIndexableRelation
>>> from cybertools.relation.registry import IndexableRelationAdapter
>>> from cybertools.relation.interfaces import IRelation
>>> ztapi.provideAdapter(IRelation, IIndexableRelation,
... IndexableRelationAdapter)
So we are ready again to register a set of relations with our new relations So we are ready again to register a set of relations with our new relations
registry and query it. registry and query it.

View file

@ -25,6 +25,12 @@
<factory id="cybertools.relation.registry.RelationsRegistry" /> <factory id="cybertools.relation.registry.RelationsRegistry" />
</localUtility> </localUtility>
<adapter
for=".interfaces.IRelation"
provides=".registry.IIndexableRelation"
factory=".registry.IndexableRelationAdapter"
/>
<browser:tool <browser:tool
interface=".interfaces.IRelationsRegistry" interface=".interfaces.IRelationsRegistry"
title="Relations Registry" title="Relations Registry"

View file

@ -22,10 +22,10 @@ Implementation of the utilities needed for the relations package.
$Id$ $Id$
""" """
from zope.interface import implements from zope.interface import Interface, Attribute, implements
from zope.app import zapi from zope.app import zapi
from zope.app.catalog.catalog import Catalog from zope.app.catalog.catalog import Catalog
from zope.index.field import FieldIndex from zope.app.catalog.field import FieldIndex
from zope.app.intid.interfaces import IIntIds from zope.app.intid.interfaces import IIntIds
from interfaces import IRelationsRegistry from interfaces import IRelationsRegistry
@ -72,43 +72,63 @@ class RelationsRegistry(Catalog):
indexesSetUp = False indexesSetUp = False
def setupIndexes(self): def setupIndexes(self):
self['relationship'] = FieldIndex() self['relationship'] = FieldIndex('relationship', IIndexableRelation)
self['first'] = FieldIndex() self['first'] = FieldIndex('first', IIndexableRelation)
self['second'] = FieldIndex() self['second'] = FieldIndex('second', IIndexableRelation)
self['third'] = FieldIndex() self['third'] = FieldIndex('third', IIndexableRelation)
self.indexesSetUp = True self.indexesSetUp = True
def register(self, relation): def register(self, relation):
if not self.indexesSetUp: if not self.indexesSetUp:
self.setupIndexes() self.setupIndexes()
relid = self._getUid(relation) self.index_doc(_getUid(relation), relation)
for idx in self:
index = self[idx]
if idx == 'relationship':
index.index_doc(relid, self._getRelationship(relation))
else:
target = getattr(relation, idx, None)
index.index_doc(relid, target and self._getUid(target))
def unregister(self, relation): def unregister(self, relation):
self.unindex_doc(self._getUid(relation)) self.unindex_doc(_getUid(relation))
def query(self, **kw): def query(self, **kw):
for k in kw: for k in kw:
if k == 'relationship': if k == 'relationship':
quString = self._getClassString(kw[k]) quString = _getClassString(kw[k])
else: else:
quString = self._getUid(kw[k]) quString = _getUid(kw[k])
# set min, max # set min, max
kw[k] = (quString, quString) kw[k] = (quString, quString)
return self.searchResults(**kw) return self.searchResults(**kw)
def _getUid(self, ob):
return zapi.getUtility(IIntIds).getId(ob)
def _getRelationship(self, relation):
return self._getClassString(relation.__class__)
def _getClassString(self, cls):
return cls.__module__ + '.' + cls.__name__
class IIndexableRelation(Interface):
""" Provides the attributes needed for indexing relation objects in
a catalog-based registry.
"""
class IndexableRelationAdapter(object):
""" Adapter for providing the attributes needed for indexing
relation objects.
"""
implements(IIndexableRelation)
def __init__(self, context):
self.context = context
def getRelationship(self):
return _getRelationship(self.context)
relationship = property(getRelationship)
def __getattr__(self, attr):
value = getattr(self.context, attr)
return _getUid(value)
def _getUid(ob):
return zapi.getUtility(IIntIds).getId(ob)
def _getRelationship(relation):
return _getClassString(relation.__class__)
def _getClassString(cls):
return cls.__module__ + '.' + cls.__name__