Stop relying on a hook to create SEDATitle if necessary
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 11 Jan 2017 22:20:59 +0100
changeset 2294 5c4708aac9cb
parent 2293 71684d75fcd8
child 2295 8433dcfc7419
Stop relying on a hook to create SEDATitle if necessary This is not anymore necessary for the web UI which may now inline this into the archive unit creation form.
hooks.py
test/test_diag.py
test/test_hooks.py
test/test_profile_generation.py
test/test_schema.py
test/testutils.py
--- a/hooks.py	Wed Jan 11 22:02:14 2017 +0100
+++ b/hooks.py	Wed Jan 11 22:20:59 2017 +0100
@@ -120,32 +120,6 @@
                                   {role_name('seda_ref_non_rule_id_to', 'subject'): msg})
 
 
-class EnsureArchiveUnitHasATitleOp(hook.DataOperationMixIn, hook.Operation):
-    """Make sure that a SEDAArchiveUnit has a title."""
-
-    def precommit_event(self):
-        for entity in self.get_data():
-            choice = entity.seda_alt_archive_unit_archive_unit_ref_id[0]
-            seq = choice.content_sequence
-            if seq is None:
-                # 'reference' archive unit
-                continue
-            if seq.reverse_seda_title:
-                # title is already set
-                continue
-            self.cnx.create_entity('SEDATitle', seda_title=seq)
-
-
-class EnsureArchiveUnitHasAContentHook(hook.Hook):
-    """Hook triggering an operation that will make sure that a SEDAArchiveUnit has a title."""
-    __regid__ = 'seda.archive-unit.ensure-content'
-    __select__ = hook.Hook.__select__ & is_instance('SEDAArchiveUnit')
-    events = ('before_add_entity', 'before_update_entity')
-
-    def __call__(self):
-        EnsureArchiveUnitHasATitleOp.get_instance(self._cw).add_data(self.entity)
-
-
 class EnsureChoiceNotEmptyOp(hook.DataOperationMixIn, hook.Operation):
     """Make sure that, when a choice element is created/updated, it is not empty."""
 
--- a/test/test_diag.py	Wed Jan 11 22:02:14 2017 +0100
+++ b/test/test_diag.py	Wed Jan 11 22:20:59 2017 +0100
@@ -92,7 +92,6 @@
                    user_cardinality=u'1',
                    seda_start_date=access_rule_seq)
 
-            create('SEDATitle', seda_title=unit_alt_seq)
             history_item = create('SEDACustodialHistoryItem',
                                   seda_custodial_history_item=unit_alt_seq)
 
--- a/test/test_hooks.py	Wed Jan 11 22:02:14 2017 +0100
+++ b/test/test_hooks.py	Wed Jan 11 22:20:59 2017 +0100
@@ -1,4 +1,4 @@
-# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr -- mailto:contact@logilab.fr
 #
 # This program is free software: you can redistribute it and/or modify it under
@@ -60,40 +60,6 @@
             cnx.commit()
 
 
-class ArchiveUnitContentTC(CubicWebTC):
-    """Test case checking hook creating title for SEDAArchiveUnit."""
-
-    def test_content_created(self):
-        """Check that a title entity is created when an empty ArchiveUnit is created."""
-        with self.admin_access.client_cnx() as cnx:
-            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
-            au, alt, seq = create_archive_unit(transfer)
-            cnx.commit()
-            self.assertTrue(au.first_level_choice.content_sequence.reverse_seda_title)
-
-    def test_no_new_content_create(self):
-        """Check that a title entity is not created when an ArchiveUnit with one is created.
-        """
-        with self.admin_access.client_cnx() as cnx:
-            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
-            au, alt, seq = create_archive_unit(transfer)
-            cnx.create_entity('SEDATitle', seda_title=seq)
-            cnx.commit()
-            self.assertEqual(len(seq.reverse_seda_title), 1)
-
-    def test_archive_unit_deletion(self):
-        """Check that hook does nothing when an ArchiveUnit is deleted.
-        """
-        with self.admin_access.client_cnx() as cnx:
-            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
-            au, alt, seq = create_archive_unit(transfer)
-            cnx.create_entity('SEDATitle', seda_title=seq)
-            cnx.commit()
-            # Check that everything goes fine when the hook is called
-            au.cw_delete()
-            cnx.commit()
-
-
 class NotEmptyChoiceTC(CubicWebTC):
     """Test case checking that xsd:choice elements cannot be empty."""
 
@@ -191,7 +157,6 @@
         with self.admin_access.client_cnx() as cnx:
             transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
             unit, unit_alt, unit_alt_seq = create_archive_unit(transfer)
-            cnx.create_entity('SEDATitle', seda_title=unit_alt_seq)
             version_of = cnx.create_entity('SEDAIsVersionOf', seda_is_version_of=unit_alt_seq)
             alt2 = cnx.create_entity('SEDAAltIsVersionOfArchiveUnitRefId',
                                      reverse_seda_alt_is_version_of_archive_unit_ref_id=version_of)
--- a/test/test_profile_generation.py	Wed Jan 11 22:02:14 2017 +0100
+++ b/test/test_profile_generation.py	Wed Jan 11 22:20:59 2017 +0100
@@ -593,7 +593,6 @@
             transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
             unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(
                 transfer, user_cardinality=u'0..n')
-            cnx.create_entity('SEDATitle', seda_title=unit_alt_seq)
             profile = self.profile_etree(transfer)
             title = self.get_element(profile, 'Title')
             self.assertEqual(len(self.xpath(title, 'rng:data')), 1)
