Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💥 Remove OR-filter behaviour on form definitions endpoint #4913

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions src/openforms/forms/api/filters.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import warnings

from django.db.models import Q
from django.utils.translation import gettext_lazy as _

from django_filters import rest_framework as filters
from django_filters.constants import EMPTY_VALUES
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema_field

Expand All @@ -31,36 +27,3 @@ class FormDefinitionFilter(filters.FilterSet):
class Meta:
model = FormDefinition
fields = ("is_reusable",)
or_fields = ("is_reusable", "used_in")

def filter_queryset(self, queryset):
"""
Override base implementation to add OR-behaviour instead of the standard AND.

See https://github.com/carltongibson/django-filter/discussions/1426
"""
_or = Q()

for name in self.Meta.or_fields:
# this essentially re-implements django_filters.filters.Filter.filter,
# as otherwise we would have to introspect private django query/queryset API
# *shudders*
or_filter_value = self.form.cleaned_data.pop(name)
if or_filter_value in EMPTY_VALUES:
continue
or_filter = self.filters[name]
if or_filter.distinct:
queryset = queryset.distinct()
lookup = "%s__%s" % (or_filter.field_name, or_filter.lookup_expr)
q_expr = Q(**{lookup: or_filter_value})
if or_filter.exclude:
q_expr = ~q_expr
_or |= q_expr

if _or:
warnings.warn(
"Using OR filters is deprecated and will be removed starting with Open Forms 3.0",
DeprecationWarning,
)
queryset = queryset.filter(_or)
return super().filter_queryset(queryset)
34 changes: 0 additions & 34 deletions src/openforms/forms/tests/test_api_formdefinition.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,40 +483,6 @@ def test_filter_by_used_in(self):
fd["uuid"], str(form_1.formstep_set.get().form_definition.uuid)
)

def test_filter_by_used_in_or_reusable(self):
user = StaffUserFactory.create(user_permissions=["change_form"])
self.client.force_authenticate(user=user)
FormDefinitionFactory.create(is_reusable=False, slug="fd-0")
fd2 = FormDefinitionFactory.create(is_reusable=True, slug="fd-1")
FormFactory.create(
generate_minimal_setup=True,
formstep__form_definition__slug="fd-2",
formstep__form_definition__is_reusable=False,
)
form_2 = FormFactory.create(
generate_minimal_setup=True,
formstep__form_definition__slug="fd-3",
formstep__form_definition__is_reusable=True,
)
url = reverse("api:formdefinition-list")

response = self.client.get(
url,
{
"used_in": form_2.uuid,
"is_reusable": "true",
},
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = response.json()
self.assertEqual(response_data["count"], 2)
fd_2, fd_4 = response_data["results"]
self.assertEqual(fd_2["uuid"], str(fd2.uuid))
self.assertEqual(
fd_4["uuid"], str(form_2.formstep_set.get().form_definition.uuid)
)

@override_settings(LANGUAGE_CODE="en")
def test_duplicate_components(self):
user = StaffUserFactory.create(user_permissions=["change_form"])
Expand Down
Loading