Skip to content

Commit

Permalink
ci: auto fixes from pre-commit hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Mar 9, 2024
1 parent 497f9bd commit 8b15ec1
Show file tree
Hide file tree
Showing 35 changed files with 1,883 additions and 1,494 deletions.
4 changes: 1 addition & 3 deletions djangocms_alias/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
__version__ = '1.11.0'

default_app_config = 'djangocms_alias.apps.AliasConfig'
__version__ = "1.11.0"
80 changes: 55 additions & 25 deletions djangocms_alias/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,44 @@


__all__ = [
'AliasAdmin',
'CategoryAdmin',
'AliasContentAdmin',
"AliasAdmin",
"CategoryAdmin",
"AliasContentAdmin",
]

alias_content_admin_classes = [admin.ModelAdmin]
alias_content_admin_list_display = ('name', 'get_category',)
alias_content_admin_list_filter = (SiteFilter, CategoryFilter, LanguageFilter,)
alias_content_admin_list_display = (
"name",
"get_category",
)
alias_content_admin_list_filter = (
SiteFilter,
CategoryFilter,
LanguageFilter,
)
djangocms_versioning_enabled = AliasCMSConfig.djangocms_versioning_enabled

if djangocms_versioning_enabled:
from djangocms_versioning.admin import ExtendedVersionAdminMixin

from .filters import UnpublishedFilter

alias_content_admin_classes.insert(0, ExtendedVersionAdminMixin)
alias_content_admin_list_display = ('name', 'get_category',)
alias_content_admin_list_filter = (SiteFilter, CategoryFilter, LanguageFilter, UnpublishedFilter)
alias_content_admin_list_display = (
"name",
"get_category",
)
alias_content_admin_list_filter = (
SiteFilter,
CategoryFilter,
LanguageFilter,
UnpublishedFilter,
)


@admin.register(Category)
class CategoryAdmin(TranslatableAdmin):
list_display = ['name']
list_display = ["name"]

def save_model(self, request, obj, form, change):
change = not obj._state.adding
Expand All @@ -59,10 +75,10 @@ def save_model(self, request, obj, form, change):

@admin.register(Alias)
class AliasAdmin(admin.ModelAdmin):
list_display = ['name', 'category']
list_filter = ['site', 'category']
fields = ('category', 'site')
readonly_fields = ('static_code', )
list_display = ["name", "category"]
list_filter = ["site", "category"]
fields = ("category", "site")
readonly_fields = ("static_code",)

def get_urls(self):
return urlpatterns + super().get_urls()
Expand All @@ -76,16 +92,21 @@ def has_delete_permission(self, request, obj=None):
if obj:
if not obj.is_in_use:
return request.user.has_perm(
get_model_permission_codename(self.model, 'add'),
get_model_permission_codename(self.model, "add"),
)
return request.user.is_superuser
return False

def get_deleted_objects(self, objs, request):
deleted_objects, model_count, perms_needed, protected = super().get_deleted_objects(objs, request)
(
deleted_objects,
model_count,
perms_needed,
protected,
) = super().get_deleted_objects(objs, request)
# This is bad and I should feel bad.
if 'placeholder' in perms_needed:
perms_needed.remove('placeholder')
if "placeholder" in perms_needed:
perms_needed.remove("placeholder")
return deleted_objects, model_count, perms_needed, protected

def save_model(self, request, obj, form, change):
Expand Down Expand Up @@ -118,12 +139,16 @@ class AliasContentAdmin(*alias_content_admin_classes):
def get_queryset(self, request):
queryset = super().get_queryset(request)
# Force the category set to Lower, to be able to sort the category in ascending/descending order
queryset = queryset.annotate(alias_category_translations_ordered=Lower("alias__category__translations__name"))
queryset = queryset.annotate(
alias_category_translations_ordered=Lower(
"alias__category__translations__name"
)
)
return queryset

# Add Alias category in the admin manager list and order field
@admin.display(
description=_('category'),
description=_("category"),
ordering="alias_category_translations_ordered",
)
def get_category(self, obj):
Expand Down Expand Up @@ -172,8 +197,9 @@ def get_list_display_links(self, request, list_display):
return super().get_list_display_links(request, list_display)