@@ -652,7 +651,6 @@
                    seda_originating_agency_archive_unit_identifier=unit_alt_seq)
             create('SEDATransferringAgencyArchiveUnitIdentifier',
                    seda_transferring_agency_archive_unit_identifier=unit_alt_seq)
-            create('SEDATitle', seda_title=unit_alt_seq)
             create('SEDADescription', seda_description=unit_alt_seq)
             create('SEDALanguage', seda_language_from=unit_alt_seq)
             create('SEDADescriptionLanguage', seda_description_language_from=unit_alt_seq)
@@ -792,7 +790,6 @@
                                 reverse_seda_end_date=create('SEDAEndDate'),
                                 # XXX, value=date(2015, 2, 24)),
                                 reverse_seda_description=create('SEDADescription'))
-            create('SEDATitle', seda_title=unit_alt_seq)
 
             kw = create('SEDAKeyword',
                         user_cardinality=u'0..n',
@@ -818,7 +815,6 @@
             # Add sub archive unit
             _, _, subunit_alt_seq = testutils.create_archive_unit(unit_alt_seq,
                                                                   user_cardinality=u'1..n')
-            create('SEDATitle', seda_title=subunit_alt_seq)
 
             create('SEDAAppraisalRule',
                    seda_appraisal_rule=subunit_alt_seq,
@@ -851,7 +847,6 @@
             # Add another sub archive unit
             _, _, subunit2_alt_seq = testutils.create_archive_unit(unit_alt_seq,
                                                                    user_cardinality=u'1..n')
-            create('SEDATitle', seda_title=subunit2_alt_seq)
 
             cnx.commit()
 
@@ -927,7 +922,6 @@
             create('SEDAComment', seda_comment=transfer)
 
             unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
-            create('SEDATitle', seda_title=unit_alt_seq)
             create('SEDAArchivalAgreement', seda_archival_agreement=transfer)
             create('SEDATransferringAgencyArchiveUnitIdentifier',
                    seda_transferring_agency_archive_unit_identifier=unit_alt_seq)
@@ -944,7 +938,6 @@
 
             subunit, subunit_alt, subunit_alt_seq = testutils.create_archive_unit(
                 unit_alt_seq)
-            create('SEDATitle', seda_title=subunit_alt_seq)
             create('SEDATransferringAgencyArchiveUnitIdentifier',
                    seda_transferring_agency_archive_unit_identifier=subunit_alt_seq)
             create('SEDAStartDate', seda_start_date=subunit_alt_seq)
--- a/test/test_schema.py	Wed Jan 11 22:02:14 2017 +0100
+++ b/test/test_schema.py	Wed Jan 11 22:20:59 2017 +0100
@@ -1,4 +1,4 @@
-# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr -- mailto:contact@logilab.fr
 #
 # This program is free software: you can redistribute it and/or modify it under
@@ -219,8 +219,8 @@
         # entities that may live in the DB (e.g. Concepts) with similar text.
         with self.admin_access.client_cnx() as cnx:
             transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'Profile')
-            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
-            cnx.create_entity('SEDATitle', seda_title=unit_alt_seq, title=u'transfer name'[::-1])
+            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(
+                transfer, title=u'transfer name'[::-1])
             cnx.create_entity('SEDAAccessRule', seda_access_rule=unit_alt_seq,
                               user_annotation=u'some annotation'[::-1])
             testutils.create_data_object(transfer, filename=u'fixed.txt'[::-1])
@@ -296,23 +296,22 @@
     def test_archive_unit(self):
         with self.admin_access.repo_cnx() as cnx:
             unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(None, cnx=cnx)
-            title = cnx.create_entity('SEDATitle', seda_title=unit_alt_seq)
             cnx.commit()
 
             # unit has no parent, modifications are allowed.
             unit.cw_set(user_annotation=u'argh')
-            title.cw_set(title=u'gloup')
+            unit_alt_seq.reverse_seda_title[0].cw_set(title=u'gloup')
             cnx.commit()
 
         with self.new_access('anon').client_cnx() as cnx:
-            title = cnx.entity_from_eid(title.eid)
+            title = cnx.entity_from_eid(unit_alt_seq.reverse_seda_title[0].eid)
             unit = cnx.entity_from_eid(unit.eid)
             with self.assertUnauthorized(cnx):
                 title.cw_set(title=u'zorglub')
             with self.assertUnauthorized(cnx):
                 unit.cw_set(user_annotation=u'zorglub')
             with self.assertUnauthorized(cnx):
-                cnx.create_entity('SEDATitle', seda_title=unit_alt_seq)
+                cnx.create_entity('SEDADescription', seda_description=unit_alt_seq)
             with self.assertUnauthorized(cnx):
                 title.cw_delete()
             with self.assertUnauthorized(cnx):
--- a/test/testutils.py	Wed Jan 11 22:02:14 2017 +0100
+++ b/test/testutils.py	Wed Jan 11 22:20:59 2017 +0100
@@ -1,4 +1,4 @@
-# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr -- mailto:contact@logilab.fr
 #
 # This program is free software: you can redistribute it and/or modify it under
@@ -26,7 +26,7 @@
     return bdo
 
 
-def create_archive_unit(parent, archive_unit_reference=False, **kwargs):
+def create_archive_unit(parent, archive_unit_reference=False, title=None, **kwargs):
     """Create an archive unit and its mandatory children:
 
     return (archive unit, alternative, sequence) if archive_unit_reference is false (the default),
@@ -43,6 +43,8 @@
         last = cnx.create_entity(
             'SEDASeqAltArchiveUnitArchiveUnitRefIdManagement',
             reverse_seda_seq_alt_archive_unit_archive_unit_ref_id_management=alt)
+        cnx.create_entity('SEDATitle', seda_title=last, title=title)
+
     return au, alt, last