diff --git a/relation/README.txt b/relation/README.txt index 7105cfc..e3bd983 100644 --- a/relation/README.txt +++ b/relation/README.txt @@ -159,6 +159,14 @@ It is also possible to remove a relation from the relation registry: >>> nyRels[0].first == kirk True +The relation registry also provides unique ids for all relations +registered and all objects taking part in these relations. This unique +ids may be used e.g. if relationships or predicates are themselves +content objects. + + >>> relations.getUniqueIdForObject(kirk) + 7 + Triadic Relations ~~~~~~~~~~~~~~~~~ @@ -305,6 +313,12 @@ a relation registry; it allows to query for more than one relationship: >>> len(getRelations(first=clark, relationships=[LivesIn])) 1 +The relation registry also supports the getUniqueIdForObject() method that +just returns the intid of the object given: + + >>> relations.getUniqueIdForObject(kirk) + 1 + Handling object removal ~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/relation/interfaces.py b/relation/interfaces.py index 0abcbd4..90036e5 100644 --- a/relation/interfaces.py +++ b/relation/interfaces.py @@ -131,6 +131,11 @@ class IRelationRegistryUpdate(Interface): """ Remove the relation given from this registry. """ + def getUniqueIdForObject(object): + """ Return an identifier for the object given that is unique at + least within the scope of this registry. + """ + class IRelationRegistryQuery(Interface): """ Interface for querying a relation registry. @@ -155,4 +160,3 @@ class IRelationRegistry(IRelationRegistryUpdate, IRelationRegistryQuery): implemented as a local utility . """ - diff --git a/relation/registry.py b/relation/registry.py index 2ff04e6..c5f8674 100644 --- a/relation/registry.py +++ b/relation/registry.py @@ -45,15 +45,27 @@ class DummyRelationRegistry(object): def __init__(self): self.relations = [] + self.objects = [] def register(self, relation): if relation not in self.relations: self.relations.append(relation) + if relation not in self.objects: + self.objects.append(relation) + for attr in ('first', 'second', 'third',): + value = getattr(relation, attr, None) + if value is not None and value not in self.objects: + self.objects.append(value) def unregister(self, relation): if relation in self.relations: self.relations.remove(relation) notify(RelationInvalidatedEvent(relation)) + + def getUniqueIdForObject(self, obj): + if obj in self.objects: + return self.objects.index(obj) + return None def query(self, example=None, **kw): result = [] @@ -66,6 +78,8 @@ class DummyRelationRegistry(object): criteria['relationship'] = example criteria.update(kw) for r in self.relations: + if r is None: + continue hit = True for k in criteria: if ((k == 'relationship' @@ -100,6 +114,9 @@ class RelationRegistry(Catalog): self.unindex_doc(zapi.getUtility(IIntIds).getId(relation)) notify(RelationInvalidatedEvent(relation)) + def getUniqueIdForObject(self, obj): + return zapi.getUtility(IIntIds).getId(obj) + def query(self, example=None, **kw): intIds = zapi.getUtility(IIntIds) criteria = {}