Skip to content

Commit

Permalink
Fixed choice param issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mtyton committed Jan 17, 2024
1 parent a8745ad commit 2afc475
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
18 changes: 13 additions & 5 deletions audoma/drf/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
class DocumentedTypedChoiceFilter(df_filters.TypedChoiceFilter):
"""Extended TypedChoiceFilter to generate documentation automatically"""

def _parse_choices(self, choices):
if hasattr(choices, "get_api_choices"):
return choices.get_api_choices()
if isinstance(choices, dict):
return choices
if isinstance(choices, (list, tuple)):
if isinstance(choices[0], (list, tuple)):
return choices
else:
return tuple([(c, c) for c in choices])
raise ValueError(f"Choices must be a dict, list or tuple, not {type(choices)}")

def __init__(
self, full_choices: Union[NamedTuple, Tuple], parameter_name: str, **kwargs
) -> None:
self.parsed_choices = (
full_choices.get_api_choices()
if hasattr(full_choices, "get_api_choices")
else full_choices
)
self.parsed_choices = self._parse_choices(full_choices)
if hasattr(full_choices, "get_value_by_name"):

def coerce(value):
Expand Down
2 changes: 2 additions & 0 deletions audoma/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def resolve_filter_field(
if choices and "x-choices" not in schema:
schema["schema"]["x-choices"] = self._get_x_choices(choices)
parsed_schemas.append(schema)
if not choices:
parsed_schemas.append(schema)

return parsed_schemas

Expand Down
5 changes: 4 additions & 1 deletion audoma/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ def test_get_x_choices_no_choices(self):
result = self.extension.resolve_filter_field(
self.view.schema, self.view.model, None, "company_rate", field
)
self.assertEqual(result, [])
self.assertEqual(
result,
[{"in": "query", "name": "company_rate", "schema": {"type": "string"}}],
)


class SearchFilterExtensionTestCase(TestCase):
Expand Down
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
==========
Changelog

0.6.4
======

Added Features
---------------
* Fixed documentng native `django_filters` fields
==========

0.6.4
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_reqiuired_packages():

setup(
name=name,
version="0.6.4",
version="0.6.6",
packages=find_packages(),
install_requires=get_reqiuired_packages(),
description=description,
Expand Down

0 comments on commit 2afc475

Please sign in to comment.