From 3b5fcafd3f2bb293c71f93d1ccb4c51b164ee59c Mon Sep 17 00:00:00 2001 From: Sajeyks Date: Tue, 6 Oct 2026 13:10:20 +0300 Subject: [PATCH] Add compat shims for removed Django names used by unmaintained packages --- dynamicweb/compat.py | 57 +++++++++++++++++++++++++++++++++++++ dynamicweb/settings/base.py | 13 ++++----- 2 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 dynamicweb/compat.py diff --git a/dynamicweb/compat.py b/dynamicweb/compat.py new file mode 100644 index 00000000..5bc3d9be --- /dev/null +++ b/dynamicweb/compat.py @@ -0,0 +1,57 @@ +""" +Aliases for helpers that newer Django versions removed. + +The unmaintained aldryn-* packages (and similar) still import them. Imported +first thing from the settings, before any app code is loaded. +""" +import django.utils.encoding +import django.utils.translation + +if not hasattr(django.utils.encoding, 'python_2_unicode_compatible'): + django.utils.encoding.python_2_unicode_compatible = lambda cls: cls + +for _name in ('gettext', 'gettext_lazy', 'gettext_noop', + 'ngettext', 'ngettext_lazy'): + if not hasattr(django.utils.translation, 'u' + _name): + setattr(django.utils.translation, 'u' + _name, + getattr(django.utils.translation, _name)) + +import sys + +import six +from django.conf import urls +from django.db import models +from django.http import HttpRequest +from django.urls import re_path +from django.utils import encoding + +for _name, _target in (('force_text', 'force_str'), ('smart_text', 'smart_str'), + ('force_unicode', 'force_str')): + if not hasattr(encoding, _name): + setattr(encoding, _name, getattr(encoding, _target)) + +if not hasattr(urls, 'url'): + urls.url = re_path + +if 'django.utils.six' not in sys.modules: + import django.utils + django.utils.six = six + sys.modules['django.utils.six'] = six + sys.modules['django.utils.six.moves'] = six.moves + +if not hasattr(models, 'NullBooleanField'): + class NullBooleanField(models.BooleanField): + def __init__(self, *args, **kwargs): + kwargs['null'] = True + kwargs['blank'] = True + super().__init__(*args, **kwargs) + + def deconstruct(self): + name, path, args, kwargs = super().deconstruct() + del kwargs['null'], kwargs['blank'] + return name, path, args, kwargs + + models.NullBooleanField = NullBooleanField + +if not hasattr(HttpRequest, 'is_ajax'): + HttpRequest.is_ajax = lambda self: self.headers.get('x-requested-with') == 'XMLHttpRequest' diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 339846ef..9452e1f3 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -10,13 +10,9 @@ import os # dotenv import dotenv -import django.utils.encoding -from django.utils.translation import ugettext_lazy as _ -# The unmaintained aldryn-* packages still import this Django 2 helper -# (removed in Django 3.0); on Python 3 it is a no-op. -if not hasattr(django.utils.encoding, 'python_2_unicode_compatible'): - django.utils.encoding.python_2_unicode_compatible = lambda cls: cls +import dynamicweb.compat # noqa: F401, shims for packages Django 4 broke +from django.utils.translation import gettext_lazy as _ logger = logging.getLogger(__name__) @@ -527,8 +523,9 @@ META_INCLUDE_KEYWORDS = ["ungleich", "hosting", "switzerland", "Schweiz", "Swiss", "cdist"] META_USE_SITES = True -# django-multisite's SiteID is a lazy object, not an int -SILENCED_SYSTEM_CHECKS = ['sites.E101'] +# sites.E101: django-multisite's SiteID is a lazy object, not an int +# fields.E903: django-multisite still uses NullBooleanField +SILENCED_SYSTEM_CHECKS = ['sites.E101', 'fields.E903'] PARLER_LANGUAGES = {SITE_ID: ({'code': 'en-us'}, {'code': 'de'},)} AUTH_USER_MODEL = 'membership.CustomUser'