Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Re-introduce deleting languages of a page #443

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions djangocms_versioning/cms_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from django.utils.http import urlencode
from django.utils.translation import gettext_lazy as _

from djangocms_versioning.conf import LOCK_VERSIONS
from djangocms_versioning.conf import ALLOW_DELETING_VERSIONS, LOCK_VERSIONS
from djangocms_versioning.constants import DRAFT, PUBLISHED
from djangocms_versioning.helpers import (
get_latest_admin_viewable_content,
Expand Down Expand Up @@ -385,6 +385,18 @@ def change_language_menu(self):
)
add_plugins_menu.add_modal_item(name, url=url)

if remove and ALLOW_DELETING_VERSIONS:
remove_plugins_menu = language_menu.get_or_create_menu(
f"{LANGUAGE_MENU_IDENTIFIER}-del", _("Delete Translation")
)
disabled = len(remove) == 1
for code, name in remove:
pagecontent = self.page.get_admin_content(language=code)
if pagecontent:
translation_delete_url = admin_reverse("cms_pagecontent_delete", args=(pagecontent.pk,))
url = add_url_parameters(translation_delete_url, language=code)
remove_plugins_menu.add_modal_item(name, url=url, disabled=disabled)
fsbraun marked this conversation as resolved.
Show resolved Hide resolved

if copy:
copy_plugins_menu = language_menu.get_or_create_menu(
f"{LANGUAGE_MENU_IDENTIFIER}-copy", _("Copy all plugins")
Expand All @@ -394,7 +406,7 @@ def change_language_menu(self):
item_added = False
for code, name in copy:
# Get the Draft or Published PageContent.
page_content = self.get_page_content(language=code)
page_content = self.page.get_admin_content(language=code)
if page_content: # Only offer to copy if content for source language exists
page_copy_url = admin_reverse("cms_pagecontent_copy_language", args=(page_content.pk,))
copy_plugins_menu.add_ajax_item(
Expand Down
47 changes: 46 additions & 1 deletion tests/test_toolbars.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import re
from unittest.mock import patch

from cms.cms_toolbars import LANGUAGE_MENU_IDENTIFIER, PlaceholderToolbar
from cms.test_utils.testcases import CMSTestCase
from cms.toolbar.utils import get_object_edit_url, get_object_preview_url
Expand Down Expand Up @@ -488,7 +491,7 @@ def _get_toolbar_item_by_name(self, menu, name):

def test_change_language_menu_page_toolbar(self):
"""Check that patched PageToolbar.change_language_menu only provides
Add Translation links.
Add Translation links if DJANGOCMS_ALLOW_DELETING_VERSIONS is False.
"""
version = PageVersionFactory(content__language="en")
PageContentWithVersionFactory(page=version.content.page, language="de")
Expand Down Expand Up @@ -533,6 +536,48 @@ def test_change_language_menu_page_toolbar(self):
lang_code = "fr" if "Française" in item.name else "it"
self.assertIn(f"language={lang_code}", item.url)

def test_change_language_menu_page_toolbar_including_delete(self):
"""Check that patched PageToolbar.change_language_menu also provides
Delete Translation links if DJANGOCMS_ALLOW_DELETING_VERSIONS is True.
"""
from djangocms_versioning import cms_toolbars

with patch.object(cms_toolbars, "ALLOW_DELETING_VERSIONS", True):
fsbraun marked this conversation as resolved.
Show resolved Hide resolved
version = PageVersionFactory(content__language="en")
PageContentWithVersionFactory(page=version.content.page, language="de")
PageContentWithVersionFactory(page=version.content.page, language="it")
page = version.content.page
page.update_languages(["en", "de", "it"])

request = self.get_page_request(
page=page,
path=get_object_edit_url(version.content),
user=self.get_superuser(),
)
request.toolbar.set_object(version.content)
request.toolbar.populate()
request.toolbar.post_template_populate()

language_menu = request.toolbar.get_menu(LANGUAGE_MENU_IDENTIFIER)
# 3 out of 4 populated languages, Break, Add Translation menu, Copy all plugins
self.assertEqual(language_menu.get_item_count(), 7)

language_menu_dict = {
menu.name: list(menu.items)
for key, menu in language_menu.menus.items()
}
self.assertIn("Add Translation", language_menu_dict.keys())
self.assertIn("Copy all plugins", language_menu_dict.keys())
self.assertIn("Delete Translation", language_menu_dict.keys())

pattern = r"\?language=([a-z]{2})"
for item in language_menu_dict["Delete Translation"]:
match = re.search(pattern, item.url) # Contains "?language=<code>“?
self.assertTrue(bool(match))
code = match.group(1) # Extract code
fsbraun marked this conversation as resolved.
Show resolved Hide resolved
fsbraun marked this conversation as resolved.
Show resolved Hide resolved
pk = page.get_admin_content(code).pk # get content object
self.assertIn(admin_reverse("cms_pagecontent_delete", args=(int(pk),)), item.url) # verify url
fsbraun marked this conversation as resolved.
Show resolved Hide resolved
fsbraun marked this conversation as resolved.
Show resolved Hide resolved

def test_change_language_menu_page_toolbar_language_selector_version_link(self):
"""
Ensure that the correct version is navigated to in the language selector.
Expand Down
Loading