Added convenience function registry.getRelations()
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@846 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
536983c5da
commit
2657b5d243
3 changed files with 187 additions and 155 deletions
|
@ -280,6 +280,15 @@ It should work also for triadic relations:
|
|||
>>> clarkChildren[0].third == kirk
|
||||
True
|
||||
|
||||
There is also a convenience function that it makes even easier to query
|
||||
a relations registry; it allows to query for more than one relationship:
|
||||
|
||||
>>> from cybertools.relation.registry import getRelations
|
||||
>>> len(getRelations(first=clark))
|
||||
2
|
||||
>>> len(getRelations(first=clark, relationships=[LivesIn]))
|
||||
1
|
||||
|
||||
|
||||
Handling object removal
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
|
|
@ -60,4 +60,3 @@ class TriadicRelation(Relation):
|
|||
self.second = second
|
||||
self.third = third
|
||||
|
||||
|
|
@ -129,6 +129,30 @@ class IndexableRelationAdapter(object):
|
|||
return value
|
||||
|
||||
|
||||
# convenience function:
|
||||
|
||||
def getRelations(first=None, second=None, third=None, relationships=None):
|
||||
""" Return a sequence of relations matching the query specified by the
|
||||
parameters.
|
||||
|
||||
The relationships parameter expects a sequence of relationships
|
||||
(relation classes or predicate objects).
|
||||
"""
|
||||
registry = zapi.getUtility(IRelationsRegistry)
|
||||
query = {}
|
||||
if first: query['first'] = first
|
||||
if second: query['second'] = second
|
||||
if third: query['third'] = third
|
||||
if not relationships:
|
||||
return registry.query(**query)
|
||||
else:
|
||||
result = set()
|
||||
for r in relationships:
|
||||
query['relationship'] = r
|
||||
result.update(registry.query(**query))
|
||||
return result
|
||||
|
||||
|
||||
# events and handlers
|
||||
|
||||
class RelationInvalidatedEvent(ObjectEvent):
|
||||
|
|
Loading…
Add table
Reference in a new issue