From 47e4b0d4385bdbb8e985548a3365b0f41f719c84 Mon Sep 17 00:00:00 2001 From: ronardcaktus Date: Tue, 18 Jul 2023 10:30:33 -0400 Subject: [PATCH 1/6] Add support for Django 4.x --- .github/workflows/test.yml | 2 +- .gitignore | 1 + .pre-commit-config.yaml | 10 +++++----- README.rst | 4 ++-- bread/bread.py | 20 ++++++++++++++++---- docs/conf.py | 2 +- setup.py | 5 ++++- tox.ini | 7 ++++--- 8 files changed, 34 insertions(+), 17 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4872280..3247a8d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,7 +20,7 @@ jobs: matrix: # tox-gh-actions will only run the tox environments which match the currently # running python-version. See [gh-actions] in tox.ini for the mapping - python-version: [3.7, 3.8, 3.9] + python-version: [3.8, 3.9, 3.10] steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} diff --git a/.gitignore b/.gitignore index 893bed7..ecb5794 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ _build .coverage .envrc .direnv +.vscode/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5466e41..0c461f6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.2.0 + rev: v4.4.0 hooks: - id: check-added-large-files - id: check-case-conflict @@ -15,16 +15,16 @@ repos: - id: trailing-whitespace - repo: https://github.com/psf/black - rev: 22.3.0 + rev: 23.7.0 hooks: - id: black - repo: https://github.com/pycqa/isort - rev: 5.10.1 + rev: 5.12.0 hooks: - id: isort - - repo: https://gitlab.com/pycqa/flake8 - rev: 3.9.2 + - repo: https://github.com/PyCQA/flake8 + rev: 6.0.0 hooks: - id: flake8 diff --git a/README.rst b/README.rst index 991fb9a..84485a3 100644 --- a/README.rst +++ b/README.rst @@ -28,8 +28,8 @@ Django 2.0 has been out of support since April 1, 2019.) Supported versions ------------------ -Django: 2.2, 3.2 -Python: 3.7, 3.8, 3.9, 3.10 +Django: 3.2, 4.2 +Python: 3.8, 3.9, 3.10 For Python 2.7 and/or Django 1.11 support, the 0.5 release series is identical (features-wise) to 0.6 and is available on PyPI: https://pypi.org/project/django-bread/#history diff --git a/bread/bread.py b/bread/bread.py index ba9451b..1ced5e3 100644 --- a/bread/bread.py +++ b/bread/bread.py @@ -4,7 +4,6 @@ from urllib.parse import urlencode from django.conf import settings -from django.contrib.admin.utils import lookup_needs_distinct from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth.models import Permission from django.contrib.auth.views import redirect_to_login @@ -339,6 +338,7 @@ def get_search_results(self, request, queryset, search_term): Returns a tuple containing a queryset to implement the search, and a boolean indicating if the results may contain duplicates. """ + # Apply keyword searches. def construct_search(field_name): if field_name.startswith("^"): @@ -362,9 +362,21 @@ def construct_search(field_name): if not use_distinct: opts = self.bread.model._meta for search_spec in orm_lookups: - if lookup_needs_distinct(opts, search_spec): - use_distinct = True - break + # The function lookup_needs_distinct() was renamed + # to lookup_spawns_duplicates() in Django 4.0 + # https://docs.djangoproject.com/en/4.2/releases/4.0/#:~:text=The%20undocumented%20django.contrib.admin.utils.lookup_needs_distinct()%20function%20is%20renamed%20to%20lookup_spawns_duplicates(). + try: + from django.contrib.admin.utils import lookup_needs_distinct + + if lookup_needs_distinct(opts, search_spec): + use_distinct = True + break + except ImportError: + from django.contrib.admin.utils import lookup_spawns_duplicates + + if lookup_spawns_duplicates(opts, search_spec): + use_distinct = True + break return queryset, use_distinct diff --git a/docs/conf.py b/docs/conf.py index da9535c..7abd169 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -48,7 +48,7 @@ # General information about the project. project = "Django Bread" -copyright = "2015, Caktus Consulting, LLC" +copyright = "2015-2023, Caktus Consulting, LLC" author = "Caktus Consulting, LLC" # The version info for the project you're documenting, acts as replacement for diff --git a/setup.py b/setup.py index 58b577d..f2fe7dc 100644 --- a/setup.py +++ b/setup.py @@ -28,9 +28,12 @@ "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", "Topic :: Software Development :: Libraries :: Python Modules", - "Framework :: Django :: 2.2", "Framework :: Django :: 3.2", + "Framework :: Django :: 4.0", + "Framework :: Django :: 4.1", + "Framework :: Django :: 4.2", ], zip_safe=False, # because we're including media that Django needs ) diff --git a/tox.ini b/tox.ini index a455b2c..4d442ec 100644 --- a/tox.ini +++ b/tox.ini @@ -1,9 +1,8 @@ [tox] -envlist = {py37,py38,py39}-django{22,32},py310-django32 +envlist = {py38,py39,py310}-django{3.2,4.0,4.1,4.2},py310-django42 [gh-actions] python = - 3.7: py37 3.8: py38 3.9: py39 3.10: py310 @@ -11,6 +10,8 @@ python = [testenv] deps = factory_boy==2.3.1 - django22: Django>=2.2,<3.0 django32: Django>=3.2,<4.0 + django40: Django>=4.0 + django42: Django>=4.1 + django42: Django>=4.2 commands = python -Wmodule runtests.py From 3017bded62988de8eedc923db31c1ec238be10d8 Mon Sep 17 00:00:00 2001 From: ronardcaktus Date: Tue, 18 Jul 2023 11:25:22 -0400 Subject: [PATCH 2/6] Upgrade factory-boy --- dev-requirements.txt | 2 +- tests/factories.py | 24 +++++++++++++----------- tox.ini | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index a5957bc..20c4ac8 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,7 +1,7 @@ . # <- install ourselves coverage django>=3.2,<4.0 -factory_boy==2.3.1 +factory_boy==3.2.1 flake8 pre-commit sphinx diff --git a/tests/factories.py b/tests/factories.py index 7139562..135cc8a 100644 --- a/tests/factories.py +++ b/tests/factories.py @@ -1,24 +1,26 @@ import factory -import factory.fuzzy from .models import BreadLabelValueTestModel, BreadTestModel, BreadTestModel2 -class BreadTestModel2Factory(factory.DjangoModelFactory): - FACTORY_FOR = BreadTestModel2 +class BreadTestModel2Factory(factory.django.DjangoModelFactory): + class Meta: + model = BreadTestModel2 - text = factory.fuzzy.FuzzyText(length=10) + text = factory.Faker("text", max_nb_chars=10) -class BreadTestModelFactory(factory.DjangoModelFactory): - FACTORY_FOR = BreadTestModel +class BreadTestModelFactory(factory.django.DjangoModelFactory): + class Meta: + model = BreadTestModel - name = factory.fuzzy.FuzzyText(length=10) - age = factory.fuzzy.FuzzyInteger(low=1, high=99) + name = factory.Faker("name") + age = factory.Faker("pyint", min_value=0, max_value=99) other = factory.SubFactory(BreadTestModel2Factory) -class BreadLabelValueTestModelFactory(factory.DjangoModelFactory): - FACTORY_FOR = BreadLabelValueTestModel +class BreadLabelValueTestModelFactory(factory.django.DjangoModelFactory): + class Meta: + model = BreadLabelValueTestModel - name = factory.fuzzy.FuzzyText(length=10) + name = factory.Faker("name") diff --git a/tox.ini b/tox.ini index 4d442ec..0297103 100644 --- a/tox.ini +++ b/tox.ini @@ -9,7 +9,7 @@ python = [testenv] deps = - factory_boy==2.3.1 + factory_boy==3.2.1 django32: Django>=3.2,<4.0 django40: Django>=4.0 django42: Django>=4.1 From 478e60c9e98f0c60e0038754cae92cd4713e2210 Mon Sep 17 00:00:00 2001 From: ronardcaktus Date: Tue, 18 Jul 2023 11:58:21 -0400 Subject: [PATCH 3/6] Address comments --- bread/bread.py | 8 ++++---- tox.ini | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bread/bread.py b/bread/bread.py index 1ced5e3..64b5e31 100644 --- a/bread/bread.py +++ b/bread/bread.py @@ -366,15 +366,15 @@ def construct_search(field_name): # to lookup_spawns_duplicates() in Django 4.0 # https://docs.djangoproject.com/en/4.2/releases/4.0/#:~:text=The%20undocumented%20django.contrib.admin.utils.lookup_needs_distinct()%20function%20is%20renamed%20to%20lookup_spawns_duplicates(). try: - from django.contrib.admin.utils import lookup_needs_distinct + from django.contrib.admin.utils import lookup_spawns_duplicates - if lookup_needs_distinct(opts, search_spec): + if lookup_spawns_duplicates(opts, search_spec): use_distinct = True break except ImportError: - from django.contrib.admin.utils import lookup_spawns_duplicates + from django.contrib.admin.utils import lookup_needs_distinct - if lookup_spawns_duplicates(opts, search_spec): + if lookup_needs_distinct(opts, search_spec): use_distinct = True break diff --git a/tox.ini b/tox.ini index 0297103..89c9332 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = {py38,py39,py310}-django{3.2,4.0,4.1,4.2},py310-django42 +envlist = {py38,py39,py310}-django{32,40,41,42} [gh-actions] python = @@ -11,7 +11,7 @@ python = deps = factory_boy==3.2.1 django32: Django>=3.2,<4.0 - django40: Django>=4.0 - django42: Django>=4.1 - django42: Django>=4.2 + django40: Django>=4.0,<4.1 + django41: Django>=4.1,<4.2 + django42: Django>=4.2,<5.0 commands = python -Wmodule runtests.py From 6e22cd3ab86ecbddad656c2808732f03a624e016 Mon Sep 17 00:00:00 2001 From: ronardcaktus Date: Tue, 18 Jul 2023 12:15:53 -0400 Subject: [PATCH 4/6] Install django-upgrade hook and run it --- .pre-commit-config.yaml | 6 ++++++ bread/bread.py | 4 ++-- bread/migrations/0001_initial.py | 1 - bread/migrations/0002_delete_breadtestmodel.py | 1 - 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0c461f6..ea10675 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,3 +28,9 @@ repos: rev: 6.0.0 hooks: - id: flake8 + + - repo: https://github.com/adamchainz/django-upgrade + rev: "1.14.0" + hooks: + - id: django-upgrade + args: [--target-version, "4.2"] diff --git a/bread/bread.py b/bread/bread.py index 64b5e31..86c79cb 100644 --- a/bread/bread.py +++ b/bread/bread.py @@ -372,9 +372,9 @@ def construct_search(field_name): use_distinct = True break except ImportError: - from django.contrib.admin.utils import lookup_needs_distinct + from django.contrib.admin.utils import lookup_spawns_duplicates - if lookup_needs_distinct(opts, search_spec): + if lookup_spawns_duplicates(opts, search_spec): use_distinct = True break diff --git a/bread/migrations/0001_initial.py b/bread/migrations/0001_initial.py index 9f5d50b..0bb3ef2 100644 --- a/bread/migrations/0001_initial.py +++ b/bread/migrations/0001_initial.py @@ -3,7 +3,6 @@ class Migration(migrations.Migration): - dependencies = [] operations = [ diff --git a/bread/migrations/0002_delete_breadtestmodel.py b/bread/migrations/0002_delete_breadtestmodel.py index 32c091c..c86549b 100644 --- a/bread/migrations/0002_delete_breadtestmodel.py +++ b/bread/migrations/0002_delete_breadtestmodel.py @@ -3,7 +3,6 @@ class Migration(migrations.Migration): - dependencies = [ ("bread", "0001_initial"), ] From ce6291c1554bf87f3578fd000bb794c8c75a511d Mon Sep 17 00:00:00 2001 From: ronardcaktus Date: Tue, 18 Jul 2023 12:45:59 -0400 Subject: [PATCH 5/6] Revert django-upgrade changes --- .pre-commit-config.yaml | 6 ------ bread/bread.py | 4 ++-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ea10675..0c461f6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,9 +28,3 @@ repos: rev: 6.0.0 hooks: - id: flake8 - - - repo: https://github.com/adamchainz/django-upgrade - rev: "1.14.0" - hooks: - - id: django-upgrade - args: [--target-version, "4.2"] diff --git a/bread/bread.py b/bread/bread.py index 86c79cb..64b5e31 100644 --- a/bread/bread.py +++ b/bread/bread.py @@ -372,9 +372,9 @@ def construct_search(field_name): use_distinct = True break except ImportError: - from django.contrib.admin.utils import lookup_spawns_duplicates + from django.contrib.admin.utils import lookup_needs_distinct - if lookup_spawns_duplicates(opts, search_spec): + if lookup_needs_distinct(opts, search_spec): use_distinct = True break From 5d8011ec7407fb73373b19c5afa6c3d5516c2575 Mon Sep 17 00:00:00 2001 From: ronardcaktus Date: Wed, 26 Jul 2023 13:43:25 -0400 Subject: [PATCH 6/6] Remove Django 4.0 reference --- setup.py | 1 - tox.ini | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/setup.py b/setup.py index f2fe7dc..cab707a 100644 --- a/setup.py +++ b/setup.py @@ -31,7 +31,6 @@ "Programming Language :: Python :: 3.10", "Topic :: Software Development :: Libraries :: Python Modules", "Framework :: Django :: 3.2", - "Framework :: Django :: 4.0", "Framework :: Django :: 4.1", "Framework :: Django :: 4.2", ], diff --git a/tox.ini b/tox.ini index 89c9332..2ecb7d4 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = {py38,py39,py310}-django{32,40,41,42} +envlist = {py38,py39,py310}-django{32,41,42} [gh-actions] python = @@ -11,7 +11,6 @@ python = deps = factory_boy==3.2.1 django32: Django>=3.2,<4.0 - django40: Django>=4.0,<4.1 django41: Django>=4.1,<4.2 django42: Django>=4.2,<5.0 commands = python -Wmodule runtests.py