[hooks] Drop hook preventing creation of "ambiguous" profile
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 02 Nov 2017 20:44:54 +0100
changeset 2827 0f1331287a4c
parent 2826 54a83326c9a5
child 2828 4921e19f5e62
[hooks] Drop hook preventing creation of "ambiguous" profile Those will be turn into rule of the diagnosis tool in the next cset. Related to #39302963.
cubicweb_seda/hooks.py
test/test_hooks.py
--- a/cubicweb_seda/hooks.py	Thu Nov 02 20:42:48 2017 +0100
+++ b/cubicweb_seda/hooks.py	Thu Nov 02 20:44:54 2017 +0100
@@ -374,101 +374,6 @@
             CheckProfileSEDACompatiblityOp.get_instance(self._cw).add_entity(self.entity)
 
 
-class AddedCardinalityCheckedRelationHook(hook.Hook):
-    """Some relations whose children should be checked for unhandled cardinality has
-    been added.
-    """
-    __regid__ = 'seda.transfer.checkcard.relations'
-    __select__ = hook.Hook.__select__ & hook.match_rtype_sets(set(CHECK_CHILDREN_CARD_RTYPES))
-    events = ('after_add_relation',)
-    category = 'integrity'
-
-    def __call__(self):
-        CheckChildrenUnhandledCardinalityOp.get_instance(self._cw).add_data(
-            (self.eidto, self.rtype, self.eidfrom))
-
-
-class UpdatedCardinalityCheckedEntityHook(hook.Hook):
-    """Some entity whose cardinality should be checked for unhandled cardinality has
-    been updated.
-    """
-    __regid__ = 'seda.transfer.checkcard.entities'
-    __select__ = hook.Hook.__select__ & is_instance(*CHECK_CARD_ETYPES)
-    events = ('after_update_entity',)
-    category = 'integrity'
-
-    def __call__(self):
-        # nothing to do if cardinality isn't edited or new value is '1'
-        if ('user_cardinality' not in self.entity.cw_edited
-                or self.entity.user_cardinality == '1'):
-            return
-        # back to its parent entity
-        icontained = self.entity.cw_adapt_to('IContained')
-        if icontained is None or not icontained.parent:
-            # entity is being created in the transaction, should be catched by
-            # AddedCardinalityCheckedRelationHook
-            return
-        parent = icontained.parent
-        rtype, role = icontained.parent_relation()
-        CheckChildrenUnhandledCardinalityOp.get_instance(self._cw).add_data(
-            (parent.eid, rtype, self.entity.eid))
-
-
-class CheckChildrenUnhandledCardinalityOp(hook.DataOperationMixIn, hook.LateOperation):
-    """Check we don't run into the case of an with more than one child through a
-    same relation with `user_cardinality != '1'` (otherwise it won't be
-    validable by RelaxNG validator like Jing).
-
-    Expect (parent_eid, rtype, error_entity_eid) to be added into the data container:
-
-    * eid of the parent whose child should be checked,
-
-    * name of the relation to retrieve its children (expected to be an object
-      relation),
-
-    * eid of the entity on which the `ValidationError` will be raised in case of
-      error.
-
-    This is a late operation since it has to be executed once the 'container' relation is set.
-    """
-
-    def precommit_event(self):
-        for parent_eid, rtype, added_entity_eid in self.get_data():
-            parent = self.cnx.entity_from_eid(parent_eid)
-            # if the container is a simplified profile, allow to have several
-            # entities with cardinality != 1 under the seda_binary_data_object
-            # relation because data objects are all linked to the transfer
-            # through this relation, but this is only relevant for SEDA 2 export
-            # and we don't want to prevent this in profiles for SEDA 0.2 and 1.0
-            # export.
-            if rtype == 'seda_binary_data_object':
-                if parent.cw_etype == 'SEDAArchiveTransfer':
-                    container = parent
-                else:
-                    if not parent.container:
-                        # we may have to clear the container cache if the parent has
-                        # been added in the same transaction
-                        parent.cw_clear_relation_cache('container', 'subject')
-                    container = parent.cw_adapt_to('IContained').container
-                if container.cw_etype != 'SEDAArchiveTransfer':
-                    # component archive unit, don't check the relation, it will
-                    # be checked upon import of the component into a transfer
-                    continue
-                if container.simplified_profile:
-                    continue
-            has_non_single_cardinality = False
-            # sort to attempt to return the child with the greatest eid
-            for child in sorted(parent.related(rtype, 'object', entities=True),
-                                key=lambda x: x.eid):
-                if child.user_cardinality != '1':
-                    if has_non_single_cardinality:
-                        msg = _('a sibling entity already has a cardinality non '
-                                'equal to "1" and only one is supported')
-                        raise ValidationError(added_entity_eid,
-                                              {role_name('user_cardinality', 'subject'): msg})
-                    has_non_single_cardinality = True
-
-
 # XXX rather kill SEDAMimeType and SEDAFormatId if client is fine with "auto"
 # cardinality handling
 class InitBDO(hook.Hook):
