author Denis Laxalde <>
Fri, 19 Oct 2018 13:59:56 +0200
changeset 2972 359177d6a1c8
parent 2899 ed666cbea6af
child 3021 2212adf2c673
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 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 <>.
"""Generate CubicWeb's entities from XSD file"""

import json

from xsd2yams import MULTIPLE_CHILDREN, CodeGenerator
from xsd2uicfg import ordered_attributes

MULTIPLE_ETYPES = set(etype for etype, _ in MULTIPLE_CHILDREN)

TEMPLATE = '''class {etype}(SEDAAnyEntity):
    __regid__ = '{etype}'
    fetch_attrs, cw_fetch_order = fetch_config({attributes})
    value_attr = {value_attribute}

class EntitiesGenerator(CodeGenerator):

    def _generate(self, mapping, stream):
        stream.write('from cubicweb.entities import AnyEntity, fetch_config\n\n')
class SEDAAnyEntity(AnyEntity):
    __abstract__ = True
    value_attr = None

    def dc_title(self):
        if self.value_attr is None:
            return self.dc_type()
        return self.printable_value(self.value_attr)

        self.altetype2rtype = {}  # rtype to children elements for all choices
        self.check_card_etypes = set()  # entity types whose cardinality must be checked
        self.check_card_rtypes = set()  # relation types whose children cardinality must be checked
        for mapping_element in mapping.ordered:
            self._callback('register_alt_rtype_for', mapping_element)
            eclass_code = self._callback('entity_class_for', mapping_element)
            if eclass_code:
                stream.write(eclass_code + '\n')
        # Write pre-computed data structures as constants
        for name, data in [('CHOICE_RTYPE', self.altetype2rtype),
                           ('CHECK_CARD_ETYPES', sorted(self.check_card_etypes)),
                           ('CHECK_CHILDREN_CARD_RTYPES', sorted(self.check_card_rtypes))]:
            stream.write(u'{} = '.format(name))
            stream.write(json.dumps(data, sort_keys=True, indent=2,
                                    separators=(',', ': ')).replace('"', "'") + '\n')

    def entity_class_for_e_type_mapping(self, mapping):
        attributes = ordered_attributes(mapping)
        value_attribute = None
        if mapping.etype in ('SEDAArchiveUnit', 'SEDABinaryDataObject', 'SEDAPhysicalDataObject'):
        elif mapping.attributes:
            value_attribute = next(iter(mapping.attributes))
        elif and len( > 1:
            attributes = ['user_cardinality']
        if mapping.etype in MULTIPLE_ETYPES:
            attributes.insert(0, 'ordering')
        return TEMPLATE.format(etype=mapping.etype,

    def register_alt_rtype_for_rdef_mapping(self, mapping):
        composite = mapping.composite
        if not composite:
        altetypes = mapping.objtypes if composite == 'object' else [mapping.subjtype]
        for altetype in altetypes:
            if altetype.startswith('SEDAAlt'):
                self.altetype2rtype.setdefault(altetype, []).append((mapping.rtype, composite))

        if mapping.card[1] in '*+':

if __name__ == '__main__':