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:
parent
e9cee1f052
commit
c04f84ed8f
3 changed files with 59 additions and 24 deletions
|
@ -115,6 +115,15 @@ an utility (using a stub/dummy implementation for testing purposes):
|
|||
>>> from zope.app.testing import ztapi
|
||||
>>> 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
|
||||
registry and query it.
|
||||
|
||||
|
|
|
@ -25,6 +25,12 @@
|
|||
<factory id="cybertools.relation.registry.RelationsRegistry" />
|
||||
</localUtility>
|
||||
|
||||
<adapter
|
||||
for=".interfaces.IRelation"
|
||||
provides=".registry.IIndexableRelation"
|
||||
factory=".registry.IndexableRelationAdapter"
|
||||
/>
|
||||
|
||||
<browser:tool
|
||||
interface=".interfaces.IRelationsRegistry"
|
||||
title="Relations Registry"
|
||||
|
|
|
@ -22,10 +22,10 @@ Implementation of the utilities needed for the relations package.
|
|||
$Id$
|
||||
"""
|
||||
|
||||
from zope.interface import implements
|
||||
from zope.interface import Interface, Attribute, implements
|
||||
from zope.app import zapi
|
||||
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 interfaces import IRelationsRegistry
|
||||
|
@ -72,43 +72,63 @@ class RelationsRegistry(Catalog):
|
|||
indexesSetUp = False
|
||||
|
||||
def setupIndexes(self):
|
||||
self['relationship'] = FieldIndex()
|
||||
self['first'] = FieldIndex()
|
||||
self['second'] = FieldIndex()
|
||||
self['third'] = FieldIndex()
|
||||
self['relationship'] = FieldIndex('relationship', IIndexableRelation)
|
||||
self['first'] = FieldIndex('first', IIndexableRelation)
|
||||
self['second'] = FieldIndex('second', IIndexableRelation)
|
||||
self['third'] = FieldIndex('third', IIndexableRelation)
|
||||
self.indexesSetUp = True
|
||||
|
||||
def register(self, relation):
|
||||
if not self.indexesSetUp:
|
||||
self.setupIndexes()
|
||||
relid = self._getUid(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))
|
||||
self.index_doc(_getUid(relation), relation)
|
||||
|
||||
def unregister(self, relation):
|
||||
self.unindex_doc(self._getUid(relation))
|
||||
self.unindex_doc(_getUid(relation))
|
||||
|
||||
def query(self, **kw):
|
||||
for k in kw:
|
||||
if k == 'relationship':
|
||||
quString = self._getClassString(kw[k])
|
||||
quString = _getClassString(kw[k])
|
||||
else:
|
||||
quString = self._getUid(kw[k])
|
||||
quString = _getUid(kw[k])
|
||||
# set min, max
|
||||
kw[k] = (quString, quString)
|
||||
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__
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue