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:
parent
b79cfea129
commit
dc5c09afdd
2 changed files with 12 additions and 6 deletions
|
@ -1,4 +1,3 @@
|
||||||
"""
|
"""
|
||||||
$Id$
|
$Id$
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
|
@ -162,29 +162,36 @@ def getRelations(first=None, second=None, third=None, relationships=None):
|
||||||
result.update(registry.query(**query))
|
result.update(registry.query(**query))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def getRelationSingle(first, relationship):
|
def getRelationSingle(obj=None, relationship=None, forSecond=True):
|
||||||
""" Returns the one and only relation for first having relationship
|
""" Returns the one and only relation for first having relationship
|
||||||
or None if there is none.
|
or None if there is none.
|
||||||
|
|
||||||
Raise an error if there is more than one hit.
|
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:
|
if len(rels) == 0:
|
||||||
return None
|
return None
|
||||||
if len(rels) > 1:
|
if len(rels) > 1:
|
||||||
raise ValueError('Multiple hits when only one relation expected: '
|
raise ValueError('Multiple hits when only one relation expected: '
|
||||||
'%s, relationship: %s' % (zapi.getName(first),
|
'%s, relationship: %s' % (zapi.getName(obj),
|
||||||
relationship.getPredicateName()))
|
relationship.getPredicateName()))
|
||||||
return list(rels)[0]
|
return list(rels)[0]
|
||||||
|
|
||||||
def setRelationSingle(relation):
|
def setRelationSingle(relation, forSecond=True):
|
||||||
""" Register the relation given, unregistering already existing
|
""" Register the relation given, unregistering already existing
|
||||||
relations for first and relationship. After this operation there
|
relations for first and relationship. After this operation there
|
||||||
will be only one relation for first with the relationship given.
|
will be only one relation for first with the relationship given.
|
||||||
"""
|
"""
|
||||||
first = relation.first
|
first = relation.first
|
||||||
|
second = relation.second
|
||||||
registry = zapi.getUtility(IRelationRegistry)
|
registry = zapi.getUtility(IRelationRegistry)
|
||||||
rels = list(registry.query(first=first, relationship=relation))
|
if forSecond:
|
||||||
|
rels = list(registry.query(second=second, relationship=relation))
|
||||||
|
else:
|
||||||
|
rels = list(registry.query(first=first, relationship=relation))
|
||||||
for oldRel in rels:
|
for oldRel in rels:
|
||||||
registry.unregister(oldRel)
|
registry.unregister(oldRel)
|
||||||
registry.register(relation)
|
registry.register(relation)
|
||||||
|
|
Loading…
Add table
Reference in a new issue