From dc5c09afdddd7706abf35cfb0a6148c995cc06cc Mon Sep 17 00:00:00 2001 From: helmutm Date: Sat, 18 Feb 2006 10:56:14 +0000 Subject: [PATCH] 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 --- __init__.py | 1 - relation/registry.py | 17 ++++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/__init__.py b/__init__.py index 4bc90fb..38314f3 100644 --- a/__init__.py +++ b/__init__.py @@ -1,4 +1,3 @@ """ $Id$ """ - diff --git a/relation/registry.py b/relation/registry.py index 014d03b..d4228f6 100644 --- a/relation/registry.py +++ b/relation/registry.py @@ -162,29 +162,36 @@ 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) - 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: registry.unregister(oldRel) registry.register(relation)