Skip to content

Commit

Permalink
✨ [#472] support commas in values for 'data_attrs' query param
Browse files Browse the repository at this point in the history
  • Loading branch information
annashamray committed Dec 9, 2024
1 parent efd240d commit 234daf0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/objects/api/v2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ class Meta:
form = ObjectRecordFilterForm

def filter_data_attrs(self, queryset, name, value: str):
# temporarily replace escaped comma
comma_replace = "*COMMA*"
value = value.replace("\\,", comma_replace)
parts = value.split(",")

for value_part in parts:
# replace to comma back after splitting
value_part = value_part.replace(comma_replace, ",")
variable, operator, str_value = value_part.rsplit("__", 2)
real_value = string_to_value(str_value)

Expand Down
7 changes: 7 additions & 0 deletions src/objects/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ def __call__(self, new_value, serializer_field):

def validate_data_attrs(value: str):
code = "invalid-data-attrs-query"

# temporarily replace escaped comma
comma_replace = "*COMMA*"
value = value.replace("\\,", comma_replace)

parts = value.split(",")

for value_part in parts:
# replace to comma back after splitting
value_part = value_part.replace(comma_replace, ",")
try:
variable, operator, val = value_part.rsplit("__", 2)
except ValueError as exc:
Expand Down
14 changes: 6 additions & 8 deletions src/objects/tests/v2/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,16 @@ def test_filter_icontains_string_with_comma(self):
"""
regression test for https://github.com/maykinmedia/objects-api/issues/472
"""
record = ObjectRecordFactory.create(
ObjectRecordFactory.create(
data={"name": "Something important"}, object__object_type=self.object_type
)
ObjectRecordFactory.create(
record = ObjectRecordFactory.create(
data={"name": "Advies, support en kennis om te weten"},
object__object_type=self.object_type,
)
ObjectRecordFactory.create(data={}, object__object_type=self.object_type)

response = self.client.get(
self.url, {"data_attrs": "name__icontains__Advies, support en kennis"}
self.url, {"data_attrs": "name__icontains__Advies\\, support en kennis"}
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -390,19 +389,18 @@ def test_filter_two_icontains_with_comma(self):
"""
regression test for https://github.com/maykinmedia/objects-api/issues/472
"""
record = ObjectRecordFactory.create(
ObjectRecordFactory.create(
data={"name": "Something important"}, object__object_type=self.object_type
)
ObjectRecordFactory.create(
record = ObjectRecordFactory.create(
data={"name": "Advies, support en kennis om te weten"},
object__object_type=self.object_type,
)
ObjectRecordFactory.create(data={}, object__object_type=self.object_type)

response = self.client.get(
self.url,
{
"data_attrs": "name__icontains__Advies, support en kennis,name__icontains__om"
"data_attrs": "name__icontains__Advies\\, support en kennis,name__icontains__om"
},
)

Expand Down

0 comments on commit 234daf0

Please sign in to comment.