def _get_rename_alias_link(self, obj, request, disabled=False):
url = admin_reverse('{}_{}_change'.format(
obj._meta.app_label, obj._meta.model_name), args=(obj.pk,)
url = admin_reverse(
"{}_{}_change".format(obj._meta.app_label, obj._meta.model_name),
args=(obj.pk,),
)
return render_to_string(
"admin/djangocms_alias/icons/rename_alias.html",
Expand All @@ -188,8 +214,9 @@ def _get_alias_usage_link(self, obj, request, disabled=False):
)

def _get_change_alias_settings_link(self, obj, request, disabled=False):
url = admin_reverse('{}_{}_change'.format(
obj._meta.app_label, obj.alias._meta.model_name), args=(obj.alias.pk,)
url = admin_reverse(
"{}_{}_change".format(obj._meta.app_label, obj.alias._meta.model_name),
args=(obj.alias.pk,),
)
return render_to_string(
"admin/djangocms_alias/icons/change_alias_settings.html",
Expand All @@ -213,10 +240,13 @@ def _get_preview_link(self, obj, request, disabled=False):
{"url": preview_url, "disabled": disabled, "keepsideframe": False},
)

def change_view(self, request, object_id, form_url='', extra_context=None):
def change_view(self, request, object_id, form_url="", extra_context=None):
extra_context = extra_context or {}
# Provide additional context to the changeform
extra_context['is_versioning_enabled'] = is_versioning_enabled()
extra_context["is_versioning_enabled"] = is_versioning_enabled()
return super().change_view(
request, object_id, form_url, extra_context=extra_context,
request,
object_id,
form_url,
extra_context=extra_context,
)
4 changes: 2 additions & 2 deletions djangocms_alias/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@


class AliasConfig(AppConfig):
name = 'djangocms_alias'
verbose_name = _('django CMS Alias')
name = "djangocms_alias"
verbose_name = _("django CMS Alias")
16 changes: 9 additions & 7 deletions djangocms_alias/cms_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


try:
apps.get_app_config('djangocms_internalsearch')
apps.get_app_config("djangocms_internalsearch")
from .internal_search import AliasContentConfig
except (ImportError, LookupError):
AliasContentConfig = None
Expand All @@ -27,20 +27,21 @@ class AliasCMSConfig(CMSAppConfig):
moderated_models = [AliasContent]

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

if djangocms_versioning_enabled and djangocms_versioning_installed:

from cms.utils.i18n import get_language_tuple

from djangocms_versioning.datastructures import VersionableItem

versioning = [
VersionableItem(
content_model=AliasContent,
grouper_field_name='alias',
grouper_field_name="alias",
extra_grouping_fields=["language"],
version_list_filter_lookups={"language": get_language_tuple},
copy_function=copy_alias_content,
Expand All @@ -49,9 +50,10 @@ class AliasCMSConfig(CMSAppConfig):
]

djangocms_references_enabled = getattr(
settings, 'REFERENCES_ALIAS_MODELS_ENABLED', True)
settings, "REFERENCES_ALIAS_MODELS_ENABLED", True
)
reference_fields = [
(AliasPlugin, 'alias'),
(AliasPlugin, "alias"),
]

# Internalsearch configuration
Expand Down
5 changes: 2 additions & 3 deletions djangocms_alias/cms_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ 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
57 changes: 29 additions & 28 deletions djangocms_alias/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@


__all__ = [
'Alias',
"Alias",
]


@plugin_pool.register_plugin
class Alias(CMSPluginBase):
name = _('Alias')
name = _("Alias")
model = AliasPlugin
form = AliasPluginForm

Expand All @@ -35,8 +35,8 @@ def get_render_template(self, context, instance, placeholder):
isinstance(instance.placeholder.source, AliasContent)
and instance.is_recursive()
):
return 'djangocms_alias/alias_recursive.html'
return 'djangocms_alias/{}/alias.html'.format(instance.template)
return "djangocms_alias/alias_recursive.html"
return f"djangocms_alias/{instance.template}/alias.html"

@classmethod
def get_extra_plugin_menu_items(cls, request, plugin):
Expand All @@ -49,10 +49,10 @@ def get_extra_plugin_menu_items(cls, request, plugin):

plugin_menu_items = [
PluginMenuItem(
_('Edit Alias'),
_("Edit Alias"),
edit_endpoint,
action='sideframe',
attributes={'icon': 'alias'},
action="sideframe",
attributes={"icon": "alias"},
),
]

Expand All @@ -63,69 +63,69 @@ def get_extra_plugin_menu_items(cls, request, plugin):
):
plugin_menu_items.append(
PluginMenuItem(
_('Detach Alias'),
_("Detach Alias"),
detach_endpoint,
action='modal',
attributes={'icon': 'alias'},
action="modal",
attributes={"icon": "alias"},
)
)
return plugin_menu_items

data = {
'plugin': plugin.pk,
'language': get_language_from_request(request, check_path=True),
"plugin": plugin.pk,
"language": get_language_from_request(request, check_path=True),
}
endpoint = add_url_parameters(admin_reverse(CREATE_ALIAS_URL_NAME), **data)
return [
PluginMenuItem(
_('Create Alias'),
_("Create Alias"),
endpoint,
action='modal',
attributes={'icon': 'alias'},
action="modal",
attributes={"icon": "alias"},
),
]

@classmethod
def get_extra_placeholder_menu_items(cls, request, placeholder):
data = {
'placeholder': placeholder.pk,
'language': get_language_from_request(request, check_path=True),
"placeholder": placeholder.pk,
"language": get_language_from_request(request, check_path=True),
}
endpoint = add_url_parameters(admin_reverse(CREATE_ALIAS_URL_NAME), **data)

menu_items = [
PluginMenuItem(
_('Create Alias'),
_("Create Alias"),
endpoint,
action='modal',
attributes={'icon': 'alias'},
action="modal",
attributes={"icon": "alias"},
),
]
return menu_items

@classmethod
def can_create_alias(cls, user, plugins=None, replace=False):
if not user.has_perm(
get_model_permission_codename(AliasModel, 'add'),
get_model_permission_codename(AliasModel, "add"),
):
return False

if not plugins:
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(
has_plugin_permission(
user,
plugin.plugin_type,
'add',
) for plugin in plugins
"add",
)
for plugin in plugins
)

@classmethod
Expand All @@ -134,8 +134,9 @@ def can_detach(cls, user, target_placeholder, plugins):
has_plugin_permission(
user,
plugin.plugin_type,
'add',
) for plugin in plugins
"add",
)
for plugin in plugins
) and target_placeholder.check_source(user)

@classmethod
Expand Down
Loading

0 comments on commit 8b15ec1

Please sign in to comment.