Skip to content

Commit

Permalink
Merge branch 'master' into fix/remove_get_abs_url
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun authored Mar 5, 2024
2 parents 0cfb612 + 84df2ef commit ef5760e
Show file tree
Hide file tree
Showing 34 changed files with 300 additions and 511 deletions.
28 changes: 8 additions & 20 deletions djangocms_alias/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from cms.admin.utils import GrouperModelAdmin
from cms.utils.permissions import get_model_permission_codename
from cms.utils.urlutils import admin_reverse
from django import forms
from django.contrib import admin
from django.http import (
Expand All @@ -8,11 +11,6 @@
)
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _

from cms.admin.utils import GrouperModelAdmin
from cms.utils.permissions import get_model_permission_codename
from cms.utils.urlutils import admin_reverse

from parler.admin import TranslatableAdmin

from .cms_config import AliasCMSConfig
Expand All @@ -32,7 +30,6 @@
is_versioning_enabled,
)


__all__ = [
"AliasAdmin",
"CategoryAdmin",
Expand Down Expand Up @@ -94,9 +91,7 @@ def get_actions_list(self) -> list:
"""Add alias usage list actions"""
return super().get_actions_list() + [self._get_alias_usage_link]

def can_change_content(
self, request: HttpRequest, content_obj: AliasContent
) -> bool:
def can_change_content(self, request: HttpRequest, content_obj: AliasContent) -> bool:
"""Returns True if user can change content_obj"""
if content_obj and is_versioning_enabled():
version = Version.objects.get_for_content(content_obj)
Expand All @@ -114,9 +109,7 @@ def has_delete_permission(self, request: HttpRequest, obj: Alias = None) -> bool
return request.user.is_superuser
return False

def save_model(
self, request: HttpRequest, obj: Alias, form: forms.Form, change: bool
) -> None:
def save_model(self, request: HttpRequest, obj: Alias, form: forms.Form, change: bool) -> None:
super().save_model(request, obj, form, change)

# Only emit content changes if Versioning is not installed because
Expand Down Expand Up @@ -150,9 +143,7 @@ def delete_model(self, request: HttpRequest, obj: Alias):
sender=self.model,
)

def _get_alias_usage_link(
self, obj: Alias, request: HttpRequest, disabled: bool = False
) -> str:
def _get_alias_usage_link(self, obj: Alias, request: HttpRequest, disabled: bool = False) -> str:
url = admin_reverse(USAGE_ALIAS_URL_NAME, args=[obj.pk])
return self.admin_action_button(url, "info", _("View usage"), disabled=disabled)

Expand All @@ -172,9 +163,7 @@ class AliasContentAdmin(admin.ModelAdmin):
actions = None
change_form_template = "admin/djangocms_alias/aliascontent/change_form.html"

