Skip to content

Commit

Permalink
fix: PageContent extension's copy_relations method not called (#344)
Browse files Browse the repository at this point in the history
* Fix: call extension's copy function

* Fix: Update tests

Co-authored-by: Jacob Rief <[email protected]>

---------

Co-authored-by: Jacob Rief <[email protected]>
  • Loading branch information
fsbraun and jrief authored Sep 22, 2023
1 parent 6ac73e8 commit 8e045dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
13 changes: 4 additions & 9 deletions djangocms_versioning/cms_config.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion tests/test_extensions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 8e045dd

Please sign in to comment.