예제 #1
0
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)
예제 #2
0

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))


def unrelateAll(obj):
    """Break all relationships of `obj`.

    Note that this operation is not atomic: if an event subscriber catches
    a BeforeRemovingRelationshipEvent and vetoes the operation, some
예제 #3
0
                               (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))


def unrelateAll(obj):
    """Break all relationships of `obj`.

    Note that this operation is not atomic: if an event subscriber catches
    a BeforeRemovingRelationshipEvent and vetoes the operation, some
    relationships may have been removed, while others may still be there.