From 2273192e87d3f84929646b63c4df838733becb1f Mon Sep 17 00:00:00 2001 From: Michael Graf Date: Wed, 19 May 2021 22:17:08 +0200 Subject: [PATCH 1/4] fix: update relations on Check-In fix #89 --- plone/app/iterate/dexterity/policy.py | 5 +++ plone/app/iterate/tests/test_iterate.py | 54 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/plone/app/iterate/dexterity/policy.py b/plone/app/iterate/dexterity/policy.py index f88c69c..e90911c 100644 --- a/plone/app/iterate/dexterity/policy.py +++ b/plone/app/iterate/dexterity/policy.py @@ -13,6 +13,7 @@ from zope.component import adapter from zope.component import queryAdapter from zope.event import notify +from zope.lifecycleevent import ObjectModifiedEvent @adapter(IDexterityIterateAware) @@ -34,6 +35,10 @@ def checkin(self, checkin_message): new_baseline = copier.merge() # don't need to unlock the lock disappears with old baseline deletion notify(AfterCheckinEvent(new_baseline, checkin_message)) + + # update our new_baseline, ex. for all References ( and BackReferences) + notify(ObjectModifiedEvent(new_baseline)) + return new_baseline def _get_relation_to_baseline(self): diff --git a/plone/app/iterate/tests/test_iterate.py b/plone/app/iterate/tests/test_iterate.py index 8fcf288..7a059be 100644 --- a/plone/app/iterate/tests/test_iterate.py +++ b/plone/app/iterate/tests/test_iterate.py @@ -281,3 +281,57 @@ def test_relationship_deleted_on_cancel_checkout(self): rels = list(catalog.findRelations({"from_id": obj_id})) self.assertEqual(len(rels), 0) + + def test_baseline_relations_updated_on_checkin(self): + # Ensure that relations between the baseline and + # and other objects are up-to-date on checkin + from zope.event import notify + from zope.lifecycleevent import ObjectModifiedEvent + from z3c.relationfield import RelationValue + + folder = self.portal.docs + baseline = folder.doc1 + target = folder.doc2 + + intids = component.getUtility(IIntIds) + catalog = component.getUtility(ICatalog) + + target_id = intids.getId(target) + target_rel = [RelationValue(target_id)] + + # Test, if nothing is present in the relation catalog + rels = list(catalog.findRelations({'to_id': target_id})) + self.assertEqual(len(rels), 0) + + # set relatedItems on baseline + baseline.relatedItems = target_rel + notify(ObjectModifiedEvent(baseline)) + + # Test, if relation is present in the relation catalog + rels = list(catalog.findRelations({'to_id': target_id})) + self.assertEqual(len(rels), 1) + + # proof for empty relations + # baseline.relatedItems = [] + # notify(ObjectModifiedEvent(baseline)) + # rels = list(catalog.findRelations({'to_id': target_id})) + # self.assertEqual(len(rels), 0) + + # make a workingcopy from baseline + wc = ICheckinCheckoutPolicy(baseline).checkout(folder) + + # remove relations on wc + wc.relatedItems = [] + notify(ObjectModifiedEvent(wc)) + + # baseline -> target relation should be still there, we are on wc + rels = list(catalog.findRelations({'to_id': target_id})) + self.assertEqual(len(rels), 1) + + # baseline -> target relation should be empty now + # because we replaced our baseline with our wc (with empty relations) + baseline = ICheckinCheckoutPolicy(wc).checkin("updated") + rels = list(catalog.findRelations({'to_id': target_id})) + + # new baseline's relatedItems should be empty + self.assertEqual(len(rels), 0) From 8c061e46e7f1d13cc4cf44e72b28c6a08368ea6d Mon Sep 17 00:00:00 2001 From: Michael Graf Date: Thu, 20 May 2021 07:58:19 +0200 Subject: [PATCH 2/4] chore: remove debug comment --- plone/app/iterate/tests/test_iterate.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/plone/app/iterate/tests/test_iterate.py b/plone/app/iterate/tests/test_iterate.py index 7a059be..1662ad1 100644 --- a/plone/app/iterate/tests/test_iterate.py +++ b/plone/app/iterate/tests/test_iterate.py @@ -311,12 +311,6 @@ def test_baseline_relations_updated_on_checkin(self): rels = list(catalog.findRelations({'to_id': target_id})) self.assertEqual(len(rels), 1) - # proof for empty relations - # baseline.relatedItems = [] - # notify(ObjectModifiedEvent(baseline)) - # rels = list(catalog.findRelations({'to_id': target_id})) - # self.assertEqual(len(rels), 0) - # make a workingcopy from baseline wc = ICheckinCheckoutPolicy(baseline).checkout(folder) From 1cc0648d506b8c77a2f0ddc00de14436e4fea89b Mon Sep 17 00:00:00 2001 From: Michael Graf Date: Thu, 20 May 2021 08:02:32 +0200 Subject: [PATCH 3/4] chore: black and flake8 --- plone/app/iterate/tests/test_iterate.py | 30 ++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/plone/app/iterate/tests/test_iterate.py b/plone/app/iterate/tests/test_iterate.py index 1662ad1..9739f6a 100644 --- a/plone/app/iterate/tests/test_iterate.py +++ b/plone/app/iterate/tests/test_iterate.py @@ -142,9 +142,13 @@ def test_folderContents(self): version of the folder. UIDs of contained content are also preserved.""" container = self.portal.docs - folder = container[container.invokeFactory(type_name="Folder", id="foo-folder")] + folder = container[ + container.invokeFactory(type_name="Folder", id="foo-folder") + ] existing_doc = folder[ - folder.invokeFactory(type_name="Document", id="existing-folder-item") + folder.invokeFactory( + type_name="Document", id="existing-folder-item" + ) ] existing_doc_uid = existing_doc.UID() @@ -152,7 +156,9 @@ def test_folderContents(self): wc = ICheckinCheckoutPolicy(folder).checkout(container) new_doc = wc[ wc.invokeFactory( - type_name="Document", id="new-folder-item", text="new folder item text" + type_name="Document", + id="new-folder-item", + text="new folder item text", ) ] new_doc_uid = new_doc.UID() @@ -161,10 +167,14 @@ def test_folderContents(self): catalog = getToolByName(self.portal, "portal_catalog") self.assertTrue("existing-folder-item" in new_folder) - self.assertEqual(new_folder["existing-folder-item"].UID(), existing_doc_uid) + self.assertEqual( + new_folder["existing-folder-item"].UID(), existing_doc_uid + ) self.assertTrue("new-folder-item" in new_folder) self.assertEqual(new_folder["new-folder-item"].UID(), new_doc_uid) - brains = catalog(path="/".join(new_folder["new-folder-item"].getPhysicalPath())) + brains = catalog( + path="/".join(new_folder["new-folder-item"].getPhysicalPath()) + ) self.assertTrue(brains) self.assertTrue( "new folder item text" in new_folder["new-folder-item"].getText() @@ -300,7 +310,7 @@ def test_baseline_relations_updated_on_checkin(self): target_rel = [RelationValue(target_id)] # Test, if nothing is present in the relation catalog - rels = list(catalog.findRelations({'to_id': target_id})) + rels = list(catalog.findRelations({"to_id": target_id})) self.assertEqual(len(rels), 0) # set relatedItems on baseline @@ -308,7 +318,7 @@ def test_baseline_relations_updated_on_checkin(self): notify(ObjectModifiedEvent(baseline)) # Test, if relation is present in the relation catalog - rels = list(catalog.findRelations({'to_id': target_id})) + rels = list(catalog.findRelations({"to_id": target_id})) self.assertEqual(len(rels), 1) # make a workingcopy from baseline @@ -319,13 +329,13 @@ def test_baseline_relations_updated_on_checkin(self): notify(ObjectModifiedEvent(wc)) # baseline -> target relation should be still there, we are on wc - rels = list(catalog.findRelations({'to_id': target_id})) + rels = list(catalog.findRelations({"to_id": target_id})) self.assertEqual(len(rels), 1) # baseline -> target relation should be empty now - # because we replaced our baseline with our wc (with empty relations) + # because we replaced our baseline with our wc (with empty relations) baseline = ICheckinCheckoutPolicy(wc).checkin("updated") - rels = list(catalog.findRelations({'to_id': target_id})) + rels = list(catalog.findRelations({"to_id": target_id})) # new baseline's relatedItems should be empty self.assertEqual(len(rels), 0) From 8cead3cbcc55cb167695916dda02fad86835425b Mon Sep 17 00:00:00 2001 From: Michael Graf Date: Thu, 20 May 2021 08:05:53 +0200 Subject: [PATCH 4/4] chore: add changes message --- news/89.bugfix | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 news/89.bugfix diff --git a/news/89.bugfix b/news/89.bugfix new file mode 100644 index 0000000..6a24dd6 --- /dev/null +++ b/news/89.bugfix @@ -0,0 +1,3 @@ +- Update relations on Check-In WorkingCopy, by trigger an ObjectModifiedEvent event +- black and flake8 formatting +[2silver] \ No newline at end of file