1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
from __future__ import annotations
import abc
from typing import (
TYPE_CHECKING,
Any,
Callable,
Iterable,
Optional,
Pattern,
Set,
Type,
Union,
cast,
)
from pydantic.config import JsonDict
from pydantic.fields import Field as PDField
from pydantic.fields import FieldInfo, PydanticUndefined
from odmantic.config import ODMConfigDict
from odmantic.query import (
QueryExpression,
SortExpression,
asc,
desc,
eq,
gt,
gte,
in_,
lt,
lte,
match,
ne,
not_in,
)
if TYPE_CHECKING:
from odmantic.model import EmbeddedModel, Model # noqa: F401
from .typing import NoArgAnyCallable
def Field(
default: Any = PydanticUndefined,
*,
key_name: Optional[str] = None,
primary_field: bool = False,
index: bool = False,
unique: bool = False,
default_factory: Optional["NoArgAnyCallable"] = None,
# alias: str = None, # FIXME not supported yet
title: Optional[str] = None,
description: Optional[str] = None,
json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = None,
const: Optional[bool] = None,
gt: Optional[float] = None,
ge: Optional[float] = None,
lt: Optional[float] = None,
le: Optional[float] = None,
multiple_of: Optional[float] = None,
min_items: Optional[int] = None,
max_items: Optional[int] = None,
min_length: Optional[int] = None,
max_length: Optional[int] = None,
regex: Optional[str] = None,
examples: list[Any] | None = None,
) -> Any:
"""Used to provide extra information about a field, either for the model schema or
complex validation. Some arguments apply only to number fields (``int``, ``float``,
``Decimal``) and some apply only to ``str``.
Tip:
The main additions of ODMantic to the regular pydantic `Field` are the
`key_name`, `index`, `unique` and the `primary_field` options.
Warning:
If both `default` and `default_factory` are set, an error is raised.
Warning:
`primary_field` can't be used along with `key_name` since the key_name will be
set to `_id`.
Args:
default: since this is replacing the field’s default, its first argument is
used to set the default, use ellipsis (``...``) to indicate the field has no
default value
key_name: the name to use in the the mongo document structure
primary_field: this field should be considered as a primary key.
index: this field should be considered as an index
unique: this field should be considered as a unique index
default_factory: callable that will be called when a default value is needed
for this field.
title: can be any string, used in the schema
description: can be any string, used in the schema
examples: can be any list, used in the schema
json_schema_extra: Any additional JSON schema data for the schema property.
const: this field is required and *must* take it's default value
gt: only applies to numbers, requires the field to be "greater than". The
schema will have an ``exclusiveMinimum`` validation keyword
ge: only applies to numbers, requires the field to be "greater than or equal
to". The schema will have a ``minimum`` validation keyword
lt: only applies to numbers, requires the field to be "less than". The schema
will have an ``exclusiveMaximum`` validation keyword
le: only applies to numbers, requires the field to be "less than or equal to"
. The schema will have a ``maximum`` validation keyword
multiple_of: only applies to numbers, requires the field to be "a multiple of
". The schema will have a ``multipleOf`` validation keyword
min_items: only applies to sequences, requires the field to have a minimum
item count.
max_items: only applies to sequences, requires the field to have a maximum
item count.
min_length: only applies to strings, requires the field to have a minimum
length. The schema will have a ``maximum`` validation keyword
max_length: only applies to strings, requires the field to have a maximum
length. The schema will have a ``maxLength`` validation keyword
regex: only applies to strings, requires the field match agains a regular
expression pattern string. The schema will have a ``pattern`` validation
keyword
<!---
# noqa: DAR201
# noqa: DAR003
# noqa: DAR401
# noqa: DAR101
-->
"""
# Perform casts on optional fields to avoid incompatibility due to the strict
# optional mypy setting
# TODO: add remaining validation fields from pydantic
pydantic_field = PDField(
default,
default_factory=default_factory,
# alias=alias, # FIXME check aliases compatibility
title=cast(str, title),
description=cast(str, description),
examples=examples,
json_schema_extra=json_schema_extra,
const=cast(bool, const),
gt=cast(float, gt),
ge=cast(float, ge),
lt=cast(float, lt),
le=cast(float, le),
multiple_of=cast(float, multiple_of),
min_items=cast(int, min_items),
max_items=cast(int, max_items),
min_length=cast(int, min_length),
max_length=cast(int, max_length),
regex=cast(str, regex),
)
if primary_field:
if key_name is not None and key_name != "_id":
raise ValueError(
"cannot specify a primary field with a custom key_name,"
"key_name='_id' enforced"
)
else:
key_name = "_id"
elif key_name == "_id":
raise ValueError(
"cannot specify key_name='_id' without defining the field as primary"
)
return ODMFieldInfo(
pydantic_field_info=pydantic_field,
primary_field=primary_field,
key_name=key_name,
index=index,
unique=unique,
)
class ODMFieldInfo:
"""Extra data for an ODM field."""
__slots__ = ("pydantic_field_info", "primary_field", "key_name", "index", "unique")
def __init__(
self,
*,
pydantic_field_info: FieldInfo,
primary_field: bool,
key_name: Optional[str],
index: bool,
unique: bool,
):
self.pydantic_field_info = pydantic_field_info
self.primary_field = primary_field
self.key_name = key_name
self.index = index
self.unique = unique
class ODMBaseField(metaclass=abc.ABCMeta):
__slots__ = ("key_name", "model_config", "pydantic_field")
__allowed_operators__: Set[str]
def __init__(self, key_name: str, model_config: ODMConfigDict):
self.key_name = key_name
self.model_config = model_config
def bind_pydantic_field(self, field: FieldInfo) -> None:
self.pydantic_field = field
def is_required_in_doc(self) -> bool:
if self.model_config["parse_doc_with_default_factories"]:
return self.pydantic_field.is_required()
else:
return (
self.pydantic_field.default_factory is not None
or self.pydantic_field.is_required()
)
class ODMBaseIndexableField(ODMBaseField, metaclass=abc.ABCMeta):
__slots__ = ("index", "unique")
def __init__(
self,
key_name: str,
model_config: ODMConfigDict,
index: bool,
unique: bool,
):
super().__init__(key_name, model_config)
self.index = index
self.unique = unique
class ODMField(ODMBaseIndexableField):
"""Used to interact with the ODM model class."""
__slots__ = ("primary_field",)
__allowed_operators__ = set(
("eq", "ne", "in_", "not_in", "lt", "lte", "gt", "gte", "match", "asc", "desc")
)
def __init__(
self,
*,
key_name: str,
model_config: ODMConfigDict,
primary_field: bool,
index: bool = False,
unique: bool = False,
):
super().__init__(key_name, model_config, index, unique)
self.primary_field = primary_field
def get_default_importing_value(self) -> Any:
# The default importing value doesn't consider the default_factory setting by
# default as it could result in inconsistent behaviors for datetime.now
# factories for example
return self.pydantic_field.get_default(
call_default_factory=self.model_config["parse_doc_with_default_factories"]
)
class ODMReference(ODMBaseField):
"""Field pointing on a referenced model."""
__slots__ = ("model",)
__allowed_operators__ = set(("eq", "ne", "in_", "not_in"))
def __init__(
self, key_name: str, model_config: ODMConfigDict, model: Type["Model"]
):
super().__init__(key_name, model_config)
self.model = model
class ODMEmbedded(ODMField):
__slots__ = "model"
__allowed_operators__ = set(("eq", "ne", "in_", "not_in"))
def __init__(
self,
primary_field: bool,
key_name: str,
model_config: ODMConfigDict,
model: Type["EmbeddedModel"],
index: bool = False,
unique: bool = False,
):
super().__init__(
primary_field=primary_field,
key_name=key_name,
model_config=model_config,
index=index,
unique=unique,
)
self.model = model
class ODMEmbeddedGeneric(ODMField):
# Only dict,set and list are "officially" supported for now
__slots__ = ("model", "generic_origin")
__allowed_operators__ = set(("eq", "ne"))
def __init__(
self,
key_name: str,
model_config: ODMConfigDict,
model: Type["EmbeddedModel"],
generic_origin: Any,
index: bool = False,
unique: bool = False,
):
super().__init__(
primary_field=False,
key_name=key_name,
model_config=model_config,
index=index,
unique=unique,
)
self.model = model
self.generic_origin = generic_origin
class KeyNameProxy(str):
"""Used to provide the `++` operator enabling reference key name creation"""
def __pos__(self) -> str:
return f"${self}"
class FieldProxy:
__slots__ = ("parent", "field")
def __init__(self, parent: Optional["FieldProxy"], field: ODMBaseField) -> None:
self.parent = parent
self.field = field
def _get_key_name(self) -> str:
parent: Optional[FieldProxy] = object.__getattribute__(self, "parent")
field: ODMBaseField = object.__getattribute__(self, "field")
if parent is None:
return field.key_name
parent_name: str = object.__getattribute__(parent, "_get_key_name")()
return f"{parent_name}.{field.key_name}"
def __getattribute__(self, name: str) -> Any:
if name == "__class__": # support `isinstance` for python < 3.7
return super().__getattribute__(name)
field: ODMBaseField = object.__getattribute__(self, "field")
if isinstance(field, ODMReference):
if name in field.model.__odm_fields__:
raise NotImplementedError(
"filtering across references is not supported"
)
elif isinstance(field, ODMEmbedded):
child_field = field.model.__odm_fields__.get(name)
if child_field is None:
try:
return super().__getattribute__(name)
except AttributeError:
raise AttributeError(
f"attribute {name} not found in {field.model.__name__}"
)
return FieldProxy(parent=self, field=child_field)
if name not in field.__allowed_operators__:
raise AttributeError(
f"operator {name} not allowed for {type(field).__name__} fields"
)
return super().__getattribute__(name)
def __pos__(self) -> KeyNameProxy:
return KeyNameProxy(object.__getattribute__(self, "_get_key_name")())
def __gt__(self, value: Any) -> QueryExpression:
return self.gt(value)
def gt(self, value: Any) -> QueryExpression:
return gt(self, value)
def gte(self, value: Any) -> QueryExpression:
return gte(self, value)
def __ge__(self, value: Any) -> QueryExpression:
return self.gte(value)
def lt(self, value: Any) -> QueryExpression:
return lt(self, value)
def __lt__(self, value: Any) -> QueryExpression:
return self.lt(value)
def lte(self, value: Any) -> QueryExpression:
return lte(self, value)
def __le__(self, value: Any) -> QueryExpression:
return self.lte(value)
def eq(self, value: Any) -> QueryExpression:
return eq(self, value)
def __eq__(self, value: Any) -> QueryExpression: # type: ignore
return self.eq(value)
def ne(self, value: Any) -> QueryExpression:
return ne(self, value)
def __ne__(self, value: Any) -> QueryExpression: # type: ignore
return self.ne(value)
def in_(self, value: Iterable) -> QueryExpression:
return in_(self, value)
def not_in(self, value: Iterable) -> QueryExpression:
return not_in(self, value)
def match(self, pattern: Union[Pattern, str]) -> QueryExpression:
return match(self, pattern)
def asc(self) -> SortExpression:
return asc(self)
def desc(self) -> SortExpression:
return desc(self)
|