Skip to content

Commit

Permalink
👌 [#472] process PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
annashamray committed Dec 18, 2024
1 parent 2e3ac54 commit 5ee5e7e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 41 deletions.
79 changes: 44 additions & 35 deletions src/objects/api/v2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,47 @@
"""

DATA_ATTRS_HELP_TEXT = (
_(
"""**DEPRECATED: Use 'data_attr' instead**.
Only include objects that have attributes with certain values.
Data filtering expressions are comma-separated and are structured as follows:
%(value_part_help_text)s
Example: in order to display only objects with `height` equal to 100, query `data_attrs=height__exact__100`
should be used. If `height` is nested inside `dimensions` attribute, query should look like
`data_attrs=dimensions__height__exact__100`
`value` may not contain comma, since commas are used as separator between filtering expressions.
If you want to use commas in `value` you can use `data_attr` query parameter.
"""
)
% {"value_part_help_text": DATA_ATTR_VALUE_HELP_TEXT}
)

DATA_ATTR_HELP_TEXT = (
_(
"""Only include objects that have attributes with certain values.
%(value_part_help_text)s
Example: in order to display only objects with `height` equal to 100, query `data_attr=height__exact__100`
should be used. If `height` is nested inside `dimensions` attribute, query should look like
`data_attr=dimensions__height__exact__100`
This filter is very similar to the old `data_attrs` filter, but it has two differences:
* `value` may contain commas
* only one filtering expression is allowed
If you want to use several filtering expressions, just use this `data_attr` several times in the query string.
Example: `data_attr=height__exact__100&data_attr=naam__icontains__boom`
"""
)
% {"value_part_help_text": DATA_ATTR_VALUE_HELP_TEXT}
)


def filter_data_attr_value_part(value_part: str, queryset: QuerySet) -> QuerySet:
"""
Expand Down Expand Up @@ -98,49 +139,17 @@ class ObjectRecordFilterSet(FilterSet):
"date would be between `registrationAt` attributes of different records"
),
)

data_attrs = filters.CharFilter(
method="filter_data_attrs",
validators=[validate_data_attrs],
help_text=_(
"""**DEPRECATED: Use 'data_attr' instead**.
Only include objects that have attributes with certain values.
Data filtering expressions are comma-separated and are structured as follows:
%(value_part_help_text)s
Example: in order to display only objects with `height` equal to 100, query `data_attrs=height__exact__100`
should be used. If `height` is nested inside `dimensions` attribute, query should look like
`data_attrs=dimensions__height__exact__100`
`value` may not contain comma, since commas are used as separator between filtering expressions.
If you want to use commas in `value` you can use `data_attr` query parameter.
"""
)
% {"value_part_help_text": DATA_ATTR_VALUE_HELP_TEXT},
help_text=DATA_ATTRS_HELP_TEXT,
)

data_attr = ManyCharFilter(
method="filter_data_attr",
validators=[validate_data_attr],
help_text=_(
"""Only include objects that have attributes with certain values.
%(value_part_help_text)s
Example: in order to display only objects with `height` equal to 100, query `data_attr=height__exact__100`
should be used. If `height` is nested inside `dimensions` attribute, query should look like
`data_attr=dimensions__height__exact__100`
This filter is very similar to the old `data_attrs` filter, but it has two differences:
* `value` may contain commas
* only one filtering expression is allowed
If you want to use several filtering expressions, just use this `data_attr` several times in the query string.
Example: `data_attr=height__exact__100&data_attr=naam__icontains__boom`
"""
)
% {"value_part_help_text": DATA_ATTR_VALUE_HELP_TEXT},
help_text=DATA_ATTR_HELP_TEXT,
)

data_icontains = filters.CharFilter(
Expand Down
2 changes: 2 additions & 0 deletions src/objects/api/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ paths:
If you want to use several filtering expressions, just use this `data_attr` several times in the query string.
Example: `data_attr=height__exact__100&data_attr=naam__icontains__boom`
explode: true
- in: query
name: data_attrs
schema:
Expand Down Expand Up @@ -157,6 +158,7 @@ paths:
`value` may not contain comma, since commas are used as separator between filtering expressions.
If you want to use commas in `value` you can use `data_attr` query parameter.
deprecated: true
- in: query
name: data_icontains
schema:
Expand Down
31 changes: 27 additions & 4 deletions src/objects/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
from django.db import models
from django.utils.dateparse import parse_date

from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_view
from drf_spectacular.utils import (
OpenApiParameter,
OpenApiTypes,
extend_schema,
extend_schema_view,
)
from rest_framework import mixins, viewsets
from rest_framework.decorators import action
from rest_framework.generics import get_object_or_404
Expand All @@ -28,13 +32,32 @@
PermissionSerializer,
)
from ..utils import is_date
from .filters import ObjectRecordFilterSet
from .filters import DATA_ATTR_HELP_TEXT, DATA_ATTRS_HELP_TEXT, ObjectRecordFilterSet

# manually override OAS because of "deprecated" attribute
data_attrs_parameter = OpenApiParameter(
name="data_attrs",
type=OpenApiTypes.STR,
location=OpenApiParameter.QUERY,
description=DATA_ATTRS_HELP_TEXT,
deprecated=True,
)

# manually override OAS because of "explode" attribute
data_attr_parameter = OpenApiParameter(
name="data_attr",
location=OpenApiParameter.QUERY,
type=OpenApiTypes.STR,
description=DATA_ATTR_HELP_TEXT,
explode=True,
)


@extend_schema_view(
list=extend_schema(
description="Retrieve a list of OBJECTs and their actual RECORD. "
"The actual record is defined as if the query parameter `date=<today>` was given."
"The actual record is defined as if the query parameter `date=<today>` was given.",
parameters=[data_attrs_parameter, data_attr_parameter],
),
retrieve=extend_schema(
description="Retrieve a single OBJECT and its actual RECORD. "
Expand Down
3 changes: 2 additions & 1 deletion src/objects/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def validate_data_attr(value: list):
# check that comma can be only in the value part
if "," in value_part.rsplit("__", 1)[0]:
message = _(
"Filter expression '%(value_part)s' doesn't have the shape 'key__operator__value'"
"Filter expression '%(value_part)s' must have the shape 'key__operator__value', "
"commas can only be present in the 'value'"
) % {"value_part": value_part}
raise serializers.ValidationError(message, code=code)

Expand Down
7 changes: 6 additions & 1 deletion src/objects/tests/v2/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ def test_filter_exact_date(self):
record = ObjectRecordFactory.create(
data={"date": "2000-11-01"}, object__object_type=self.object_type
)
ObjectRecordFactory.create(
data={"date": "2020-11-01"}, object__object_type=self.object_type
)
ObjectRecordFactory.create(data={}, object__object_type=self.object_type)

response = self.client.get(self.url, {"data_attr": "date__exact__2000-11-01"})
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down Expand Up @@ -814,7 +818,8 @@ def test_filter_comma_separated_invalid(self):
response.json(),
[
"Filter expression 'dimensions__diameter__exact__4,name__exact__demo' "
"doesn't have the shape 'key__operator__value'"
"must have the shape 'key__operator__value', commas can only be present in "
"the 'value'"
],
)

Expand Down

0 comments on commit 5ee5e7e

Please sign in to comment.