From 5b15e0b6e9eb8fc83e1f570939e6ffb9e63048cc Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Sun, 12 Nov 2023 15:00:43 +0100 Subject: [PATCH 1/7] Remove `get_absolute_url()` from Alias --- djangocms_alias/cms_plugins.py | 21 ++++++++------- djangocms_alias/cms_toolbars.py | 5 ++-- djangocms_alias/forms.py | 5 +--- djangocms_alias/internal_search.py | 4 +-- djangocms_alias/models.py | 17 +----------- .../alias/delete_confirmation.html | 2 +- tests/test_admin.py | 9 ++++--- tests/test_cms_plugins.py | 7 ++--- tests/test_menu.py | 6 +++-- tests/test_models.py | 2 +- tests/test_views.py | 27 ++++++++++--------- tests/test_wizards.py | 6 +++-- 12 files changed, 51 insertions(+), 60 deletions(-) diff --git a/djangocms_alias/cms_plugins.py b/djangocms_alias/cms_plugins.py index 039547ee..6f80f6d2 100644 --- a/djangocms_alias/cms_plugins.py +++ b/djangocms_alias/cms_plugins.py @@ -1,5 +1,6 @@ from copy import copy +from cms.toolbar.utils import get_object_edit_url from django.utils.translation import ( get_language_from_request, gettext_lazy as _, @@ -41,20 +42,22 @@ def get_render_template(self, context, instance, placeholder): @classmethod def get_extra_plugin_menu_items(cls, request, plugin): if plugin.plugin_type == cls.__name__: - edit_endpoint = plugin.alias.get_absolute_url() + alias_content = plugin.alias.get_content() detach_endpoint = admin_reverse( DETACH_ALIAS_PLUGIN_URL_NAME, args=[plugin.pk], ) - plugin_menu_items = [ - PluginMenuItem( - _('Edit Alias'), - edit_endpoint, - action='sideframe', - attributes={'cms-icon': 'alias'}, - ), - ] + plugin_menu_items = [] + if alias_content: + plugin_menu_items.append( + PluginMenuItem( + _('Edit Alias'), + get_object_edit_url(alias_content), + action='', + attributes={'cms-icon': 'alias'}, + ), + ) if cls.can_detach( request.user, diff --git a/djangocms_alias/cms_toolbars.py b/djangocms_alias/cms_toolbars.py index faca20c5..275b7b60 100644 --- a/djangocms_alias/cms_toolbars.py +++ b/djangocms_alias/cms_toolbars.py @@ -1,6 +1,7 @@ import itertools from copy import copy +from cms.toolbar.utils import get_object_edit_url from django.urls import NoReverseMatch from django.utils.encoding import force_str from django.utils.http import urlencode @@ -20,7 +21,6 @@ from cms.toolbar_base import CMSToolbar from cms.toolbar_pool import toolbar_pool from cms.utils.i18n import ( - force_language, get_default_language, get_language_dict, get_language_tuple, @@ -193,8 +193,7 @@ def override_language_switcher(self): show_draft_content=True, ) if alias_content: - with force_language(code): - url = alias_content.get_absolute_url() + url = get_object_edit_url(alias_content, language=code) language_menu.add_link_item(name, url=url, active=self.current_lang == code) def change_language_menu(self): diff --git a/djangocms_alias/forms.py b/djangocms_alias/forms.py index a958d3ff..0741b1bb 100644 --- a/djangocms_alias/forms.py +++ b/djangocms_alias/forms.py @@ -153,14 +153,11 @@ def save(self): category=self.cleaned_data.get('category'), site=self.cleaned_data.get('site'), ) - alias_content = AliasContent.objects.create( + alias_content = AliasContent.objects.with_user(self.user).create( alias=alias, name=self.cleaned_data.get('name'), language=self.cleaned_data.get('language'), ) - if is_versioning_enabled(): - from djangocms_versioning.models import Version - Version.objects.create(content=alias_content, created_by=self.user) if self.cleaned_data.get('replace'): placeholder = self.cleaned_data.get('placeholder') plugin = self.cleaned_data.get('plugin') diff --git a/djangocms_alias/internal_search.py b/djangocms_alias/internal_search.py index acb0e5e3..d72b183d 100644 --- a/djangocms_alias/internal_search.py +++ b/djangocms_alias/internal_search.py @@ -1,7 +1,7 @@ from django.template import RequestContext from django.utils.translation import gettext_lazy as _ -from cms.toolbar.utils import get_toolbar_from_request +from cms.toolbar.utils import get_toolbar_from_request, get_object_preview_url from djangocms_internalsearch.base import BaseSearchConfig from djangocms_internalsearch.helpers import get_request, get_version_object @@ -79,7 +79,7 @@ def prepare_text(self, obj): return content def prepare_url(self, obj): - return obj.get_absolute_url() + return get_object_preview_url(obj) def prepare_category(self, obj): obj.alias.category.set_current_language(obj.language) diff --git a/djangocms_alias/models.py b/djangocms_alias/models.py index f6af0d63..a0b7e96d 100644 --- a/djangocms_alias/models.py +++ b/djangocms_alias/models.py @@ -13,7 +13,6 @@ from cms.models import CMSPlugin, Placeholder from cms.models.fields import PlaceholderRelationField from cms.models.managers import WithUserMixin -from cms.toolbar.utils import get_object_preview_url from cms.utils.plugins import copy_plugins_to_placeholder from cms.utils.urlutils import admin_reverse @@ -64,7 +63,7 @@ def __str__(self): # Be sure to be able to see the category name even if it's not in the current language return self.safe_translation_getter("name", any_language=True) - def get_absolute_url(self): + def get_edit_url(self): """Builds the url to the admin category change view""" return admin_reverse(CHANGE_CATEGORY_URL_NAME, args=[self.pk]) @@ -160,17 +159,6 @@ def get_name(self, language=None): return name - def get_absolute_url(self, language=None): - if is_versioning_enabled(): - from djangocms_versioning.helpers import ( - version_list_url_for_grouper, - ) - - return version_list_url_for_grouper(self) - content = self.get_content(language=language) - if content: - return content.get_absolute_url() - def get_content(self, language=None, show_draft_content=False): if not language: language = get_language() @@ -293,9 +281,6 @@ def placeholder(self): def get_placeholders(self): return [self.placeholder] - def get_absolute_url(self): - return get_object_preview_url(self) - def get_template(self): return 'djangocms_alias/alias_content.html' diff --git a/djangocms_alias/templates/admin/djangocms_alias/alias/delete_confirmation.html b/djangocms_alias/templates/admin/djangocms_alias/alias/delete_confirmation.html index ae4878ab..70c55eff 100644 --- a/djangocms_alias/templates/admin/djangocms_alias/alias/delete_confirmation.html +++ b/djangocms_alias/templates/admin/djangocms_alias/alias/delete_confirmation.html @@ -26,7 +26,7 @@ {% for item in object.objects_using %}
  • {{ item|verbose_name|capfirst|escape }}: - {{ item }} + {% if item.get_absolute_url %}{{ item }}{% else %}{{ item }}{% endif %}
  • {% endfor %} diff --git a/tests/test_admin.py b/tests/test_admin.py index 6b73d984..1e9ad91f 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -1,5 +1,6 @@ from unittest import skipUnless +from cms.toolbar.utils import get_object_edit_url, get_object_preview_url from django.contrib.auth.models import Permission from django.urls import reverse from django.utils.formats import localize @@ -223,7 +224,7 @@ def test_alias_content_manager_rendering_preview_add_url(self): response_content_decoded = response.content.decode() self.assertIn( - expected_en_content.get_absolute_url(), + get_object_preview_url(expected_en_content), response_content_decoded, ) self.assertNotIn( @@ -425,9 +426,9 @@ def test_aliascontent_list_view(self): self.assertNotContains(response, "Published") self.assertNotContains(response, "Draft") - aliascontent1_url = alias1.get_absolute_url() - aliascontent2_url = alias2.get_absolute_url() - aliascontent3_url = alias3.get_absolute_url() + aliascontent1_url = get_object_preview_url(alias1.get_content(show_draft_content=True)) + aliascontent2_url = get_object_preview_url(alias2.get_content(show_draft_content=True)) + aliascontent3_url = get_object_preview_url(alias3.get_content(show_draft_content=True)) # when versioning is not enabled, the django admin change form # is used which used links to the aliascontent_change view diff --git a/tests/test_cms_plugins.py b/tests/test_cms_plugins.py index af19b5d7..d231f504 100644 --- a/tests/test_cms_plugins.py +++ b/tests/test_cms_plugins.py @@ -3,6 +3,7 @@ from urllib.parse import urlparse from cms.api import add_plugin, create_title +from cms.toolbar.utils import get_object_edit_url from cms.utils import get_current_site from cms.utils.plugins import downcast_plugins from cms.utils.urlutils import admin_reverse @@ -51,8 +52,8 @@ def test_extra_plugin_items_for_alias_plugins(self): self.assertEqual(len(extra_items), 2) first, second = extra_items self.assertEqual(first.name, 'Edit Alias') - self.assertEqual(first.url, alias.get_absolute_url()) - self.assertEqual(first.action, 'sideframe') + self.assertEqual(first.url, get_object_edit_url(alias.get_content())) + self.assertEqual(first.action, '') self.assertEqual(second.name, 'Detach Alias') self.assertEqual(second.action, 'modal') @@ -96,7 +97,7 @@ def test_extra_plugin_items_with_versioning_checks(self): first = extra_items[0] # We cannot detach alias on undraft page self.assertEqual(first.name, 'Edit Alias') - self.assertEqual(first.url, alias.get_absolute_url()) + self.assertEqual(first.url, get_object_edit_url(alias.get_content())) def test_rendering_plugin_on_page(self): alias = self._create_alias(published=True) diff --git a/tests/test_menu.py b/tests/test_menu.py index 37f0d359..cd52513f 100644 --- a/tests/test_menu.py +++ b/tests/test_menu.py @@ -1,3 +1,5 @@ +from cms.toolbar.utils import get_object_edit_url + from djangocms_alias.utils import is_versioning_enabled from .base import BaseAliasPluginTestCase @@ -8,8 +10,8 @@ class AliasMenuTestCase(BaseAliasPluginTestCase): def test_alias_pages_have_no_menu_nodes(self): alias = self._create_alias() with self.login_user_context(self.superuser): - response = self.client.get(alias.get_absolute_url()) - if is_versioning_enabled(): + response = self.client.get(get_object_edit_url(alias.get_content())) + if is_versioning_enabled() and False: self.assertNotContains(response, '