Skip to content

filters#

parse_csv_string#

Source code in src/apps/common/filters.py
def parse_csv_string(string):
    reader = csv.reader([string], delimiter=",", quotechar='"', escapechar="\\")
    return next(reader)

parse_search_string#

Source code in src/apps/common/filters.py
def parse_search_string(string):
    reader = csv.reader([string], delimiter=" ", quotechar='"', escapechar="\\")
    return next(reader)

VerboseChoiceField#

Bases: ChoiceField

Source code in src/apps/common/filters.py
class VerboseChoiceField(fields.ChoiceField):
    default_error_messages = {
        "invalid_choice": _("Value '%(value)s' is not a valid choice.")
        + " "
        + _("Valid choices are: %(choices)s"),
    }

    def validate(self, value):
        """Validate that the input is in self.choices."""
        super(forms.ChoiceField, self).validate(value)
        if value and not self.valid_value(value):
            # Like forms.ChoiceField.validate but adds choices to error params
            raise exceptions.ValidationError(
                self.error_messages["invalid_choice"],
                code="invalid_choice",
                params={"value": value, "choices": list([c[0] for c in self.choices])},
            )

validate(value) #

Validate that the input is in self.choices.

Source code in src/apps/common/filters.py
def validate(self, value):
    """Validate that the input is in self.choices."""
    super(forms.ChoiceField, self).validate(value)
    if value and not self.valid_value(value):
        # Like forms.ChoiceField.validate but adds choices to error params
        raise exceptions.ValidationError(
            self.error_messages["invalid_choice"],
            code="invalid_choice",
            params={"value": value, "choices": list([c[0] for c in self.choices])},
        )

VerboseChoiceFilter#

Bases: ChoiceFilter

Source code in src/apps/common/filters.py
class VerboseChoiceFilter(filters.ChoiceFilter):
    field_class = VerboseChoiceField

MultipleTextInput#

Bases: TextInput

Source code in src/apps/common/filters.py
class MultipleTextInput(forms.TextInput):
    def value_from_datadict(self, data, files, name):
        try:
            getter = data.getlist
        except AttributeError:
            getter = data.get
        return getter(name)

MultipleCharField#

Bases: CharField

Source code in src/apps/common/filters.py
class MultipleCharField(forms.CharField):
    widget = MultipleTextInput

    default_error_messages = {
        "invalid_list": _("Enter a list of values."),
    }

    def to_python(self, value):
        if not value:
            return []
        elif not isinstance(value, (list, tuple)):
            raise forms.ValidationError(self.error_messages["invalid_list"], code="invalid_list")

        return [parse_csv_string(str(val)) for val in value]

MultipleCharFilter#

Bases: MultipleChoiceFilter

Source code in src/apps/common/filters.py
class MultipleCharFilter(filters.MultipleChoiceFilter):
    field_class = MultipleCharField

    def filter(self, qs, value):
        if not value:
            # Even though not a noop, no point filtering if empty.
            return qs

        if self.is_noop(qs, value):
            return qs

        qq = Q()
        for term in value:
            q = Q()
            for v in set(term):
                predicate = self.get_filter_predicate(v)
                q |= Q(**predicate)
            qq &= q

        qs = qs.filter(qq)

        return qs.distinct() if self.distinct else qs

SearchField#

Bases: CharField

Source code in src/apps/common/filters.py
class SearchField(forms.CharField):
    def to_python(self, value):
        if not value:
            return []
        if not isinstance(value, (str)):
            raise forms.ValidationError(self.error_messages["invalid_str"], code="invalid_str")

        return parse_search_string(str(value))

SearchFilter#

Bases: CharFilter

Source code in src/apps/common/filters.py
class SearchFilter(filters.CharFilter):
    field_class = SearchField

CustomDjangoFilterBackend#

Bases: DjangoFilterBackend

Filter backend that shows boolean filters as booleans in swagger.

Source code in src/apps/common/filters.py
class CustomDjangoFilterBackend(filters.DjangoFilterBackend):
    """Filter backend that shows boolean filters as booleans in swagger."""

    def get_operation_parameter_type(self, field):
        if isinstance(field, filters.BooleanFilter):
            return "boolean"
        return "string"

    def get_schema_operation_parameters(self, view):
        try:
            queryset = view.get_queryset()
        except Exception:
            queryset = None

        filterset_class = self.get_filterset_class(view, queryset)

        if not filterset_class:
            return []

        parameters = []
        for field_name, field in filterset_class.base_filters.items():
            parameter = {
                "name": field_name,
                "required": field.extra["required"],
                "in": "query",
                "description": field.label if field.label is not None else field_name,
                "schema": {
                    # Original implementation always uses "string" type here
                    "type": self.get_operation_parameter_type(field),
                },
            }
            if field.extra and "choices" in field.extra:
                parameter["schema"]["enum"] = [c[0] for c in field.extra["choices"]]
            parameters.append(parameter)
        return parameters