From 1966c9ee012f9ec42f1934201cdb729c0d5f183c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Thu, 10 Oct 2024 10:36:15 +0200 Subject: [PATCH 1/2] Drop support for EOL Python 3.8 --- .github/workflows/test.yml | 1 - pyproject.toml | 3 +-- tox.ini | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6c71ffd6..046b73bb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,6 @@ jobs: matrix: # https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django python-version: - - '3.8' - '3.9' - '3.10' - '3.11' diff --git a/pyproject.toml b/pyproject.toml index 3b4a52b4..8bb75a0e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "django-auth-ldap" -requires-python = ">=3.8" +requires-python = ">=3.9" description = "Django LDAP authentication backend" readme = "README.rst" authors = [ @@ -23,7 +23,6 @@ classifiers = [ "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", diff --git a/tox.ini b/tox.ini index 87ffa51a..121f6e55 100644 --- a/tox.ini +++ b/tox.ini @@ -10,7 +10,6 @@ isolated_build = true [gh] python = - 3.8 = django42 3.9 = django42 3.10 = django{42,50,51,main} 3.11 = django{42,50,51,main} From 943c8002bc51532e99f482ed10301b3bd4938865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Thu, 10 Oct 2024 10:30:56 +0200 Subject: [PATCH 2/2] Allow importing the backend without loading apps Make it possible to monkey-patch the backend from the settings. --- django_auth_ldap/backend.py | 5 ++++- tests/import_test_without_django.py | 16 ++++++++++++++++ tox.ini | 4 +++- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/import_test_without_django.py diff --git a/django_auth_ldap/backend.py b/django_auth_ldap/backend.py index c63b6cec..4356d79c 100644 --- a/django_auth_ldap/backend.py +++ b/django_auth_ldap/backend.py @@ -51,7 +51,6 @@ import django.dispatch import ldap from django.contrib.auth import get_user_model -from django.contrib.auth.models import Group, Permission from django.core.cache import cache from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist @@ -777,6 +776,8 @@ def _mirror_groups(self): Mirrors the user's LDAP groups in the Django database and updates the user's membership. """ + from django.contrib.auth.models import Group + try: target_group_names = frozenset(self._get_groups().get_group_names()) except ldap.LDAPError as e: @@ -833,6 +834,8 @@ def _load_group_permissions(self): Populates self._group_permissions based on LDAP group membership and Django group permissions. """ + from django.contrib.auth.models import Permission + group_names = self._get_groups().get_group_names() perms = Permission.objects.filter(group__name__in=group_names) diff --git a/tests/import_test_without_django.py b/tests/import_test_without_django.py new file mode 100644 index 00000000..0fbdc9b4 --- /dev/null +++ b/tests/import_test_without_django.py @@ -0,0 +1,16 @@ +import os +from unittest import TestCase + + +class TestLoading(TestCase): + def test_django_not_ready(self): + orig_env = os.environ.copy() + + def reset_env(): + os.environ = orig_env + + self.addCleanup(reset_env) + + os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings" + + import django_auth_ldap.backend # noqa: F401 diff --git a/tox.ini b/tox.ini index 121f6e55..dd4f3f91 100644 --- a/tox.ini +++ b/tox.ini @@ -17,7 +17,9 @@ python = 3.13 = django{42,50,51,main} [testenv] -commands = {envpython} -Wa -b -m django test --settings tests.settings +commands = + {envpython} -Wa -b -m django test --settings tests.settings + {envpython} -Wa -b -m unittest discover --pattern *_test_without_django.py deps = django42: Django>=4.2,<4.3 django50: Django>=5.0,<5.1