|
1 | 1 | from pathlib import Path |
2 | 2 | from uuid import UUID |
3 | 3 |
|
4 | | -from fastadmin.models.schemas import ModelFieldWidgetSchema |
5 | | - |
6 | | - |
7 | | -def sanitize_filter_value(value: str | list) -> bool | None | str | list: |
| 4 | +from fastadmin.models.schemas import ModelFieldWidgetSchema, WidgetType |
| 5 | + |
| 6 | +# Text-like filter widgets whose values are free-form strings. For these the |
| 7 | +# literals "true"/"false"/"null" are legitimate content and must NOT be coerced |
| 8 | +# to bool/None (otherwise a Char column can never be filtered for those words). |
| 9 | +TEXT_FILTER_WIDGET_TYPES = frozenset( |
| 10 | + { |
| 11 | + WidgetType.Input, |
| 12 | + WidgetType.TextArea, |
| 13 | + WidgetType.RichTextArea, |
| 14 | + WidgetType.JsonTextArea, |
| 15 | + WidgetType.SlugInput, |
| 16 | + WidgetType.EmailInput, |
| 17 | + WidgetType.PhoneInput, |
| 18 | + WidgetType.UrlInput, |
| 19 | + WidgetType.PasswordInput, |
| 20 | + } |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def sanitize_filter_value( |
| 25 | + value: str | list, |
| 26 | + field: ModelFieldWidgetSchema | None = None, |
| 27 | +) -> bool | None | str | list: |
8 | 28 | """Sanitize value (string or list for __in filters). |
9 | 29 |
|
10 | 30 | :params value: a value (str or list of str for __in). |
| 31 | + :params field: the field being filtered, used to decide whether the |
| 32 | + "true"/"false"/"null" literals should be coerced (skipped for text fields). |
11 | 33 | :return: A sanitized value. |
12 | 34 | """ |
13 | 35 | if isinstance(value, list): |
14 | | - return [sanitize_filter_value(v) for v in value] |
| 36 | + return [sanitize_filter_value(v, field) for v in value] |
| 37 | + if field is not None and field.filter_widget_type in TEXT_FILTER_WIDGET_TYPES: |
| 38 | + return value |
15 | 39 | match value: |
16 | 40 | case "false": |
17 | 41 | return False |
@@ -64,6 +88,31 @@ def sanitize_filter_key(key: str, fields: list[ModelFieldWidgetSchema]) -> tuple |
64 | 88 | return field_name, condition |
65 | 89 |
|
66 | 90 |
|
| 91 | +def build_query_filters( |
| 92 | + filters: dict, |
| 93 | + fields: list[ModelFieldWidgetSchema], |
| 94 | + exclude: tuple[str, ...], |
| 95 | +) -> dict[tuple[str, str], bool | None | str | list]: |
| 96 | + """Build the sanitized ``{(field_name, condition): value}`` filter dict. |
| 97 | +
|
| 98 | + Resolves each key's field so value coercion (true/false/null) can be applied |
| 99 | + in a type-aware way (see :func:`sanitize_filter_value`). |
| 100 | +
|
| 101 | + :param filters: raw filters mapping ``key -> value``. |
| 102 | + :param fields: model fields with widget types. |
| 103 | + :param exclude: keys to skip (search, sort_by, offset, limit). |
| 104 | + :return: sanitized filters dict. |
| 105 | + """ |
| 106 | + result: dict[tuple[str, str], bool | None | str | list] = {} |
| 107 | + for key, value in filters.items(): |
| 108 | + if key in exclude: |
| 109 | + continue |
| 110 | + field_name = key.partition("__")[0] |
| 111 | + field = next((f for f in fields if f.name == field_name), None) |
| 112 | + result[sanitize_filter_key(key, fields)] = sanitize_filter_value(value, field) |
| 113 | + return result |
| 114 | + |
| 115 | + |
67 | 116 | def is_valid_uuid(uuid_to_test: str) -> bool: |
68 | 117 | """Check if uuid_to_test is a valid uuid. |
69 | 118 |
|
|
0 commit comments