def changelist_view(
self, request: HttpRequest, extra_context: dict = None
) -> HttpResponse:
def changelist_view(self, request: HttpRequest, extra_context: dict = None) -> HttpResponse:
"""Needed for the Alias Content Admin breadcrumbs"""
return HttpResponseRedirect(
admin_reverse(
Expand All @@ -194,8 +183,7 @@ def change_view(
if not obj:
raise Http404()
return HttpResponseRedirect(
admin_reverse(CHANGE_ALIAS_URL_NAME, args=(obj.alias_id,))
+ f"?language={obj.language}"
admin_reverse(CHANGE_ALIAS_URL_NAME, args=(obj.alias_id,)) + f"?language={obj.language}"
)

def has_module_permission(self, request: HttpRequest) -> bool:
Expand Down
16 changes: 4 additions & 12 deletions djangocms_alias/cms_config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from cms.app_base import CMSAppConfig
from django.apps import apps
from django.conf import settings

from cms.app_base import CMSAppConfig

from .models import AliasContent, AliasPlugin, copy_alias_content
from .rendering import render_alias_content


try:
apps.get_app_config("djangocms_internalsearch")
from .internal_search import AliasContentConfig
Expand All @@ -21,17 +19,13 @@ class AliasCMSConfig(CMSAppConfig):
cms_toolbar_enabled_models = [(AliasContent, render_alias_content)]
moderated_models = [AliasContent]

djangocms_moderation_enabled = getattr(
settings, "MODERATING_ALIAS_MODELS_ENABLED", True
)
djangocms_moderation_enabled = getattr(settings, "MODERATING_ALIAS_MODELS_ENABLED", True)
djangocms_versioning_enabled = (
getattr(settings, "VERSIONING_ALIAS_MODELS_ENABLED", True)
and djangocms_versioning_installed
getattr(settings, "VERSIONING_ALIAS_MODELS_ENABLED", True) and djangocms_versioning_installed
)

if djangocms_versioning_enabled:
from cms.utils.i18n import get_language_tuple

from djangocms_versioning.datastructures import VersionableItem

versioning = [
Expand All @@ -45,9 +39,7 @@ class AliasCMSConfig(CMSAppConfig):
),
]

djangocms_references_enabled = getattr(
settings, "REFERENCES_ALIAS_MODELS_ENABLED", True
)
djangocms_references_enabled = getattr(settings, "REFERENCES_ALIAS_MODELS_ENABLED", True)
reference_fields = [
(AliasPlugin, "alias"),
]
Expand Down
4 changes: 1 addition & 3 deletions djangocms_alias/cms_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ class AliasDisableMenu(Modifier):
"""Disable menu rendering on alias pages"""

def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
if request.toolbar.app_name == PLUGIN_URL_NAME_PREFIX or isinstance(
request.toolbar.obj, AliasContent
):
if request.toolbar.app_name == PLUGIN_URL_NAME_PREFIX or isinstance(request.toolbar.obj, AliasContent):
return []
return nodes

Expand Down
28 changes: 11 additions & 17 deletions djangocms_alias/cms_plugins.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
from copy import copy

from django.utils.translation import (
get_language_from_request,
gettext_lazy as _,
)

from cms.plugin_base import CMSPluginBase, PluginMenuItem
from cms.plugin_pool import plugin_pool
from cms.toolbar.utils import get_object_edit_url
Expand All @@ -14,11 +9,17 @@
)
from cms.utils.plugins import copy_plugins_to_placeholder
from cms.utils.urlutils import add_url_parameters, admin_reverse
from django.utils.translation import (
get_language_from_request,
)
from django.utils.translation import (
gettext_lazy as _,
)

from .constants import CREATE_ALIAS_URL_NAME, DETACH_ALIAS_PLUGIN_URL_NAME
from .forms import AliasPluginForm
from .models import Alias as AliasModel, AliasContent, AliasPlugin

from .models import Alias as AliasModel
from .models import AliasContent, AliasPlugin

__all__ = [
"Alias",
Expand All @@ -32,10 +33,7 @@ class Alias(CMSPluginBase):
form = AliasPluginForm

def get_render_template(self, context, instance, placeholder):
if (
isinstance(instance.placeholder.source, AliasContent)
and instance.is_recursive()
):
if isinstance(instance.placeholder.source, AliasContent) and instance.is_recursive():
return "djangocms_alias/alias_recursive.html"
return f"djangocms_alias/{instance.template}/alias.html"

Expand Down Expand Up @@ -117,9 +115,7 @@ def can_create_alias(cls, user, plugins=None, replace=False):
return True
elif replace:
target_placeholder = plugins[0].placeholder
if not target_placeholder.check_source(user) or not has_plugin_permission(
user, Alias.__name__, "add"
):
if not target_placeholder.check_source(user) or not has_plugin_permission(user, Alias.__name__, "add"):
return False

return all(
Expand All @@ -144,9 +140,7 @@ def can_detach(cls, user, target_placeholder, plugins):

@classmethod
def detach_alias_plugin(cls, plugin, language):
source_placeholder = plugin.alias.get_placeholder(
language, show_draft_content=True
) # We're in edit mode
source_placeholder = plugin.alias.get_placeholder(language, show_draft_content=True) # We're in edit mode
target_placeholder = plugin.placeholder

# Deleting uses a copy of a plugin to preserve pk on existing
Expand Down
46 changes: 16 additions & 30 deletions djangocms_alias/cms_toolbars.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import itertools
from copy import copy

from django.urls import NoReverseMatch
from django.utils.encoding import force_str
from django.utils.http import urlencode
from django.utils.translation import (
get_language_from_request,
gettext,
gettext_lazy as _,
)

from cms.cms_toolbars import (
ADMIN_MENU_IDENTIFIER,
ADMINISTRATION_BREAK,
Expand All @@ -27,6 +18,16 @@
)
from cms.utils.permissions import get_model_permission_codename
from cms.utils.urlutils import add_url_parameters, admin_reverse
from django.urls import NoReverseMatch
from django.utils.encoding import force_str
from django.utils.http import urlencode
from django.utils.translation import (
get_language_from_request,
gettext,
)
from django.utils.translation import (
gettext_lazy as _,
)

from .constants import (
DELETE_ALIAS_URL_NAME,
Expand All @@ -36,7 +37,6 @@
from .models import Alias, AliasContent
from .utils import is_versioning_enabled


__all__ = [
"AliasToolbar",
]
Expand Down Expand Up @@ -71,11 +71,7 @@ def add_aliases_link_to_admin_menu(self):

url = admin_reverse(LIST_ALIAS_URL_NAME)
obj = self.toolbar.get_object()
language = (
obj.language
if hasattr(obj, "language")
else get_language_from_request(self.request)
)
language = obj.language if hasattr(obj, "language") else get_language_from_request(self.request)
if language is None:
language = get_default_language()
url += f'?{urlencode({"language": language})}'
Expand Down Expand Up @@ -152,19 +148,15 @@ def get_insert_position(cls, admin_menu, item_name):
return end.index

def enable_create_wizard_button(self):
button_lists = [
result.item for result in self.toolbar.find_items(item_type=ButtonList)
]
button_lists = [result.item for result in self.toolbar.find_items(item_type=ButtonList)]
buttons = list(
# flatten the list
itertools.chain.from_iterable([item.buttons for item in button_lists])
)

# There will always be this button, because we are in the context of
# alias app views
create_wizard_button = [
button for button in buttons if button.name == gettext("Create")
][0]
create_wizard_button = [button for button in buttons if button.name == gettext("Create")][0]

from cms.wizards.wizard_pool import entry_choices

Expand Down Expand Up @@ -216,17 +208,13 @@ def change_language_menu(self):
current_placeholder = alias_content.placeholder

remove = [
(code, languages.get(code, code))
for code in alias_content.alias.get_languages()
if code in languages
(code, languages.get(code, code)) for code in alias_content.alias.get_languages() if code in languages
]
add = [code for code in languages.items() if code not in remove]
copy = [
(code, name)
for code, name in languages.items()
if code != self.current_lang
and (code, name) in remove
and current_placeholder
if code != self.current_lang and (code, name) in remove and current_placeholder
]

if add or remove or copy:
Expand All @@ -240,9 +228,7 @@ def change_language_menu(self):
add_url = admin_reverse("djangocms_alias_aliascontent_add")

for code, name in add:
url = add_url_parameters(
add_url, language=code, alias=alias_content.alias_id
)
url = add_url_parameters(add_url, language=code, alias=alias_content.alias_id)
add_plugins_menu.add_modal_item(name, url=url)

if remove:
Expand Down
6 changes: 3 additions & 3 deletions djangocms_alias/cms_wizards.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.utils.translation import gettext_lazy as _

from cms.utils.permissions import get_model_permission_codename
from cms.wizards.wizard_base import Wizard
from cms.wizards.wizard_pool import wizard_pool
from django.utils.translation import gettext_lazy as _

from .cms_plugins import Alias
from .forms import CreateAliasWizardForm, CreateCategoryWizardForm
from .models import Alias as AliasModel, Category
from .models import Alias as AliasModel
from .models import Category


class CreateAliasWizard(Wizard):
Expand Down
1 change: 0 additions & 1 deletion djangocms_alias/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

import django


DJANGO_GTE_21 = LooseVersion(django.get_version()) >= LooseVersion("2.1")
25 changes: 7 additions & 18 deletions djangocms_alias/filters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from cms.forms.utils import get_sites
from django.contrib import admin
from django.utils.encoding import smart_str
from django.utils.translation import get_language, gettext_lazy as _

from cms.forms.utils import get_sites
from django.utils.translation import get_language
from django.utils.translation import gettext_lazy as _

from .cms_config import AliasCMSConfig
from .constants import (
Expand All @@ -12,7 +12,6 @@
)
from .models import Category


djangocms_versioning_enabled = AliasCMSConfig.djangocms_versioning_enabled


Expand All @@ -39,17 +38,13 @@ def choices(self, changelist):
}
yield {
"selected": self.value() == SITE_FILTER_NO_SITE_VALUE,
"query_string": changelist.get_query_string(
{self.parameter_name: SITE_FILTER_NO_SITE_VALUE}
),
"query_string": changelist.get_query_string({self.parameter_name: SITE_FILTER_NO_SITE_VALUE}),
"display": _("No site"),
}
for lookup, title in self.lookup_choices:
yield {
"selected": self.value() == str(lookup),
"query_string": changelist.get_query_string(
{self.parameter_name: lookup}
),
"query_string": changelist.get_query_string({self.parameter_name: lookup}),
"display": title,
}

Expand All @@ -63,11 +58,7 @@ def lookups(self, request, model_admin):
qs = model_admin.get_queryset(request)
cat_id = qs.values_list("category", flat=True)
# Ensure the category is ordered by the name alphabetically by default
cat = (
Category.objects.filter(pk__in=cat_id)
.translated(get_language())
.order_by("translations__name")
)
cat = Category.objects.filter(pk__in=cat_id).translated(get_language()).order_by("translations__name")
for obj in cat:
yield str(obj.pk), smart_str(obj)

Expand All @@ -84,8 +75,6 @@ def choices(self, changelist):
for lookup, title in self.lookup_choices:
yield {
"selected": self.value() == str(lookup),
"query_string": changelist.get_query_string(
{self.parameter_name: lookup}
),
"query_string": changelist.get_query_string({self.parameter_name: lookup}),
"display": title,
}
Loading

0 comments on commit ef5760e

Please sign in to comment.