[hook] Fix dispatching for file_category to format_id / mime_type on import of an archive unit component
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 19 Oct 2017 14:29:51 +0200
changeset 2787 6e325d616d1e
parent 2785 1662ae046c78
child 2788 4d31d5d82e35
[hook] Fix dispatching for file_category to format_id / mime_type on import of an archive unit component by changing related hook's category to metadata, since others are deactivated during sub-tree cloning.
cubicweb_seda/hooks.py
test/test_hooks.py
--- a/cubicweb_seda/hooks.py	Thu Oct 19 09:58:47 2017 +0200
+++ b/cubicweb_seda/hooks.py	Thu Oct 19 14:29:51 2017 +0200
@@ -520,6 +520,11 @@
     def precommit_event(self):
         for bdo_eid in self.get_data():
             bdo = self.cnx.entity_from_eid(bdo_eid)
+            if not bdo.container:
+                # we may have to clear the container cache if the parent has
+                # been added in the same transaction
+                bdo.cw_clear_relation_cache('container', 'subject')
+
             container = bdo.cw_adapt_to('IContained').container
             if container.cw_etype != 'SEDAArchiveTransfer':
                 # don't afford doing this in components which are not bound to
@@ -551,6 +556,7 @@
     __regid__ = 'seda.ux.filecategory'
     __select__ = hook.Hook.__select__ & hook.match_rtype('file_category')
     events = ('after_add_relation', 'after_delete_relation')
+    category = 'metadata'
 
     def __call__(self):
         SyncFileCategoryOp.get_instance(self._cw).add_data(self.eidfrom)
--- a/test/test_hooks.py	Thu Oct 19 09:58:47 2017 +0200
+++ b/test/test_hooks.py	Thu Oct 19 14:29:51 2017 +0200
@@ -20,6 +20,7 @@
 
 from cubicweb.devtools.testlib import CubicWebTC
 
+from cubicweb_compound.entities import copy_entity
 from cubicweb_seda import testutils, dataimport
 
 
@@ -305,6 +306,26 @@
                                    [concepts['application/msword']],
                                    [concepts['fmt/37'], concepts['fmt/38']])
 
+            # test no dispatch occurs on component archive unit...
+            unit_comp, unit_comp_alt, unit_comp_alt_seq = testutils.create_archive_unit(
+                None, cnx=cnx)
+            bdo_comp = testutils.create_data_object(unit_comp_alt_seq)
+            cnx.commit()
+
+            bdo_comp.cw_set(file_category=categories['document'])
+            cnx.commit()
+            self.assertFormatEqual(bdo_comp, u'0..1',
+                                   [],
+                                   [])
+            # until it's imported into some parent
+            copy_entity(unit_comp, seda_archive_unit=unit_alt_seq, clone_of=unit_comp)
+            cnx.commit()
+            transfer.cw_clear_relation_cache('seda_binary_data_object', 'object')
+            imported_bdo, = [x for x in transfer.binary_data_objects if x.eid != bdo.eid]
+            self.assertFormatEqual(imported_bdo, u'1',
+                                   [concepts['application/msword'], concepts['application/pdf']],
+                                   [concepts['fmt/37'], concepts['fmt/38'], concepts['fmt/14']])
+
     def assertFormatEqual(self, bdo, cardinality, mime_types, format_ids):
         bdo.cw_clear_all_caches()
         bdo.reverse_seda_mime_type_from[0].cw_clear_all_caches()