Skip to content

Commit 030b012

Browse files
annashamraySonnyBA
authored andcommitted
👌 [#472] process PR feedback
1 parent 327735c commit 030b012

File tree

5 files changed

+81
-41
lines changed

5 files changed

+81
-41
lines changed

src/objects/api/v2/filters.py

+44-35
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,47 @@
2727
2828
"""
2929

30+
DATA_ATTRS_HELP_TEXT = (
31+
_(
32+
"""**DEPRECATED: Use 'data_attr' instead**.
33+
Only include objects that have attributes with certain values.
34+
Data filtering expressions are comma-separated and are structured as follows:
35+
36+
%(value_part_help_text)s
37+
38+
Example: in order to display only objects with `height` equal to 100, query `data_attrs=height__exact__100`
39+
should be used. If `height` is nested inside `dimensions` attribute, query should look like
40+
`data_attrs=dimensions__height__exact__100`
41+
42+
`value` may not contain comma, since commas are used as separator between filtering expressions.
43+
If you want to use commas in `value` you can use `data_attr` query parameter.
44+
"""
45+
)
46+
% {"value_part_help_text": DATA_ATTR_VALUE_HELP_TEXT}
47+
)
48+
49+
DATA_ATTR_HELP_TEXT = (
50+
_(
51+
"""Only include objects that have attributes with certain values.
52+
53+
%(value_part_help_text)s
54+
55+
Example: in order to display only objects with `height` equal to 100, query `data_attr=height__exact__100`
56+
should be used. If `height` is nested inside `dimensions` attribute, query should look like
57+
`data_attr=dimensions__height__exact__100`
58+
59+
This filter is very similar to the old `data_attrs` filter, but it has two differences:
60+
61+
* `value` may contain commas
62+
* only one filtering expression is allowed
63+
64+
If you want to use several filtering expressions, just use this `data_attr` several times in the query string.
65+
Example: `data_attr=height__exact__100&data_attr=naam__icontains__boom`
66+
"""
67+
)
68+
% {"value_part_help_text": DATA_ATTR_VALUE_HELP_TEXT}
69+
)
70+
3071

3172
def filter_data_attr_value_part(value_part: str, queryset: QuerySet) -> QuerySet:
3273
"""
@@ -98,49 +139,17 @@ class ObjectRecordFilterSet(FilterSet):
98139
"date would be between `registrationAt` attributes of different records"
99140
),
100141
)
142+
101143
data_attrs = filters.CharFilter(
102144
method="filter_data_attrs",
103145
validators=[validate_data_attrs],
104-
help_text=_(
105-
"""**DEPRECATED: Use 'data_attr' instead**.
106-
Only include objects that have attributes with certain values.
107-
Data filtering expressions are comma-separated and are structured as follows:
108-
109-
%(value_part_help_text)s
110-
111-
Example: in order to display only objects with `height` equal to 100, query `data_attrs=height__exact__100`
112-
should be used. If `height` is nested inside `dimensions` attribute, query should look like
113-
`data_attrs=dimensions__height__exact__100`
114-
115-
`value` may not contain comma, since commas are used as separator between filtering expressions.
116-
If you want to use commas in `value` you can use `data_attr` query parameter.
117-
"""
118-
)
119-
% {"value_part_help_text": DATA_ATTR_VALUE_HELP_TEXT},
146+
help_text=DATA_ATTRS_HELP_TEXT,
120147
)
121148

122149
data_attr = ManyCharFilter(
123150
method="filter_data_attr",
124151
validators=[validate_data_attr],
125-
help_text=_(
126-
"""Only include objects that have attributes with certain values.
127-
128-
%(value_part_help_text)s
129-
130-
Example: in order to display only objects with `height` equal to 100, query `data_attr=height__exact__100`
131-
should be used. If `height` is nested inside `dimensions` attribute, query should look like
132-
`data_attr=dimensions__height__exact__100`
133-
134-
This filter is very similar to the old `data_attrs` filter, but it has two differences:
135-
136-
* `value` may contain commas
137-
* only one filtering expression is allowed
138-
139-
If you want to use several filtering expressions, just use this `data_attr` several times in the query string.
140-
Example: `data_attr=height__exact__100&data_attr=naam__icontains__boom`
141-
"""
142-
)
143-
% {"value_part_help_text": DATA_ATTR_VALUE_HELP_TEXT},
152+
help_text=DATA_ATTR_HELP_TEXT,
144153
)
145154

