From 180bc1e5f44c64354bff380dac694d890ded2054 Mon Sep 17 00:00:00 2001 From: Vitaly Bogomolov Date: Tue, 20 Feb 2024 21:16:04 +0400 Subject: [PATCH] Django5 support (#16) --- .github/workflows/django4.yml | 2 +- .github/workflows/django5.yml | 49 ++++++++++++++++++++++++++++ README.md | 4 +-- django_admin_filters/base.py | 13 +++++++- django_admin_filters/daterange.py | 3 ++ django_admin_filters/multi_choice.py | 6 +++- makefile | 2 +- pytest5.ini | 3 ++ tests/django3.txt | 2 +- tests/django4.txt | 2 +- tests/django5.txt | 1 + tests/test/test_multi_choice.py | 1 + 12 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/django5.yml create mode 100644 pytest5.ini create mode 100644 tests/django5.txt diff --git a/.github/workflows/django4.yml b/.github/workflows/django4.yml index 5f5f92c..5fb2e93 100644 --- a/.github/workflows/django4.yml +++ b/.github/workflows/django4.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.8', '3.9', '3.10', '3.11'] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/django5.yml b/.github/workflows/django5.yml new file mode 100644 index 0000000..f557483 --- /dev/null +++ b/.github/workflows/django5.yml @@ -0,0 +1,49 @@ +# https://docs.github.com/en/free-pro-team@latest/actions/guides/building-and-testing-python +name: django5 + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + + django5: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ['3.10', '3.11', '3.12'] + + steps: + - uses: actions/checkout@v3 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r tests/requirements.txt + pip install -r tests/django5.txt + + - name: flake8 + run: | + flake8 --count --show-source --statistics --max-line-length=120 django_admin_filters + flake8 --count --show-source --statistics --max-line-length=120 tests/test + + - name: pylint + run: | + python -m pylint django_admin_filters + python -m pylint tests/test + + - name: pytest + run: | + python manage.py collectstatic --noinput --settings example.settings + python manage.py makemigrations --settings example.settings example + python manage.py migrate --settings example.settings + pytest -c pytest5.ini --cov=django_admin_filters --cov-report xml --cov-report term:skip-covered --durations=5 tests diff --git a/README.md b/README.md index 46f3da5..2f04b86 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # DjangoAdminFilters library [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/django.admin.filters/pep257.yml?label=Pep257&style=plastic&branch=main)](https://github.com/vb64/django.admin.filters/actions?query=workflow%3Apep257) -[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/django.admin.filters/django3.yml?label=Django%203.2.20%20Python%203.7-3.10&style=plastic&branch=main)](https://github.com/vb64/django.admin.filters/actions?query=workflow%3Adjango3) -[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/django.admin.filters/django4.yml?label=Django%204.2.3%20Python%203.8-3.11&style=plastic&branch=main)](https://github.com/vb64/django.admin.filters/actions?query=workflow%3Adjango4) +[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/django.admin.filters/django3.yml?label=Django%203.2.23%20Python%203.7-3.10&style=plastic&branch=main)](https://github.com/vb64/django.admin.filters/actions?query=workflow%3Adjango3) +[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/django.admin.filters/django4.yml?label=Django%204.2.8%20Python%203.8-3.11&style=plastic&branch=main)](https://github.com/vb64/django.admin.filters/actions?query=workflow%3Adjango4) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/926ec3c1141f4230b4d0508497e5561f)](https://app.codacy.com/gh/vb64/django.admin.filters/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade) [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/926ec3c1141f4230b4d0508497e5561f)](https://app.codacy.com/gh/vb64/django.admin.filters/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage) [![PyPI - Downloads](https://img.shields.io/pypi/dm/django-admin-list-filters?label=pypi%20installs)](https://pypistats.org/packages/django-admin-list-filters) diff --git a/django_admin_filters/base.py b/django_admin_filters/base.py index 4ea98ef..2d352e3 100644 --- a/django_admin_filters/base.py +++ b/django_admin_filters/base.py @@ -31,12 +31,23 @@ def __init__(self, field, request, params, model, model_admin, field_path): super().__init__(field, request, params, model, model_admin, field_path) self.set_title() + def get_facet_counts(self, _pk_attname, _filtered_qs): + """Django5 amin site Facets. + + https://docs.djangoproject.com/en/5.0/ref/contrib/admin/filters/#facet-filters + """ + return {} + def value(self): """Return the string provided in the request's query string. None if the value wasn't provided. """ - return self.used_parameters.get(self.parameter_name) + val = self.used_parameters.get(self.parameter_name) + if isinstance(val, list): + val = val[0] + + return val def expected_parameters(self): """Parameter list for chice filter.""" diff --git a/django_admin_filters/daterange.py b/django_admin_filters/daterange.py index 9d8ee74..bfa129f 100644 --- a/django_admin_filters/daterange.py +++ b/django_admin_filters/daterange.py @@ -65,6 +65,9 @@ def __init__(self, field, request, params, model, model_admin, field_path): @staticmethod def to_dtime(text): """Convert string to datetime.""" + if isinstance(text, list): + text = text[0] + try: return datetime.fromisoformat(text) except ValueError: diff --git a/django_admin_filters/multi_choice.py b/django_admin_filters/multi_choice.py index 8fef071..a514530 100644 --- a/django_admin_filters/multi_choice.py +++ b/django_admin_filters/multi_choice.py @@ -18,7 +18,11 @@ def set_selected(self, val, title): title.update({ 'choices_separator': self.CHOICES_SEPARATOR, }) - self.selected = val.split(self.CHOICES_SEPARATOR) if val else [] + + if isinstance(val, str): + val = val.split(self.CHOICES_SEPARATOR) + + self.selected = val or [] def choices(self, _changelist): """Define filter checkboxes.""" diff --git a/makefile b/makefile index 9aa05c7..3f1f1d5 100644 --- a/makefile +++ b/makefile @@ -10,7 +10,7 @@ PTEST = ./venv/bin/pytest COVERAGE = ./venv/bin/coverage endif -DJANGO_VER = 4 +DJANGO_VER = 5 SOURCE = django_admin_filters TESTS = tests diff --git a/pytest5.ini b/pytest5.ini new file mode 100644 index 0000000..cc33aae --- /dev/null +++ b/pytest5.ini @@ -0,0 +1,3 @@ +[pytest] +filterwarnings = + ignore::django.utils.deprecation.RemovedInDjango51Warning diff --git a/tests/django3.txt b/tests/django3.txt index 2d0739f..db84d23 100644 --- a/tests/django3.txt +++ b/tests/django3.txt @@ -1 +1 @@ -Django==3.2.20 +Django==3.2.23 diff --git a/tests/django4.txt b/tests/django4.txt index b1ba9b3..07755e1 100644 --- a/tests/django4.txt +++ b/tests/django4.txt @@ -1 +1 @@ -Django==4.2.3 +Django==4.2.8 diff --git a/tests/django5.txt b/tests/django5.txt new file mode 100644 index 0000000..90208ee --- /dev/null +++ b/tests/django5.txt @@ -0,0 +1 @@ +Django==5.0 diff --git a/tests/test/test_multi_choice.py b/tests/test/test_multi_choice.py index 5fd1710..4933d16 100644 --- a/tests/test/test_multi_choice.py +++ b/tests/test/test_multi_choice.py @@ -28,6 +28,7 @@ def test_queryset(self): flt_choice = changelist.get_filters(request)[0][0] assert flt_choice.queryset(request, self.queryset) is not None + assert not flt_choice.get_facet_counts(None, None) def test_queryset_ext(self): """Filter queryset with MultiChoiceExt."""