Skip to content

Commit 5a7e65a

Browse files
mrbazzanfsbraun
andauthored
fix: Update the pks for internal child plugins (#49)
Co-authored-by: Fabian Braun <fsbraun@gmx.de>
1 parent 319b78f commit 5a7e65a

File tree

7 files changed

+46
-18
lines changed

7 files changed

+46
-18
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ Use 'x' to check each item: [x] I have ...
2626
* [ ] I have opened this pull request against ``master``
2727
* [ ] I have added or modified the tests when changing logic
2828
* [ ] I have followed [the conventional commits guidelines](https://www.conventionalcommits.org/) to add meaningful information into the changelog
29-
* [ ] I have read the [contribution guidelines ](https://github.com/django-cms/django-cms/blob/develop/CONTRIBUTING.rst) and I have joined #workgroup-pr-review on
30-
[Slack](https://www.django-cms.org/slack) to find a “pr review buddy” who is going to review my pull request.
29+
* [ ] I have read the [contribution guidelines ](https://github.com/django-cms/django-cms/blob/develop/CONTRIBUTING.rst) and I have joined #workgroup-pr-review on
30+
[Discord](https://www.django-cms.org/discord) to find a “pr review buddy” who is going to review my pull request.

djangocms_transfer/importer.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from cms.models import CMSPlugin
22
from django.db import transaction
33

4-
from .utils import get_plugin_class
5-
64

75
@transaction.atomic
86
def import_plugins(plugins, placeholder, language, root_plugin_id=None):
@@ -36,16 +34,11 @@ def import_plugins(plugins, placeholder, language, root_plugin_id=None):
3634
)
3735
source_map[archived_plugin.pk] = plugin
3836

39-
new_plugins.append(plugin)
40-
41-
for new_plugin in new_plugins:
42-
plugin_class = get_plugin_class(new_plugin.plugin_type)
37+
new_plugins.append((plugin, archived_plugin))
4338

44-
if getattr(plugin_class, "_has_do_post_copy", False):
45-
# getattr is used for django CMS 3.4 compatibility
46-
# apps on 3.4 wishing to leverage this callback will need
47-
# to manually set the _has_do_post_copy attribute.
48-
plugin_class.do_post_copy(new_plugin, source_map)
39+
for new_plugin, _ in new_plugins:
40+
# Replace all internal child plugins with their new ids
41+
new_plugin.post_copy(new_plugin, new_plugins)
4942

5043

5144
@transaction.atomic

tests/requirements/base.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ freezegun
66
djangocms-text
77
pytest
88
pytest-django
9+
10+
# for testing purposes
11+
djangocms-link

tests/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"menus",
1717
"treebeard",
1818
"djangocms_text",
19+
"djangocms_link",
1920
"djangocms_transfer",
2021
]
2122

tests/test_migrations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
class MigrationTestCase(TestCase):
10-
@override_settings(MIGRATION_MODULES={}, DEFAULT_AUTO_FIELD="django.db.models.AutoField")
10+
@override_settings(MIGRATION_MODULES={"djangocms_link": None}, DEFAULT_AUTO_FIELD="django.db.models.AutoField")
1111
def test_for_missing_migrations(self):
1212
output = StringIO()
1313
options = {

tests/transfer/abstract.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@
55

66
@freeze_time("2024-02-28 00:00:00")
77
class FunctionalityBaseTestCase(CMSTestCase):
8+
9+
TEXT_BODY = "Hello World!"
10+
811
def setUp(self):
912
self.page = self._create_page()
1013
self.page_content = self.page.pagecontent_set(manager="admin_manager").first()
1114
self.page.set_as_homepage()
1215

13-
def _create_plugin(self, parent=None):
16+
def _create_plugin(self, plugin_type="TextPlugin", parent=None, **kwargs):
17+
if plugin_type == "TextPlugin":
18+
kwargs["body"] = self.TEXT_BODY
19+
elif plugin_type == "LinkPlugin":
20+
kwargs["name"] = plugin_type.lower()
21+
kwargs["link"] = {"external_link": "https://www.django-cms.org"}
22+
1423
return self._add_plugin_to_page(
15-
"TextPlugin",
24+
plugin_type,
1625
"last-child",
1726
parent,
18-
body="Hello World!",
27+
**kwargs
1928
)
2029

2130
def _create_page(self, **kwargs):

tests/transfer/test_import.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import json
22

3+
from cms.models import CMSPlugin
4+
from djangocms_text.utils import plugin_to_tag
5+
36
from djangocms_transfer.datastructures import (
47
ArchivedPlaceholder,
58
ArchivedPlugin,
@@ -16,14 +19,33 @@ def test_import(self):
1619
placeholder = pagecontent.get_placeholders().get(slot="content")
1720
plugin = self._create_plugin()
1821

22+
# create link plugin
23+
link_plugin = self._create_plugin(plugin_type="LinkPlugin", parent=plugin)
24+
25+
# Add plugin to text body
26+
plugin.body = f"{plugin.body} {plugin_to_tag(link_plugin)}"
27+
plugin.save()
28+
29+
link_plugin_data = ArchivedPlugin(
30+
**json.loads(export_plugin(link_plugin))[0]
31+
)
1932
plugin_data = ArchivedPlugin(**json.loads(export_plugin(plugin))[0])
2033
placeholder_data = ArchivedPlaceholder(
2134
"content",
2235
[ArchivedPlugin(**data) for data in json.loads(export_placeholder(placeholder, "en"))],
2336
)
2437

2538
with self.subTest("import plugins"):
26-
import_plugins([plugin_data], placeholder, "en")
39+
import_plugins([plugin_data, link_plugin_data], placeholder, "en")
40+
41+
# test import updates child plugin
42+
new_plugin, new_link_plugin = map(
43+
lambda plugin: plugin.get_bound_plugin(), CMSPlugin.objects.filter(pk__in=[3,4])
44+
)
45+
self.assertEqual(
46+
new_plugin.body,
47+
f"{self.TEXT_BODY} {plugin_to_tag(new_link_plugin)}"
48+
)
2749

2850
with self.subTest("import placeholder"):
2951
import_plugins(placeholder_data.plugins, placeholder, "en")

0 commit comments

Comments
 (0)