Skip to content

Commit

Permalink
Feat: Django 4.2 and Python 3.10 compatibility (django-cms#230)
Browse files Browse the repository at this point in the history
* feat: django 4.2, CMS 4.0.1 and Python 3.10 compatibility

* fix: add missing `.pre-commit-config.yaml`

* ci: auto fixes from pre-commit hooks

for more information, see https://pre-commit.ci

* fix: update code according to pyupgrade

* fix flake8 error

* ci: auto fixes from pre-commit hooks

for more information, see https://pre-commit.ci

* fix: update config files

* ci: auto fixes from pre-commit hooks

for more information, see https://pre-commit.ci

* update

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
joshyu and pre-commit-ci[bot] authored Mar 11, 2024
1 parent 5c174fd commit 5bae87b
Show file tree
Hide file tree
Showing 47 changed files with 1,855 additions and 1,745 deletions.
43 changes: 11 additions & 32 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
name: Lint
name: Ruff

on: [push, pull_request]
on:
push:
pull_request:

jobs:
flake8:
name: flake8
ruff:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install flake8
run: pip install --upgrade flake8
- name: Run flake8
uses: liskin/gh-problem-matcher-wrap@v1
with:
linters: flake8
run: flake8

isort:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
- run: python -m pip install isort
- name: isort
uses: liskin/gh-problem-matcher-wrap@v1
with:
linters: isort
run: isort --check-only --diff ./
- uses: actions/checkout@v4

- run: python -Im pip install --user ruff

- name: Run ruff
run: ruff --output-format=github djangocms_alias
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ 3.7, 3.8, 3.9 ]
python-version: [ 3.8, 3.9, '3.10' ]
requirements-file: [
django_22.txt,
django_32.txt,
django_42.txt,
]
os: [
ubuntu-20.04,
Expand Down
34 changes: 34 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ci:
autofix_commit_msg: |
ci: auto fixes from pre-commit hooks
for more information, see https://pre-commit.ci
autofix_prs: true
autoupdate_commit_msg: 'ci: pre-commit autoupdate'
autoupdate_schedule: monthly

repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: ["--py38-plus"]

- repo: https://github.com/adamchainz/django-upgrade
rev: '1.15.0'
hooks:
- id: django-upgrade
args: [--target-version, "4.2"]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-yaml
- id: trailing-whitespace
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog

Unreleased
==========
* Introduced Django 4.2 support.
* Dropped Support for Django<3.1


1.11.0 (2022-10-14)
===================
Expand Down
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"
86 changes: 55 additions & 31 deletions djangocms_alias/admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from cms.utils.permissions import get_model_permission_codename
from cms.utils.urlutils import admin_reverse
from django.contrib import admin
from django.db.models.functions import Lower
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _

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 @@ -20,30 +18,45 @@
is_versioning_enabled,
)


__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 +72,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 +89,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 @@ -122,12 +140,13 @@ def get_queryset(self, request):
return queryset

# Add Alias category in the admin manager list and order field
@admin.display(
description=_("category"),
ordering="alias_category_translations_ordered",
)
def get_category(self, obj):
return obj.alias.category

get_category.short_description = _('category')
get_category.admin_order_field = "alias_category_translations_ordered"

def has_add_permission(self, request, obj=None):
# FIXME: It is not currently possible to add an alias from the django admin changelist issue #97
# https://github.com/django-cms/djangocms-alias/issues/97
Expand Down Expand Up @@ -171,8 +190,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(
f"{obj._meta.app_label}_{obj._meta.model_name}_change",
args=(obj.pk,),
)
return render_to_string(
"admin/djangocms_alias/icons/rename_alias.html",
Expand All @@ -187,8 +207,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(
f"{obj._meta.app_label}_{obj.alias._meta.model_name}_change",
args=(obj.alias.pk,),
)
return render_to_string(
"admin/djangocms_alias/icons/change_alias_settings.html",
Expand All @@ -212,10 +233,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")
21 changes: 7 additions & 14 deletions djangocms_alias/cms_config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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')
apps.get_app_config("djangocms_internalsearch")
from .internal_search import AliasContentConfig
except (ImportError, LookupError):
AliasContentConfig = None
Expand All @@ -26,32 +24,27 @@ 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_versioning_enabled = getattr(
settings, 'VERSIONING_ALIAS_MODELS_ENABLED', True)
djangocms_moderation_enabled = getattr(settings, "MODERATING_ALIAS_MODELS_ENABLED", True)
djangocms_versioning_enabled = getattr(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,
grouper_selector_option_label=lambda obj, lang: obj.get_name(lang),
),
]

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

# Internalsearch configuration
Expand Down
5 changes: 1 addition & 4 deletions djangocms_alias/cms_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +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
Loading

0 comments on commit 5bae87b

Please sign in to comment.