author Denis Laxalde <>
Fri, 19 Oct 2018 13:59:56 +0200
changeset 2972 359177d6a1c8
parent 2898 8bd8b3533dec
permissions -rw-r--r--
Delete "container" relation on archive unit when unlinked from a profile - Container machinery got introduced in 143ae7a4a964, at that time the "container" relation was mandatory (on subject) for all entity types. - Integrity of this relation relied on the assumption that the relation was mandatory. (I.e. all entities mush have a link to their container and the only way to remove this link is to remove the container entity itself). - In a88deb387b2b, this assumption got broken as "container" relation was made optional for SEDAArchiveUnit as a subject. From there, when an archive unit got unlinked from a profile (through deletion of the "seda_archive_unit" relation), a "container" relation remained set. - This is problem since permission (especially on "delete" action) relies on the presence or absence of this relation. For instance, one would get an error when trying to delete an archive unit they just unlinked from a profile because they have no rights to delete the profile. So we fix this by dropping the "container" relation when a "seda_archive_unit" relation is deleted through a new hook. Additional test goes in as other container-related tests live there. In migration, we drop spurious "container" relation (not sure there are some).

# copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact --
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <>.
"""cubicweb-seda application package

Data Exchange Standard for Archival

from cubicweb_compound import skip_rtypes_set, structure_def, CompositeGraph

# control of compound graph by adding etype / rtype to the corresponding set below
GRAPH_SKIP_RTYPES = set(['container', 'clone_of'])

def seda_profile_container_def(schema):
    """Define container for SEDAArchiveTransfer, as a list of (etype, parent_rdefs)."""
    return structure_def(schema, 'SEDAArchiveTransfer',
                         skipetypes=GRAPH_SKIP_ETYPES, skiprtypes=GRAPH_SKIP_RTYPES).items()

def _iter_external_rdefs(eschema, skip_rtypes):
    """Return an iterator on (rdef, role) of external relations from entity schema (i.e.
    non-composite relations).
    for rschema, targets, role in eschema.relation_definitions():
        if rschema in skip_rtypes:
        for target_etype in targets:
            rdef = eschema.rdef(rschema, role, target_etype)
            if rdef.composite:
            yield rdef, role

def iter_all_rdefs(schema, container_etype):
    """Return an iterator on (rdef, role) of all relations of the compound graph starting from the
    given entity type, both internal (composite) and external (non-composite).
    graph = CompositeGraph(schema, skipetypes=GRAPH_SKIP_ETYPES, skiprtypes=GRAPH_SKIP_RTYPES)
    skip_external_rtypes = skip_rtypes_set(GRAPH_SKIP_RTYPES)
    stack = [container_etype]
    visited = set(stack)
    while stack:
        etype = stack.pop()
        for (rtype, role), targets in graph.child_relations(etype):
            rschema = schema.rschema(rtype)
            for target in targets:
                if role == 'subject':
                    rdef = rschema.rdefs[(etype, target)]
                    rdef = rschema.rdefs[(target, etype)]
                yield rdef, role

                if target not in visited:
        for rdef, role in _iter_external_rdefs(schema[etype], skip_external_rtypes):
            yield rdef, role