Skip to content

Commit

Permalink
Merge branch 'master' into fscherf/fix-draft-deletion-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun authored Mar 5, 2024
2 parents ab4cda2 + e0e4bbb commit 1706862
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 74 deletions.
21 changes: 12 additions & 9 deletions djangocms_alias/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from cms.plugin_base import CMSPluginBase, PluginMenuItem
from cms.plugin_pool import plugin_pool
from cms.toolbar.utils import get_object_edit_url
from cms.utils.permissions import (
get_model_permission_codename,
has_plugin_permission,
Expand Down Expand Up @@ -39,20 +40,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,
Expand Down
5 changes: 2 additions & 3 deletions djangocms_alias/cms_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
SHORTCUTS_BREAK,
)
from cms.toolbar.items import Break, ButtonList
from cms.toolbar.utils import get_object_edit_url
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,
Expand Down Expand Up @@ -185,8 +185,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):
Expand Down
6 changes: 1 addition & 5 deletions djangocms_alias/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,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")
Expand Down
4 changes: 2 additions & 2 deletions djangocms_alias/internal_search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cms.toolbar.utils import get_toolbar_from_request
from cms.toolbar.utils import get_object_preview_url, get_toolbar_from_request
from django.template import RequestContext
from django.utils.translation import gettext_lazy as _
from djangocms_internalsearch.base import BaseSearchConfig
Expand Down Expand Up @@ -77,7 +77,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)
Expand Down
17 changes: 1 addition & 16 deletions djangocms_alias/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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
from django.conf import settings
Expand Down Expand Up @@ -62,7 +61,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_admin_change_url(self):
"""Builds the url to the admin category change view"""
return admin_reverse(CHANGE_CATEGORY_URL_NAME, args=[self.pk])

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -300,9 +288,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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{% for item in object.objects_using %}
<li>
{{ item|verbose_name|capfirst|escape }}:
<a href="{{ item.get_absolute_url }}">{{ item }}</a>
{% if item|admin_view_url %}<a href="{{ item|admin_view_url }}">{{ item }}</a>{% else %}{{ item }}{% endif %}
</li>
{% endfor %}
</ul>
Expand Down
6 changes: 5 additions & 1 deletion djangocms_alias/templates/djangocms_alias/alias_usage.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
{% with item|verbose_name as object_type %}
<td>{{ object_type|capfirst|escape }}</td>
<td>
<a target="_top" onclick="closeSideframe()" href="{{ item.get_absolute_url }}">{{ item }}</a>
{% if item|admin_view_url %}
<a target="_top" onclick="closeSideframe()" href="{{ item|admin_view_url }}">{{ item }}</a>
{% else %}
{{ item }}
{% endif %}
</td>
<td>
{% if object_type == 'alias' %}
Expand Down
6 changes: 3 additions & 3 deletions djangocms_alias/templates/djangocms_alias/detach_alias.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<br/>

<form method="post">{% csrf_token %}
<div>
<input type="hidden" name="post" value="yes" />
<input type="submit" value="{% trans "Yes, I'm sure" %}" />
<input type="hidden" name="post" value="yes" />
<div class="submit-row">
<a href="#" class="button cancel-link">{% trans "No, take me back" %}</a>
<input type="submit" value="{% trans "Yes, I'm sure" %}" />
</div>
</form>
{% endblock %}
18 changes: 17 additions & 1 deletion djangocms_alias/templatetags/djangocms_alias_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from classytags.arguments import Argument, MultiValueArgument
from classytags.core import Tag
from cms.templatetags.cms_tags import PlaceholderOptions
from cms.toolbar.utils import get_toolbar_from_request
from cms.toolbar.utils import get_object_preview_url, get_toolbar_from_request
from cms.utils import get_current_site, get_language_from_request
from cms.utils.helpers import is_editable_model
from cms.utils.i18n import get_default_language, get_language_list
from cms.utils.placeholder import validate_placeholder_name
from cms.utils.urlutils import add_url_parameters, admin_reverse
Expand All @@ -27,6 +28,21 @@ def get_alias_usage_view_url(alias, **kwargs):
return add_url_parameters(url, **ChainMap(kwargs))


@register.filter()
def admin_view_url(obj):
if is_editable_model(obj.__class__):
# Is obj frontend-editable?
return get_object_preview_url(obj)
if hasattr(obj, "get_content"):
# Is its content object frontend-editable?
content_obj = obj.get_content()
if is_editable_model(content_obj.__class__):
return get_object_preview_url(content_obj)
if hasattr(obj, "get_absolute_url"):
return obj.get_absolute_url()
return ""


@register.filter()
def verbose_name(obj):
return obj._meta.verbose_name
Expand Down
69 changes: 63 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from bs4 import BeautifulSoup
from cms.api import add_plugin
from cms.toolbar.utils import get_object_preview_url
from cms.utils.i18n import force_language
from cms.utils.urlutils import add_url_parameters, admin_reverse
from django.contrib.auth.models import Permission
Expand Down Expand Up @@ -208,7 +209,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(
Expand Down Expand Up @@ -428,9 +429,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
Expand Down
7 changes: 4 additions & 3 deletions tests/test_cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,8 +51,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")
Expand Down Expand Up @@ -95,7 +96,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)
Expand Down
Loading

0 comments on commit 1706862

Please sign in to comment.