fix json fields and fix choices validation
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
from ormar.models.helpers.models import (
|
||||
check_required_meta_parameters,
|
||||
extract_annotations_and_default_vals,
|
||||
meta_field_not_set,
|
||||
populate_default_options_values,
|
||||
)
|
||||
from ormar.models.helpers.pydantic import (
|
||||
@ -15,7 +17,9 @@ from ormar.models.helpers.relations import expand_reverse_relationships
|
||||
from ormar.models.helpers.sqlalchemy import (
|
||||
populate_meta_sqlalchemy_table_if_required,
|
||||
populate_meta_tablename_columns_and_pk,
|
||||
sqlalchemy_columns_from_model_fields,
|
||||
)
|
||||
from ormar.models.helpers.validation import populate_choices_validators
|
||||
|
||||
__all__ = [
|
||||
"expand_reverse_relationships",
|
||||
@ -28,4 +32,8 @@ __all__ = [
|
||||
"get_pydantic_field",
|
||||
"get_potential_fields",
|
||||
"get_pydantic_base_orm_config",
|
||||
"check_required_meta_parameters",
|
||||
"sqlalchemy_columns_from_model_fields",
|
||||
"populate_choices_validators",
|
||||
"meta_field_not_set",
|
||||
]
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
import itertools
|
||||
import sqlite3
|
||||
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Tuple, Type
|
||||
from typing import Any, Dict, List, TYPE_CHECKING, Tuple, Type
|
||||
|
||||
from pydantic.typing import ForwardRef
|
||||
import ormar # noqa: I100
|
||||
from ormar.fields.foreign_key import ForeignKeyField
|
||||
from ormar.models.helpers.pydantic import populate_pydantic_default_values
|
||||
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
@ -22,7 +21,7 @@ def is_field_an_forward_ref(field: Type["BaseField"]) -> bool:
|
||||
:return: result of the check
|
||||
:rtype: bool
|
||||
"""
|
||||
return issubclass(field, ForeignKeyField) and (
|
||||
return issubclass(field, ormar.ForeignKeyField) and (
|
||||
field.to.__class__ == ForwardRef or field.through.__class__ == ForwardRef
|
||||
)
|
||||
|
||||
@ -124,43 +123,6 @@ def extract_annotations_and_default_vals(attrs: Dict) -> Tuple[Dict, Dict]:
|
||||
return attrs, model_fields
|
||||
|
||||
|
||||
# cannot be in relations helpers due to cyclical import
|
||||
def validate_related_names_in_relations( # noqa CCR001
|
||||
model_fields: Dict, new_model: Type["Model"]
|
||||
) -> None:
|
||||
"""
|
||||
Performs a validation of relation_names in relation fields.
|
||||
If multiple fields are leading to the same related model
|
||||
only one can have empty related_name param
|
||||
(populated by default as model.name.lower()+'s').
|
||||
Also related_names have to be unique for given related model.
|
||||
|
||||
:raises ModelDefinitionError: if validation of related_names fail
|
||||
:param model_fields: dictionary of declared ormar model fields
|
||||
:type model_fields: Dict[str, ormar.Field]
|
||||
:param new_model:
|
||||
:type new_model: Model class
|
||||
"""
|
||||
already_registered: Dict[str, List[Optional[str]]] = dict()
|
||||
for field in model_fields.values():
|
||||
if issubclass(field, ForeignKeyField):
|
||||
to_name = (
|
||||
field.to.get_name()
|
||||
if not field.to.__class__ == ForwardRef
|
||||
else str(field.to)
|
||||
)
|
||||
previous_related_names = already_registered.setdefault(to_name, [])
|
||||
if field.related_name in previous_related_names:
|
||||
raise ormar.ModelDefinitionError(
|
||||
f"Multiple fields declared on {new_model.get_name(lower=False)} "
|
||||
f"model leading to {field.to.get_name(lower=False)} model without "
|
||||
f"related_name property set. \nThere can be only one relation with "
|
||||
f"default/empty name: '{new_model.get_name() + 's'}'"
|
||||
f"\nTip: provide different related_name for FK and/or M2M fields"
|
||||
)
|
||||
previous_related_names.append(field.related_name)
|
||||
|
||||
|
||||
def group_related_list(list_: List) -> Dict:
|
||||
"""
|
||||
Translates the list of related strings into a dictionary.
|
||||
@ -191,3 +153,19 @@ def group_related_list(list_: List) -> Dict:
|
||||
else:
|
||||
result_dict.setdefault(key, []).extend(new)
|
||||
return {k: v for k, v in sorted(result_dict.items(), key=lambda item: len(item[1]))}
|
||||
|
||||
|
||||
def meta_field_not_set(model: Type["Model"], field_name: str) -> bool:
|
||||
"""
|
||||
Checks if field with given name is already present in model.Meta.
|
||||
Then check if it's set to something truthful
|
||||
(in practice meaning not None, as it's non or ormar Field only).
|
||||
|
||||
:param model: newly constructed model
|
||||
:type model: Model class
|
||||
:param field_name: name of the ormar field
|
||||
:type field_name: str
|
||||
:return: result of the check
|
||||
:rtype: bool
|
||||
"""
|
||||
return not hasattr(model.Meta, field_name) or not getattr(model.Meta, field_name)
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import warnings
|
||||
from typing import Dict, Optional, TYPE_CHECKING, Tuple, Type
|
||||
|
||||
from pydantic import BaseConfig
|
||||
import pydantic
|
||||
from pydantic.fields import ModelField
|
||||
from pydantic.utils import lenient_issubclass
|
||||
|
||||
@ -132,7 +132,7 @@ def populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]:
|
||||
return attrs, model_fields
|
||||
|
||||
|
||||
def get_pydantic_base_orm_config() -> Type[BaseConfig]:
|
||||
def get_pydantic_base_orm_config() -> Type[pydantic.BaseConfig]:
|
||||
"""
|
||||
Returns empty pydantic Config with orm_mode set to True.
|
||||
|
||||
@ -140,7 +140,7 @@ def get_pydantic_base_orm_config() -> Type[BaseConfig]:
|
||||
:rtype: pydantic Config
|
||||
"""
|
||||
|
||||
class Config(BaseConfig):
|
||||
class Config(pydantic.BaseConfig):
|
||||
orm_mode = True
|
||||
|
||||
return Config
|
||||
|
||||
43
ormar/models/helpers/related_names_validation.py
Normal file
43
ormar/models/helpers/related_names_validation.py
Normal file
@ -0,0 +1,43 @@
|
||||
from typing import Dict, List, Optional, TYPE_CHECKING, Type
|
||||
|
||||
from pydantic.typing import ForwardRef
|
||||
import ormar # noqa: I100
|
||||
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
from ormar import Model
|
||||
|
||||
|
||||
def validate_related_names_in_relations( # noqa CCR001
|
||||
model_fields: Dict, new_model: Type["Model"]
|
||||
) -> None:
|
||||
"""
|
||||
Performs a validation of relation_names in relation fields.
|
||||
If multiple fields are leading to the same related model
|
||||
only one can have empty related_name param
|
||||
(populated by default as model.name.lower()+'s').
|
||||
Also related_names have to be unique for given related model.
|
||||
|
||||
:raises ModelDefinitionError: if validation of related_names fail
|
||||
:param model_fields: dictionary of declared ormar model fields
|
||||
:type model_fields: Dict[str, ormar.Field]
|
||||
:param new_model:
|
||||
:type new_model: Model class
|
||||
"""
|
||||
already_registered: Dict[str, List[Optional[str]]] = dict()
|
||||
for field in model_fields.values():
|
||||
if issubclass(field, ormar.ForeignKeyField):
|
||||
to_name = (
|
||||
field.to.get_name()
|
||||
if not field.to.__class__ == ForwardRef
|
||||
else str(field.to)
|
||||
)
|
||||
previous_related_names = already_registered.setdefault(to_name, [])
|
||||
if field.related_name in previous_related_names:
|
||||
raise ormar.ModelDefinitionError(
|
||||
f"Multiple fields declared on {new_model.get_name(lower=False)} "
|
||||
f"model leading to {field.to.get_name(lower=False)} model without "
|
||||
f"related_name property set. \nThere can be only one relation with "
|
||||
f"default/empty name: '{new_model.get_name() + 's'}'"
|
||||
f"\nTip: provide different related_name for FK and/or M2M fields"
|
||||
)
|
||||
previous_related_names.append(field.related_name)
|
||||
@ -3,18 +3,18 @@ from typing import Dict, List, Optional, TYPE_CHECKING, Tuple, Type, Union
|
||||
|
||||
import sqlalchemy
|
||||
|
||||
from ormar import ForeignKey, Integer, ModelDefinitionError # noqa: I202
|
||||
from ormar.fields import BaseField, ManyToManyField
|
||||
from ormar.fields.foreign_key import ForeignKeyField
|
||||
from ormar.models.helpers.models import validate_related_names_in_relations
|
||||
import ormar # noqa: I100, I202
|
||||
from ormar.models.helpers.pydantic import create_pydantic_field
|
||||
from ormar.models.helpers.related_names_validation import (
|
||||
validate_related_names_in_relations,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
from ormar import Model, ModelMeta
|
||||
from ormar import Model, ModelMeta, ManyToManyField, BaseField, ForeignKeyField
|
||||
from ormar.models import NewBaseModel
|
||||
|
||||
|
||||
def adjust_through_many_to_many_model(model_field: Type[ManyToManyField]) -> None:
|
||||
def adjust_through_many_to_many_model(model_field: Type["ManyToManyField"]) -> None:
|
||||
"""
|
||||
Registers m2m relation on through model.
|
||||
Sets ormar.ForeignKey from through model to both child and parent models.
|
||||
@ -27,13 +27,13 @@ def adjust_through_many_to_many_model(model_field: Type[ManyToManyField]) -> Non
|
||||
parent_name = model_field.default_target_field_name()
|
||||
child_name = model_field.default_source_field_name()
|
||||
|
||||
model_field.through.Meta.model_fields[parent_name] = ForeignKey(
|
||||
model_field.through.Meta.model_fields[parent_name] = ormar.ForeignKey(
|
||||
model_field.to,
|
||||
real_name=parent_name,
|
||||
ondelete="CASCADE",
|
||||
owner=model_field.through,
|
||||
)
|
||||
model_field.through.Meta.model_fields[child_name] = ForeignKey(
|
||||
model_field.through.Meta.model_fields[child_name] = ormar.ForeignKey(
|
||||
model_field.owner,
|
||||
real_name=child_name,
|
||||
ondelete="CASCADE",
|
||||
@ -52,7 +52,7 @@ def adjust_through_many_to_many_model(model_field: Type[ManyToManyField]) -> Non
|
||||
|
||||
|
||||
def create_and_append_m2m_fk(
|
||||
model: Type["Model"], model_field: Type[ManyToManyField], field_name: str
|
||||
model: Type["Model"], model_field: Type["ManyToManyField"], field_name: str
|
||||
) -> None:
|
||||
"""
|
||||
Registers sqlalchemy Column with sqlalchemy.ForeignKey leading to the model.
|
||||
@ -69,7 +69,7 @@ def create_and_append_m2m_fk(
|
||||
pk_alias = model.get_column_alias(model.Meta.pkname)
|
||||
pk_column = next((col for col in model.Meta.columns if col.name == pk_alias), None)
|
||||
if pk_column is None: # pragma: no cover
|
||||
raise ModelDefinitionError(
|
||||
raise ormar.ModelDefinitionError(
|
||||
"ManyToMany relation cannot lead to field without pk"
|
||||
)
|
||||
column = sqlalchemy.Column(
|
||||
@ -88,7 +88,7 @@ def create_and_append_m2m_fk(
|
||||
|
||||
|
||||
def check_pk_column_validity(
|
||||
field_name: str, field: BaseField, pkname: Optional[str]
|
||||
field_name: str, field: "BaseField", pkname: Optional[str]
|
||||
) -> Optional[str]:
|
||||
"""
|
||||
Receives the field marked as primary key and verifies if the pkname
|
||||
@ -106,9 +106,9 @@ def check_pk_column_validity(
|
||||
:rtype: str
|
||||
"""
|
||||
if pkname is not None:
|
||||
raise ModelDefinitionError("Only one primary key column is allowed.")
|
||||
raise ormar.ModelDefinitionError("Only one primary key column is allowed.")
|
||||
if field.pydantic_only:
|
||||
raise ModelDefinitionError("Primary key column cannot be pydantic only")
|
||||
raise ormar.ModelDefinitionError("Primary key column cannot be pydantic only")
|
||||
return field_name
|
||||
|
||||
|
||||
@ -144,7 +144,7 @@ def sqlalchemy_columns_from_model_fields(
|
||||
:rtype: Tuple[Optional[str], List[sqlalchemy.Column]]
|
||||
"""
|
||||
if len(model_fields.keys()) == 0:
|
||||
model_fields["id"] = Integer(name="id", primary_key=True)
|
||||
model_fields["id"] = ormar.Integer(name="id", primary_key=True)
|
||||
logging.warning(
|
||||
"Table {table_name} had no fields so auto "
|
||||
"Integer primary key named `id` created."
|
||||
@ -159,7 +159,7 @@ def sqlalchemy_columns_from_model_fields(
|
||||
if (
|
||||
not field.pydantic_only
|
||||
and not field.virtual
|
||||
and not issubclass(field, ManyToManyField)
|
||||
and not issubclass(field, ormar.ManyToManyField)
|
||||
):
|
||||
columns.append(field.get_column(field.get_alias()))
|
||||
return pkname, columns
|
||||
@ -201,7 +201,7 @@ def populate_meta_tablename_columns_and_pk(
|
||||
)
|
||||
|
||||
if pkname is None:
|
||||
raise ModelDefinitionError("Table has to have a primary key.")
|
||||
raise ormar.ModelDefinitionError("Table has to have a primary key.")
|
||||
|
||||
new_model.Meta.columns = columns
|
||||
new_model.Meta.pkname = pkname
|
||||
@ -248,7 +248,7 @@ def populate_meta_sqlalchemy_table_if_required(meta: "ModelMeta") -> None:
|
||||
|
||||
|
||||
def update_column_definition(
|
||||
model: Union[Type["Model"], Type["NewBaseModel"]], field: Type[ForeignKeyField]
|
||||
model: Union[Type["Model"], Type["NewBaseModel"]], field: Type["ForeignKeyField"]
|
||||
) -> None:
|
||||
"""
|
||||
Updates a column with a new type column based on updated parameters in FK fields.
|
||||
|
||||
161
ormar/models/helpers/validation.py
Normal file
161
ormar/models/helpers/validation.py
Normal file
@ -0,0 +1,161 @@
|
||||
import datetime
|
||||
import decimal
|
||||
import uuid
|
||||
from enum import Enum
|
||||
from typing import Any, Dict, List, TYPE_CHECKING, Tuple, Type
|
||||
|
||||
try:
|
||||
import orjson as json
|
||||
except ImportError: # pragma: no cover
|
||||
import json # type: ignore
|
||||
|
||||
import pydantic
|
||||
from pydantic.main import SchemaExtraCallable
|
||||
|
||||
import ormar # noqa: I100, I202
|
||||
from ormar.fields import BaseField
|
||||
from ormar.models.helpers.models import meta_field_not_set
|
||||
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
from ormar import Model
|
||||
|
||||
|
||||
def check_if_field_has_choices(field: Type[BaseField]) -> bool:
|
||||
"""
|
||||
Checks if given field has choices populated.
|
||||
A if it has one, a validator for this field needs to be attached.
|
||||
|
||||
:param field: ormar field to check
|
||||
:type field: BaseField
|
||||
:return: result of the check
|
||||
:rtype: bool
|
||||
"""
|
||||
return hasattr(field, "choices") and bool(field.choices)
|
||||
|
||||
|
||||
def convert_choices_if_needed( # noqa: CCR001
|
||||
field: Type["BaseField"], value: Any
|
||||
) -> Tuple[Any, List]:
|
||||
"""
|
||||
Converts dates to isoformat as fastapi can check this condition in routes
|
||||
and the fields are not yet parsed.
|
||||
|
||||
Converts enums to list of it's values.
|
||||
|
||||
Converts uuids to strings.
|
||||
|
||||
Converts decimal to float with given scale.
|
||||
|
||||
:param field: ormar field to check with choices
|
||||
:type field: Type[BaseField]
|
||||
:param values: current values of the model to verify
|
||||
:type values: Dict
|
||||
:return: value, choices list
|
||||
:rtype: Tuple[Any, List]
|
||||
"""
|
||||
choices = [o.value if isinstance(o, Enum) else o for o in field.choices]
|
||||
|
||||
if field.__type__ in [datetime.datetime, datetime.date, datetime.time]:
|
||||
value = value.isoformat() if not isinstance(value, str) else value
|
||||
choices = [o.isoformat() for o in field.choices]
|
||||
elif field.__type__ == pydantic.Json:
|
||||
value = json.dumps(value) if not isinstance(value, str) else value
|
||||
value = value.decode("utf-8") if isinstance(value, bytes) else value
|
||||
elif field.__type__ == uuid.UUID:
|
||||
value = str(value) if not isinstance(value, str) else value
|
||||
choices = [str(o) for o in field.choices]
|
||||
elif field.__type__ == decimal.Decimal:
|
||||
precision = field.scale # type: ignore
|
||||
value = (
|
||||
round(float(value), precision)
|
||||
if isinstance(value, decimal.Decimal)
|
||||
else value
|
||||
)
|
||||
choices = [round(float(o), precision) for o in choices]
|
||||
|
||||
return value, choices
|
||||
|
||||
|
||||
def validate_choices(field: Type["BaseField"], value: Any) -> None:
|
||||
"""
|
||||
Validates if given value is in provided choices.
|
||||
|
||||
:raises ValueError: If value is not in choices.
|
||||
:param field:field to validate
|
||||
:type field: Type[BaseField]
|
||||
:param value: value of the field
|
||||
:type value: Any
|
||||
"""
|
||||
value, choices = convert_choices_if_needed(field=field, value=value)
|
||||
if value is not ormar.Undefined and value not in choices:
|
||||
raise ValueError(
|
||||
f"{field.name}: '{value}' " f"not in allowed choices set:" f" {choices}"
|
||||
)
|
||||
|
||||
|
||||
def choices_validator(cls: Type["Model"], values: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Validator that is attached to pydantic model pre root validators.
|
||||
Validator checks if field value is in field.choices list.
|
||||
|
||||
:raises ValueError: if field value is outside of allowed choices.
|
||||
:param cls: constructed class
|
||||
:type cls: Model class
|
||||
:param values: dictionary of field values (pydantic side)
|
||||
:type values: Dict[str, Any]
|
||||
:return: values if pass validation, otherwise exception is raised
|
||||
:rtype: Dict[str, Any]
|
||||
"""
|
||||
for field_name, field in cls.Meta.model_fields.items():
|
||||
if check_if_field_has_choices(field):
|
||||
value = values.get(field_name, ormar.Undefined)
|
||||
validate_choices(field=field, value=value)
|
||||
return values
|
||||
|
||||
|
||||
def construct_modify_schema_function(fields_with_choices: List) -> SchemaExtraCallable:
|
||||
"""
|
||||
Modifies the schema to include fields with choices validator.
|
||||
Those fields will be displayed in schema as Enum types with available choices
|
||||
values listed next to them.
|
||||
|
||||
:param fields_with_choices: list of fields with choices validation
|
||||
:type fields_with_choices: List
|
||||
:return: callable that will be run by pydantic to modify the schema
|
||||
:rtype: Callable
|
||||
"""
|
||||
|
||||
def schema_extra(schema: Dict[str, Any], model: Type["Model"]) -> None:
|
||||
for field_id, prop in schema.get("properties", {}).items():
|
||||
if field_id in fields_with_choices:
|
||||
prop["enum"] = list(model.Meta.model_fields[field_id].choices)
|
||||
prop["description"] = prop.get("description", "") + "An enumeration."
|
||||
|
||||
return staticmethod(schema_extra) # type: ignore
|
||||
|
||||
|
||||
def populate_choices_validators(model: Type["Model"]) -> None: # noqa CCR001
|
||||
"""
|
||||
Checks if Model has any fields with choices set.
|
||||
If yes it adds choices validation into pre root validators.
|
||||
|
||||
:param model: newly constructed Model
|
||||
:type model: Model class
|
||||
"""
|
||||
fields_with_choices = []
|
||||
if not meta_field_not_set(model=model, field_name="model_fields"):
|
||||
for name, field in model.Meta.model_fields.items():
|
||||
if check_if_field_has_choices(field):
|
||||
fields_with_choices.append(name)
|
||||
validators = getattr(model, "__pre_root_validators__", [])
|
||||
if choices_validator not in validators:
|
||||
validators.append(choices_validator)
|
||||
model.__pre_root_validators__ = validators
|
||||
if not model._choices_fields:
|
||||
model._choices_fields = set()
|
||||
model._choices_fields.add(name)
|
||||
|
||||
if fields_with_choices:
|
||||
model.Config.schema_extra = construct_modify_schema_function(
|
||||
fields_with_choices=fields_with_choices
|
||||
)
|
||||
Reference in New Issue
Block a user