Skip to content

Commit

Permalink
Merge pull request #90 from plone/base_rels_update_on_checkin
Browse files Browse the repository at this point in the history
fix: update relations on Check-In fix #89
jensens authored May 22, 2021
2 parents ee12c16 + 8cead3c commit 0ed2747
Showing 3 changed files with 71 additions and 5 deletions.
3 changes: 3 additions & 0 deletions news/89.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Update relations on Check-In WorkingCopy, by trigger an ObjectModifiedEvent event
- black and flake8 formatting
[2silver]
5 changes: 5 additions & 0 deletions plone/app/iterate/dexterity/policy.py
Original file line number Diff line number Diff line change
@@ -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):
68 changes: 63 additions & 5 deletions plone/app/iterate/tests/test_iterate.py
Original file line number Diff line number Diff line change
@@ -142,17 +142,23 @@ 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()

self.repo.save(folder)
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()
@@ -281,3 +291,51 @@ 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)

# 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)

0 comments on commit 0ed2747

Please sign in to comment.