Slight extension of get/setRelationSingle() to allow for using for attribute on 'second'

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1083 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2006-02-18 10:56:14 +00:00
parent b79cfea129
commit dc5c09afdd
2 changed files with 12 additions and 6 deletions

View file

@ -1,4 +1,3 @@
"""
$Id$
"""

View file

@ -162,28 +162,35 @@ def getRelations(first=None, second=None, third=None, relationships=None):
result.update(registry.query(**query))
return result
def getRelationSingle(first, relationship):
def getRelationSingle(obj=None, relationship=None, forSecond=True):
""" Returns the one and only relation for first having relationship
or None if there is none.
Raise an error if there is more than one hit.
"""
rels = getRelations(first, relationships=[relationship])
if forSecond:
rels = getRelations(second=obj, relationships=[relationship])
else:
rels = getRelations(first=obj, relationships=[relationship])
if len(rels) == 0:
return None
if len(rels) > 1:
raise ValueError('Multiple hits when only one relation expected: '
'%s, relationship: %s' % (zapi.getName(first),
'%s, relationship: %s' % (zapi.getName(obj),
relationship.getPredicateName()))
return list(rels)[0]
def setRelationSingle(relation):
def setRelationSingle(relation, forSecond=True):
""" Register the relation given, unregistering already existing
relations for first and relationship. After this operation there
will be only one relation for first with the relationship given.
"""
first = relation.first
second = relation.second
registry = zapi.getUtility(IRelationRegistry)
if forSecond:
rels = list(registry.query(second=second, relationship=relation))
else:
rels = list(registry.query(first=first, relationship=relation))
for oldRel in rels:
registry.unregister(oldRel)