146155
data_icontains = filters.CharFilter(

src/objects/api/v2/openapi.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ paths:
124124
125125
If you want to use several filtering expressions, just use this `data_attr` several times in the query string.
126126
Example: `data_attr=height__exact__100&data_attr=naam__icontains__boom`
127+
explode: true
127128
- in: query
128129
name: data_attrs
129130
schema:
@@ -157,6 +158,7 @@ paths:
157158
158159
`value` may not contain comma, since commas are used as separator between filtering expressions.
159160
If you want to use commas in `value` you can use `data_attr` query parameter.
161+
deprecated: true
160162
- in: query
161163
name: data_icontains
162164
schema:

src/objects/api/v2/views.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
from django.db import models
55
from django.utils.dateparse import parse_date
66

7-
from drf_spectacular.types import OpenApiTypes
8-
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_view
7+
from drf_spectacular.utils import (
8+
OpenApiParameter,
9+
OpenApiTypes,
10+
extend_schema,
11+
extend_schema_view,
12+
)
913
from rest_framework import mixins, viewsets
1014
from rest_framework.decorators import action
1115
from rest_framework.generics import get_object_or_404
@@ -28,13 +32,32 @@
2832
PermissionSerializer,
2933
)
3034
from ..utils import is_date
31-
from .filters import ObjectRecordFilterSet
35+
from .filters import DATA_ATTR_HELP_TEXT, DATA_ATTRS_HELP_TEXT, ObjectRecordFilterSet
36+
37+
# manually override OAS because of "deprecated" attribute
38+
data_attrs_parameter = OpenApiParameter(
39+
name="data_attrs",
40+
type=OpenApiTypes.STR,
41+
location=OpenApiParameter.QUERY,
42+
description=DATA_ATTRS_HELP_TEXT,
43+
deprecated=True,
44+
)
45+
46+
# manually override OAS because of "explode" attribute
47+
data_attr_parameter = OpenApiParameter(
48+
name="data_attr",
49+
location=OpenApiParameter.QUERY,
50+
type=OpenApiTypes.STR,
51+
description=DATA_ATTR_HELP_TEXT,
52+
explode=True,
53+
)
3254

3355

3456
@extend_schema_view(
3557
list=extend_schema(
3658
description="Retrieve a list of OBJECTs and their actual RECORD. "
37-
"The actual record is defined as if the query parameter `date=<today>` was given."
59+
"The actual record is defined as if the query parameter `date=<today>` was given.",
60+
parameters=[data_attrs_parameter, data_attr_parameter],
3861
),
3962
retrieve=extend_schema(
4063
description="Retrieve a single OBJECT and its actual RECORD. "

src/objects/api/validators.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ def validate_data_attr(value: list):
110110
# check that comma can be only in the value part
111111
if "," in value_part.rsplit("__", 1)[0]:
112112
message = _(
113-
"Filter expression '%(value_part)s' doesn't have the shape 'key__operator__value'"
113+
"Filter expression '%(value_part)s' must have the shape 'key__operator__value', "
114+
"commas can only be present in the 'value'"
114115
) % {"value_part": value_part}
115116
raise serializers.ValidationError(message, code=code)
116117

src/objects/tests/v2/test_filters.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ def test_filter_exact_date(self):
492492
record = ObjectRecordFactory.create(
493493
data={"date": "2000-11-01"}, object__object_type=self.object_type
494494
)
495+
ObjectRecordFactory.create(
496+
data={"date": "2020-11-01"}, object__object_type=self.object_type
497+
)
498+
ObjectRecordFactory.create(data={}, object__object_type=self.object_type)
495499

496500
response = self.client.get(self.url, {"data_attr": "date__exact__2000-11-01"})
497501
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -814,7 +818,8 @@ def test_filter_comma_separated_invalid(self):
814818
response.json(),
815819
[
816820
"Filter expression 'dimensions__diameter__exact__4,name__exact__demo' "
817-
"doesn't have the shape 'key__operator__value'"
821+
"must have the shape 'key__operator__value', commas can only be present in "
822+
"the 'value'"
818823
],
819824
)
820825

0 commit comments

Comments
 (0)