diff options
-rw-r--r-- | patchwork/api/filters.py | 18 | ||||
-rw-r--r-- | patchwork/compat.py | 44 | ||||
-rw-r--r-- | patchwork/settings/base.py | 2 |
3 files changed, 16 insertions, 48 deletions
diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py index f7b6a6f..deb5ace 100644 --- a/patchwork/api/filters.py +++ b/patchwork/api/filters.py @@ -6,15 +6,16 @@ from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db.models import Q +from django_filters import rest_framework from django_filters.rest_framework import FilterSet from django_filters import CharFilter from django_filters import IsoDateTimeFilter from django_filters import ModelMultipleChoiceFilter from django.forms import ModelMultipleChoiceField as BaseMultipleChoiceField from django.forms.widgets import MultipleHiddenInput +from rest_framework import exceptions from patchwork.api import utils -from patchwork.compat import NAME_FIELD from patchwork.models import Bundle from patchwork.models import Check from patchwork.models import CoverLetter @@ -26,6 +27,17 @@ from patchwork.models import Series from patchwork.models import State +# custom backend + +class DjangoFilterBackend(rest_framework.DjangoFilterBackend): + + def filter_queryset(self, request, queryset, view): + try: + return super().filter_queryset(request, queryset, view) + except exceptions.ValidationError: + return queryset.none() + + # custom fields, filters class ModelMultipleChoiceField(BaseMultipleChoiceField): @@ -158,8 +170,8 @@ class BaseFilterSet(FilterSet): class TimestampMixin(BaseFilterSet): # TODO(stephenfin): These should filter on a 'updated_at' field instead - before = IsoDateTimeFilter(lookup_expr='lt', **{NAME_FIELD: 'date'}) - since = IsoDateTimeFilter(lookup_expr='gte', **{NAME_FIELD: 'date'}) + before = IsoDateTimeFilter(lookup_expr='lt', field_name='date') + since = IsoDateTimeFilter(lookup_expr='gte', field_name='date') class SeriesFilterSet(TimestampMixin, BaseFilterSet): diff --git a/patchwork/compat.py b/patchwork/compat.py deleted file mode 100644 index 8d94960..0000000 --- a/patchwork/compat.py +++ /dev/null @@ -1,44 +0,0 @@ -# Patchwork - automated patch tracking system -# Copyright (C) 2016 Intel Corporation -# -# SPDX-License-Identifier: GPL-2.0-or-later - -"""Compatibility wrappers for various library versions.""" - -from django.conf import settings - - -# NAME_FIELD -# -# The django-filter library renamed 'Filter.name' to 'Filter.field_name' in -# 1.1. -# -# DjangoFilterBackend - -# The django-filter library changed the default strictness level in 2.0 -# -# https://django-filter.readthedocs.io/en/master/guide/migration.html#migrating-to-2-0 - -if settings.ENABLE_REST_API: - import django_filters # noqa - from django_filters import rest_framework # noqa - from rest_framework import exceptions # noqa - - if django_filters.VERSION >= (1, 1): - NAME_FIELD = 'field_name' - else: - NAME_FIELD = 'name' - - if django_filters.VERSION >= (2, 0): - # TODO(stephenfin): Enable strict mode in API v2.0, possibly with a - # bump in the minimum version of django-filter [1] - # - # [1] https://github.com/carltongibson/django-filter/pull/983 - class DjangoFilterBackend(rest_framework.DjangoFilterBackend): - def filter_queryset(self, request, queryset, view): - try: - return super().filter_queryset(request, queryset, view) - except exceptions.ValidationError: - return queryset.none() - else: - DjangoFilterBackend = rest_framework.DjangoFilterBackend diff --git a/patchwork/settings/base.py b/patchwork/settings/base.py index 5160b4f..001878a 100644 --- a/patchwork/settings/base.py +++ b/patchwork/settings/base.py @@ -125,7 +125,7 @@ REST_FRAMEWORK = { 'rest_framework.versioning.URLPathVersioning', 'DEFAULT_PAGINATION_CLASS': 'patchwork.api.base.LinkHeaderPagination', 'DEFAULT_FILTER_BACKENDS': ( - 'patchwork.compat.DjangoFilterBackend', + 'patchwork.api.filters.DjangoFilterBackend', 'rest_framework.filters.SearchFilter', 'rest_framework.filters.OrderingFilter', ), |