Skip to content

Commit

Permalink
🔥 [#3283] Remove OR-filter behaviour on form definitions endpoint
Browse files Browse the repository at this point in the history
Providing both the used_in and is_reusable filter parameters no longer
returns records that match either condition, instead it applies the
default AND behaviour.
  • Loading branch information
sergei-maertens committed Dec 12, 2024
1 parent 3a70208 commit aa7abce
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 71 deletions.
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

0 comments on commit aa7abce

Please sign in to comment.