def relate(self, other, meaning=ACTIVE, code=ACTIVE_CODE): links = IRelationshipLinks(self.this) try: link = links.find(self.my_role, other, self.other_role, self.rel_type) except ValueError: relate(self.rel_type, (self.this, self.my_role), (other, self.other_role)) link = links.find(self.my_role, other, self.other_role, self.rel_type) link.state.set(self.filter_date, meaning=meaning, code=code) notify(LinkStateModifiedEvent( link, self.this, other, self.filter_date, meaning, code))
def relate(self, other, meaning=ACTIVE, code=ACTIVE_CODE): links = IRelationshipLinks(self.this) try: link = links.find(self.my_role, other, self.other_role, self.rel_type) except ValueError: relate(self.rel_type, (self.this, self.my_role), (other, self.other_role)) link = links.find(self.my_role, other, self.other_role, self.rel_type) link.state.set(self.filter_date, meaning=meaning, code=code) notify( LinkStateModifiedEvent(link, self.this, other, self.filter_date, meaning, code))
def getLinks(self): links_1 = IRelationshipLinks(self.participant1) links_2 = IRelationshipLinks(self.participant2) try: link_1_to_2 = links_1.find(self.role1, self.participant2, self.role2, self.rel_type) except ValueError: raise NoSuchRelationship try: link_2_to_1 = links_2.find(self.role2, self.participant1, self.role1, self.rel_type) except ValueError: raise NoSuchRelationship return link_1_to_2, link_2_to_1
def getLinks(self): links_1 = IRelationshipLinks(self.participant1) links_2 = IRelationshipLinks(self.participant2) try: link_1_to_2 = links_1.find( self.role1, self.participant2, self.role2, self.rel_type) except ValueError: raise NoSuchRelationship try: link_2_to_1 = links_2.find( self.role2, self.participant1, self.role1, self.rel_type) except ValueError: raise NoSuchRelationship return link_1_to_2, link_2_to_1
def state(self, other): links = IRelationshipLinks(self.this) try: link = links.find(self.my_role, other, self.other_role, self.rel_type) except ValueError: return None return link.state
def unrelateOnCopy(event): """Remove all relationships when an object is copied.""" if not IObjectCopiedEvent.providedBy(event): return # event.object may be a ContainedProxy obj = getProxiedObject(event.object) linkset = IRelationshipLinks(obj, None) if linkset is not None: links_to_remove = [] for link in linkset: other_linkset = IRelationshipLinks(link.target) try: other_linkset.find(link.role, obj, link.my_role, link.rel_type) except ValueError: # The corresponding other link was not copied, so we have a # degenerate one-sided relationship. Let's remove it # altogether. It would not difficult to have a different # function, cloneRelationshipsOnCopy, that would create # a corresponding link in other_linkset. links_to_remove.append(link) for link in links_to_remove: linkset.remove(link)
def unrelate(self, other): """Delete state on filtered date or unrelate completely if no states left or filtered date is .all() """ links = IRelationshipLinks(self.this) link = links.find(self.my_role, other, self.other_role, self.rel_type) if self.filter_date is None: unrelate(self.rel_type, (self.this, self.my_role), (other, self.other_role)) return state = link.state date = state.closest(self.filter_date) if date is None: raise KeyError(self.filter_date) del state[date] try: iter(state).next() except StopIteration: unrelate(self.rel_type, (self.this, self.my_role), (other, self.other_role))
link_a = Link(link.my_role, link.target, link.role, link.rel_type, shared) IRelationshipLinks(obj).add(link_a) link_b = Link(link.role, obj, link.my_role, link.rel_type, shared) IRelationshipLinks(link.target).add(link_b) zope.event.notify(RelationshipAddedEvent(link.rel_type, (obj, link.my_role), (link.target, link.role), shared)) def unrelate(rel_type, (a, role_of_a), (b, role_of_b)): """Break a relationship between objects `a` and `b`.""" links_of_a = IRelationshipLinks(a) links_of_b = IRelationshipLinks(b) try: link_a_to_b = links_of_a.find(role_of_a, b, role_of_b, rel_type) except ValueError: raise NoSuchRelationship extra_info = link_a_to_b.extra_info zope.event.notify(BeforeRemovingRelationshipEvent(rel_type, (a, role_of_a), (b, role_of_b), extra_info)) links_of_a.remove(link_a_to_b) # If links_of_b.find raises a ValueError, our data structures are out of # sync. link_b_to_a = links_of_b.find(role_of_b, a, role_of_a, rel_type) links_of_b.remove(link_b_to_a) zope.event.notify(RelationshipRemovedEvent(rel_type, (a, role_of_a), (b, role_of_b),
uri_cache.cache(link.role) link_a = Link(link.my_role, link.target, link.role, link.rel_type, shared) IRelationshipLinks(obj).add(link_a) link_b = Link(link.role, obj, link.my_role, link.rel_type, shared) IRelationshipLinks(link.target).add(link_b) zope.event.notify( RelationshipAddedEvent(link.rel_type, (obj, link.my_role), (link.target, link.role), shared)) def unrelate(rel_type, (a, role_of_a), (b, role_of_b)): """Break a relationship between objects `a` and `b`.""" links_of_a = IRelationshipLinks(a) links_of_b = IRelationshipLinks(b) try: link_a_to_b = links_of_a.find(role_of_a, b, role_of_b, rel_type) except ValueError: raise NoSuchRelationship extra_info = link_a_to_b.extra_info zope.event.notify( BeforeRemovingRelationshipEvent(rel_type, (a, role_of_a), (b, role_of_b), extra_info)) links_of_a.remove(link_a_to_b) # If links_of_b.find raises a ValueError, our data structures are out of # sync. link_b_to_a = links_of_b.find(role_of_b, a, role_of_a, rel_type) links_of_b.remove(link_b_to_a) zope.event.notify( RelationshipRemovedEvent(rel_type, (a, role_of_a), (b, role_of_b), extra_info))