217 lines
5.9 KiB
Python
217 lines
5.9 KiB
Python
from __future__ import annotations
|
|
|
|
from decimal import Decimal, InvalidOperation
|
|
from datetime import date
|
|
from typing import Any
|
|
from uuid import UUID
|
|
|
|
from .domain import AttributeDefinitionRecord
|
|
from .errors import InvalidSearchOperatorError, InvalidSearchValueError
|
|
from .schema import SearchOperator
|
|
|
|
|
|
_TEXT_OPERATORS = {
|
|
SearchOperator.EQ,
|
|
SearchOperator.NEQ,
|
|
SearchOperator.CONTAINS,
|
|
SearchOperator.STARTS_WITH,
|
|
SearchOperator.IN,
|
|
}
|
|
|
|
_ORDERED_OPERATORS = {
|
|
SearchOperator.EQ,
|
|
SearchOperator.NEQ,
|
|
SearchOperator.IN,
|
|
SearchOperator.GT,
|
|
SearchOperator.GTE,
|
|
SearchOperator.LT,
|
|
SearchOperator.LTE,
|
|
}
|
|
|
|
_BOOLEAN_OPERATORS = {
|
|
SearchOperator.EQ,
|
|
SearchOperator.NEQ,
|
|
SearchOperator.IN,
|
|
}
|
|
|
|
_JSON_OPERATORS = {
|
|
SearchOperator.EQ,
|
|
SearchOperator.NEQ,
|
|
SearchOperator.CONTAINS,
|
|
}
|
|
|
|
_MEDIA_OPERATORS = {
|
|
SearchOperator.EQ,
|
|
SearchOperator.NEQ,
|
|
SearchOperator.IN,
|
|
}
|
|
|
|
|
|
def operators_for_definition(
|
|
definition: AttributeDefinitionRecord,
|
|
) -> list[SearchOperator]:
|
|
if definition.definition_kind != "scalar":
|
|
return []
|
|
|
|
if definition.data_type in {"text", "rich_text"}:
|
|
allowed = _TEXT_OPERATORS
|
|
elif definition.data_type in {"number", "date"}:
|
|
allowed = _ORDERED_OPERATORS
|
|
elif definition.data_type == "boolean":
|
|
allowed = _BOOLEAN_OPERATORS
|
|
elif definition.data_type == "json":
|
|
allowed = _JSON_OPERATORS
|
|
elif definition.data_type == "media":
|
|
allowed = _MEDIA_OPERATORS
|
|
else:
|
|
allowed = set()
|
|
|
|
# Stable API ordering.
|
|
order = [
|
|
SearchOperator.EQ,
|
|
SearchOperator.NEQ,
|
|
SearchOperator.CONTAINS,
|
|
SearchOperator.STARTS_WITH,
|
|
SearchOperator.IN,
|
|
SearchOperator.GT,
|
|
SearchOperator.GTE,
|
|
SearchOperator.LT,
|
|
SearchOperator.LTE,
|
|
]
|
|
return [operator for operator in order if operator in allowed]
|
|
|
|
|
|
def renderer_hint_for_definition(
|
|
definition: AttributeDefinitionRecord,
|
|
) -> str:
|
|
if definition.allowed_values:
|
|
return "select"
|
|
if definition.data_type == "boolean":
|
|
return "select"
|
|
if definition.data_type == "number":
|
|
return "number"
|
|
if definition.data_type == "date":
|
|
return "date"
|
|
return "text"
|
|
|
|
|
|
def is_sortable_definition(
|
|
definition: AttributeDefinitionRecord,
|
|
) -> bool:
|
|
return (
|
|
definition.definition_kind == "scalar"
|
|
and not definition.is_multivalue
|
|
and definition.data_type in {"text", "rich_text", "number", "date"}
|
|
)
|
|
|
|
|
|
def coerce_filter_value(
|
|
definition: AttributeDefinitionRecord,
|
|
operator: SearchOperator,
|
|
raw_value: Any,
|
|
) -> Any:
|
|
allowed = operators_for_definition(definition)
|
|
if operator not in allowed:
|
|
raise InvalidSearchOperatorError(
|
|
f"Operator {operator.value!r} is not valid for "
|
|
f"{definition.attribute_key!r} ({definition.data_type!r})"
|
|
)
|
|
|
|
if operator == SearchOperator.IN:
|
|
if not isinstance(raw_value, list) or not raw_value:
|
|
raise InvalidSearchValueError(
|
|
f"Operator 'in' requires a non-empty array for "
|
|
f"{definition.attribute_key!r}"
|
|
)
|
|
converted = [
|
|
_coerce_scalar(definition, item)
|
|
for item in raw_value
|
|
]
|
|
_validate_allowed_values(definition, converted)
|
|
return converted
|
|
|
|
converted = _coerce_scalar(definition, raw_value)
|
|
_validate_allowed_values(definition, [converted])
|
|
return converted
|
|
|
|
|
|
def _validate_allowed_values(
|
|
definition: AttributeDefinitionRecord,
|
|
values: list[Any],
|
|
) -> None:
|
|
if definition.allowed_values is None:
|
|
return
|
|
for value in values:
|
|
if value not in definition.allowed_values:
|
|
raise InvalidSearchValueError(
|
|
f"Value {value!r} is not allowed for "
|
|
f"{definition.attribute_key!r}"
|
|
)
|
|
|
|
|
|
def _coerce_scalar(
|
|
definition: AttributeDefinitionRecord,
|
|
value: Any,
|
|
) -> Any:
|
|
if value is None:
|
|
raise InvalidSearchValueError(
|
|
f"Search value for {definition.attribute_key!r} cannot be null"
|
|
)
|
|
|
|
data_type = definition.data_type
|
|
|
|
if data_type in {"text", "rich_text"}:
|
|
if not isinstance(value, str):
|
|
raise InvalidSearchValueError(
|
|
f"{definition.attribute_key!r} requires a string value"
|
|
)
|
|
return value
|
|
|
|
if data_type == "number":
|
|
if isinstance(value, bool):
|
|
raise InvalidSearchValueError(
|
|
f"{definition.attribute_key!r} requires a numeric value"
|
|
)
|
|
try:
|
|
return Decimal(str(value))
|
|
except (InvalidOperation, TypeError, ValueError) as exc:
|
|
raise InvalidSearchValueError(
|
|
f"{definition.attribute_key!r} requires a numeric value"
|
|
) from exc
|
|
|
|
if data_type == "boolean":
|
|
if not isinstance(value, bool):
|
|
raise InvalidSearchValueError(
|
|
f"{definition.attribute_key!r} requires a boolean value"
|
|
)
|
|
return value
|
|
|
|
if data_type == "date":
|
|
if isinstance(value, date):
|
|
return value
|
|
if isinstance(value, str):
|
|
try:
|
|
return date.fromisoformat(value)
|
|
except ValueError as exc:
|
|
raise InvalidSearchValueError(
|
|
f"{definition.attribute_key!r} requires YYYY-MM-DD"
|
|
) from exc
|
|
raise InvalidSearchValueError(
|
|
f"{definition.attribute_key!r} requires YYYY-MM-DD"
|
|
)
|
|
|
|
if data_type == "json":
|
|
return value
|
|
|
|
if data_type == "media":
|
|
try:
|
|
return UUID(str(value))
|
|
except (TypeError, ValueError) as exc:
|
|
raise InvalidSearchValueError(
|
|
f"{definition.attribute_key!r} requires a media UUID"
|
|
) from exc
|
|
|
|
raise InvalidSearchValueError(
|
|
f"Unsupported search data type {data_type!r}"
|
|
)
|