--- a/test/test_hooks.py	Thu Nov 02 20:42:48 2017 +0100
+++ b/test/test_hooks.py	Thu Nov 02 20:44:54 2017 +0100
@@ -124,57 +124,6 @@
                         {'rel_eid': rel.eid})
             cnx.commit()
 
-    def test_multiple_child_unhandled_cardinality_archive_unit(self):
-        with self.admin_access.cnx() as cnx:
-            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
-            testutils.create_archive_unit(transfer)
-            cnx.commit()
-            testutils.create_archive_unit(transfer, user_cardinality=u'0..1')
-            cnx.commit()
-            unit2 = testutils.create_archive_unit(transfer, user_cardinality=u'1')[0]
-            cnx.commit()
-            with self.assertValidationError(cnx) as cm:
-                unit2.cw_set(user_cardinality=u'1..n')
-            self.assertEqual(cm.exception.entity, unit2.eid)
-            self.assertIn('user_cardinality-subject', cm.exception.errors)
-            with self.assertValidationError(cnx) as cm:
-                unit3 = testutils.create_archive_unit(transfer, user_cardinality=u'0..1')[0]
-            self.assertEqual(cm.exception.entity, unit3.eid)
-            self.assertIn('user_cardinality-subject', cm.exception.errors)
-
-    def test_multiple_child_unhandled_cardinality_document(self):
-        with self.admin_access.cnx() as cnx:
-            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
-            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
-            testutils.create_data_object(unit_alt_seq, user_cardinality=u'0..n',
-                                         seda_binary_data_object=transfer)
-            cnx.commit()
-            with self.assertValidationError(cnx):
-                testutils.create_data_object(unit_alt_seq, user_cardinality=u'1..n',
-                                             seda_binary_data_object=transfer)
-            unit2, unit2_alt, unit2_alt_seq = testutils.create_archive_unit(transfer)
-            with self.assertValidationError(cnx):
-                testutils.create_data_object(unit2_alt_seq, user_cardinality=u'0..n',
-                                             seda_binary_data_object=transfer)
-            # though adding a data object with card != to another archive unit
-            # *on a simplified profile* is fine
-            transfer.cw_set(simplified_profile=True)
-            unit2, unit2_alt, unit2_alt_seq = testutils.create_archive_unit(transfer)
-            testutils.create_data_object(unit2_alt_seq, user_cardinality=u'0..n',
-                                         seda_binary_data_object=transfer)
-            cnx.commit()
-
-    def test_multiple_child_unhandled_cardinality_keyword(self):
-        with self.admin_access.cnx() as cnx:
-            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
-            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
-            cnx.create_entity('SEDAKeyword', seda_keyword=unit_alt_seq,
-                              user_cardinality=u'1..n')
-            cnx.commit()
-            with self.assertValidationError(cnx):
-                cnx.create_entity('SEDAKeyword', seda_keyword=unit_alt_seq,
-                                  user_cardinality=u'1..n')
-
 
 class SetDefaultHooksTC(CubicWebTC):