From 8e045dd4abc12d54e3ab6e0651b8ec1fd9ccea10 Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Fri, 22 Sep 2023 17:35:09 +0200 Subject: [PATCH] fix: PageContent extension's `copy_relations` method not called (#344) * Fix: call extension's copy function * Fix: Update tests Co-authored-by: Jacob Rief --------- Co-authored-by: Jacob Rief --- djangocms_versioning/cms_config.py | 13 ++++--------- tests/test_extensions.py | 8 +++++++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/djangocms_versioning/cms_config.py b/djangocms_versioning/cms_config.py index 489afbbd..bd113ccd 100644 --- a/djangocms_versioning/cms_config.py +++ b/djangocms_versioning/cms_config.py @@ -1,6 +1,7 @@ import collections from cms.app_base import CMSAppConfig, CMSAppExtension +from cms.extensions.models import BaseExtension from cms.models import PageContent, Placeholder from cms.utils import get_language_from_request from cms.utils.i18n import get_language_list, get_language_tuple @@ -224,17 +225,12 @@ def copy_page_content(original_content): new_placeholders.append(new_placeholder) new_content.placeholders.add(*new_placeholders) - # If pagecontent has an associated title or page extension, also copy this! + # If pagecontent has an associated content or page extension, also copy this! for field in PageContent._meta.related_objects: if hasattr(original_content, field.name): extension = getattr(original_content, field.name) - extension_fields = { - field.name: getattr(extension, field.name) - for field in extension._meta.fields - if field.name not in (PageContent._meta.pk.name, "extended_object") - } - extension_fields["extended_object"] = new_content - field.related_model.objects.create(**extension_fields) + if isinstance(extension, BaseExtension): + extension.copy(new_content, new_content.language) return new_content @@ -259,7 +255,6 @@ def on_page_content_publish(version): page._update_url_path_recursive(language) page.clear_cache(menu=True) - def on_page_content_unpublish(version): """Url path and cache operations to do when a PageContent obj is unpublished""" page = version.content.page diff --git a/tests/test_extensions.py b/tests/test_extensions.py index d7bbcfdf..bcb6c35e 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + from cms.extensions.extension_pool import ExtensionPool from cms.test_utils.testcases import CMSTestCase from cms.utils.urlutils import admin_reverse @@ -63,9 +65,12 @@ def test_pagecontent_copy_method_creates_extension_title_extension_attached(self page_content = self.version.content poll_extension = PollTitleExtensionFactory(extended_object=page_content) poll_extension.votes = 5 + poll_extension.save() - new_pagecontent = copy_page_content(page_content) + with patch("cms.extensions.PageContentExtension.copy_relations") as mock: + new_pagecontent = copy_page_content(page_content) + mock.assert_called_once() self.assertNotEqual(new_pagecontent.pollpagecontentextension, poll_extension) self.assertEqual(page_content.pollpagecontentextension.pk, poll_extension.pk) self.assertNotEqual(page_content.pollpagecontentextension.pk, new_pagecontent.pollpagecontentextension.pk) @@ -89,6 +94,7 @@ def test_pagecontent_copy_method_creates_extension_multiple_title_extension_atta page_content = self.version.content poll_extension = PollTitleExtensionFactory(extended_object=page_content) poll_extension.votes = 5 + poll_extension.save() # Needs to be in the db for copy method of core to work title_extension = TestTitleExtensionFactory(extended_object=page_content) new_pagecontent = copy_page_content(page_content)