Coverage for audoma/drf/filters.py: 100%

1 from typing import NamedTuple

2

3 from django_filters import rest_framework as df_filters

4

5 from audoma.plumbing import create_choices_enum_description

6

7

8 class DocumentedTypedChoiceFilter(df_filters.TypedChoiceFilter):

9 """Extended TypedChoiceFilter to generate documentation automatically"""

10

11 def __init__(self, full_choices: NamedTuple, parameter_name: str, **kwargs) -> None:

12 super().__init__(

13 coerce=lambda value: full_choices.get_value_by_name(value),

14 choices=full_choices.get_api_choices(),

15 **kwargs,

16 )

17 self.full_choices = full_choices

18 self.parameter_name = parameter_name

19 self.extra["help_text"] = self.extra.get("help_text", "{choices}").format(

20 choices=create_choices_enum_description(

21 full_choices.get_api_choices(), self.field_name

22 )

23 )

24 self.extra["choices"] = full_choices.get_api_choices()