revert adding props to fields

This commit is contained in:
collerek
2020-12-03 16:39:14 +01:00
parent b838fa1edf
commit 3b164c76de
5 changed files with 121 additions and 106 deletions

View File

@ -1,3 +1,4 @@
import inspect
import logging import logging
import warnings import warnings
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Tuple, Type, Union from typing import Any, Dict, List, Optional, TYPE_CHECKING, Tuple, Type, Union
@ -47,7 +48,7 @@ def register_relation_on_build(table_name: str, field: Type[ForeignKeyField]) ->
def register_many_to_many_relation_on_build( def register_many_to_many_relation_on_build(
table_name: str, field: Type[ManyToManyField] table_name: str, field: Type[ManyToManyField]
) -> None: ) -> None:
alias_manager.add_relation_type(field.through.Meta.tablename, table_name) alias_manager.add_relation_type(field.through.Meta.tablename, table_name)
alias_manager.add_relation_type( alias_manager.add_relation_type(
@ -56,11 +57,11 @@ def register_many_to_many_relation_on_build(
def reverse_field_not_already_registered( def reverse_field_not_already_registered(
child: Type["Model"], child_model_name: str, parent_model: Type["Model"] child: Type["Model"], child_model_name: str, parent_model: Type["Model"]
) -> bool: ) -> bool:
return ( return (
child_model_name not in parent_model.__fields__ child_model_name not in parent_model.__fields__
and child.get_name() not in parent_model.__fields__ and child.get_name() not in parent_model.__fields__
) )
@ -71,7 +72,7 @@ def expand_reverse_relationships(model: Type["Model"]) -> None:
parent_model = model_field.to parent_model = model_field.to
child = model child = model
if reverse_field_not_already_registered( if reverse_field_not_already_registered(
child, child_model_name, parent_model child, child_model_name, parent_model
): ):
register_reverse_model_fields( register_reverse_model_fields(
parent_model, child, child_model_name, model_field parent_model, child, child_model_name, model_field
@ -79,10 +80,10 @@ def expand_reverse_relationships(model: Type["Model"]) -> None:
def register_reverse_model_fields( def register_reverse_model_fields(
model: Type["Model"], model: Type["Model"],
child: Type["Model"], child: Type["Model"],
child_model_name: str, child_model_name: str,
model_field: Type["ForeignKeyField"], model_field: Type["ForeignKeyField"],
) -> None: ) -> None:
if issubclass(model_field, ManyToManyField): if issubclass(model_field, ManyToManyField):
model.Meta.model_fields[child_model_name] = ManyToMany( model.Meta.model_fields[child_model_name] = ManyToMany(
@ -97,7 +98,7 @@ def register_reverse_model_fields(
def adjust_through_many_to_many_model( def adjust_through_many_to_many_model(
model: Type["Model"], child: Type["Model"], model_field: Type[ManyToManyField] model: Type["Model"], child: Type["Model"], model_field: Type[ManyToManyField]
) -> None: ) -> None:
model_field.through.Meta.model_fields[model.get_name()] = ForeignKey( model_field.through.Meta.model_fields[model.get_name()] = ForeignKey(
model, real_name=model.get_name(), ondelete="CASCADE" model, real_name=model.get_name(), ondelete="CASCADE"
@ -114,7 +115,7 @@ def adjust_through_many_to_many_model(
def create_pydantic_field( def create_pydantic_field(
field_name: str, model: Type["Model"], model_field: Type[ManyToManyField] field_name: str, model: Type["Model"], model_field: Type[ManyToManyField]
) -> None: ) -> None:
model_field.through.__fields__[field_name] = ModelField( model_field.through.__fields__[field_name] = ModelField(
name=field_name, name=field_name,
@ -136,7 +137,7 @@ def get_pydantic_field(field_name: str, model: Type["Model"]) -> "ModelField":
def create_and_append_m2m_fk( def create_and_append_m2m_fk(
model: Type["Model"], model_field: Type[ManyToManyField] model: Type["Model"], model_field: Type[ManyToManyField]
) -> None: ) -> None:
column = sqlalchemy.Column( column = sqlalchemy.Column(
model.get_name(), model.get_name(),
@ -152,7 +153,7 @@ def create_and_append_m2m_fk(
def check_pk_column_validity( def check_pk_column_validity(
field_name: str, field: BaseField, pkname: Optional[str] field_name: str, field: BaseField, pkname: Optional[str]
) -> Optional[str]: ) -> Optional[str]:
if pkname is not None: if pkname is not None:
raise ModelDefinitionError("Only one primary key column is allowed.") raise ModelDefinitionError("Only one primary key column is allowed.")
@ -162,7 +163,7 @@ def check_pk_column_validity(
def sqlalchemy_columns_from_model_fields( def sqlalchemy_columns_from_model_fields(
model_fields: Dict, table_name: str model_fields: Dict, table_name: str
) -> Tuple[Optional[str], List[sqlalchemy.Column]]: ) -> Tuple[Optional[str], List[sqlalchemy.Column]]:
columns = [] columns = []
pkname = None pkname = None
@ -176,9 +177,9 @@ def sqlalchemy_columns_from_model_fields(
if field.primary_key: if field.primary_key:
pkname = check_pk_column_validity(field_name, field, pkname) pkname = check_pk_column_validity(field_name, field, pkname)
if ( if (
not field.pydantic_only not field.pydantic_only
and not field.virtual and not field.virtual
and not issubclass(field, ManyToManyField) and not issubclass(field, ManyToManyField)
): ):
columns.append(field.get_column(field.get_alias())) columns.append(field.get_column(field.get_alias()))
register_relation_in_alias_manager(table_name, field) register_relation_in_alias_manager(table_name, field)
@ -186,7 +187,7 @@ def sqlalchemy_columns_from_model_fields(
def register_relation_in_alias_manager( def register_relation_in_alias_manager(
table_name: str, field: Type[ForeignKeyField] table_name: str, field: Type[ForeignKeyField]
) -> None: ) -> None:
if issubclass(field, ManyToManyField): if issubclass(field, ManyToManyField):
register_many_to_many_relation_on_build(table_name, field) register_many_to_many_relation_on_build(table_name, field)
@ -195,7 +196,7 @@ def register_relation_in_alias_manager(
def populate_default_pydantic_field_value( def populate_default_pydantic_field_value(
ormar_field: Type[BaseField], field_name: str, attrs: dict ormar_field: Type[BaseField], field_name: str, attrs: dict
) -> dict: ) -> dict:
curr_def_value = attrs.get(field_name, ormar.Undefined) curr_def_value = attrs.get(field_name, ormar.Undefined)
if lenient_issubclass(curr_def_value, ormar.fields.BaseField): if lenient_issubclass(curr_def_value, ormar.fields.BaseField):
@ -242,7 +243,7 @@ def extract_annotations_and_default_vals(attrs: dict) -> Tuple[Dict, Dict]:
def populate_meta_tablename_columns_and_pk( def populate_meta_tablename_columns_and_pk(
name: str, new_model: Type["Model"] name: str, new_model: Type["Model"]
) -> Type["Model"]: ) -> Type["Model"]:
tablename = name.lower() + "s" tablename = name.lower() + "s"
new_model.Meta.tablename = ( new_model.Meta.tablename = (
@ -268,7 +269,7 @@ def populate_meta_tablename_columns_and_pk(
def populate_meta_sqlalchemy_table_if_required( def populate_meta_sqlalchemy_table_if_required(
new_model: Type["Model"], new_model: Type["Model"],
) -> Type["Model"]: ) -> Type["Model"]:
if not hasattr(new_model.Meta, "table"): if not hasattr(new_model.Meta, "table"):
new_model.Meta.table = sqlalchemy.Table( new_model.Meta.table = sqlalchemy.Table(
@ -320,7 +321,7 @@ def populate_choices_validators(model: Type["Model"]) -> None: # noqa CCR001
def populate_default_options_values( def populate_default_options_values(
new_model: Type["Model"], model_fields: Dict new_model: Type["Model"], model_fields: Dict
) -> None: ) -> None:
if not hasattr(new_model.Meta, "constraints"): if not hasattr(new_model.Meta, "constraints"):
new_model.Meta.constraints = [] new_model.Meta.constraints = []
@ -335,10 +336,9 @@ def populate_default_options_values(
def add_cached_properties(new_model: Type["Model"]) -> None: def add_cached_properties(new_model: Type["Model"]) -> None:
new_model._props = { new_model._props = {
prop prop[0]
for prop in vars(new_model) for prop in inspect.getmembers(new_model, lambda o: isinstance(o, property))
if isinstance(getattr(new_model, prop), property) if prop[0] not in ("__values__", "__fields__", "fields", "pk_column", "saved")
and prop not in ("__values__", "__fields__", "fields", "pk_column", "saved")
} }
new_model._quick_access_fields = quick_access_set new_model._quick_access_fields = quick_access_set
new_model._related_names = None new_model._related_names = None
@ -346,24 +346,27 @@ def add_cached_properties(new_model: Type["Model"]) -> None:
def add_property_fields(new_model: Type["Model"]) -> None: def add_property_fields(new_model: Type["Model"]) -> None:
if new_model.Meta.include_props_in_fields: pass
for prop in new_model._props:
field_type = getattr(new_model, prop).fget.__annotations__.get("return")
new_model.Meta.model_fields[prop] = ModelFieldFactory( # type: ignore # if new_model.Meta.include_props_in_fields:
nullable=True, pydantic_only=True # for prop in new_model._props:
) # field_type = getattr(new_model, prop).fget.__annotations__.get("return")
new_model.__fields__[prop] = ModelField( # new_model.Meta.model_fields[prop] = ModelFieldFactory( # type: ignore
name=prop, # nullable=True, pydantic_only=True
type_=Optional[field_type] if field_type is not None else Any, # type: ignore # )
model_config=new_model.__config__, # new_model.__fields__[prop] = ModelField(
required=False, # name=prop,
class_validators={}, # type_=Optional[field_type] if field_type is not None else Any, # type: ignore
) # model_config=new_model.__config__,
# required=False,
# class_validators={},
# )
class ModelMetaclass(pydantic.main.ModelMetaclass): class ModelMetaclass(pydantic.main.ModelMetaclass):
def __new__( # type: ignore def __new__( # type: ignore
mcs: "ModelMetaclass", name: str, bases: Any, attrs: dict mcs: "ModelMetaclass", name: str, bases: Any, attrs: dict
) -> "ModelMetaclass": ) -> "ModelMetaclass":
attrs["Config"] = get_pydantic_base_orm_config() attrs["Config"] = get_pydantic_base_orm_config()
attrs["__name__"] = name attrs["__name__"] = name
@ -371,6 +374,7 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
new_model = super().__new__( # type: ignore new_model = super().__new__( # type: ignore
mcs, name, bases, attrs mcs, name, bases, attrs
) )
add_cached_properties(new_model)
if hasattr(new_model, "Meta"): if hasattr(new_model, "Meta"):
populate_default_options_values(new_model, model_fields) populate_default_options_values(new_model, model_fields)
@ -387,7 +391,6 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
) )
new_model.Meta.alias_manager = alias_manager new_model.Meta.alias_manager = alias_manager
new_model.objects = QuerySet(new_model) new_model.objects = QuerySet(new_model)
add_cached_properties(new_model)
add_property_fields(new_model) add_property_fields(new_model)
return new_model return new_model

View File

@ -20,7 +20,6 @@ from typing import (
from ormar.exceptions import ModelPersistenceError, RelationshipInstanceError from ormar.exceptions import ModelPersistenceError, RelationshipInstanceError
from ormar.queryset.utils import translate_list_to_dict, update from ormar.queryset.utils import translate_list_to_dict, update
import ormar # noqa: I100 import ormar # noqa: I100
from ormar.fields import BaseField from ormar.fields import BaseField
from ormar.fields.foreign_key import ForeignKeyField from ormar.fields.foreign_key import ForeignKeyField
@ -46,13 +45,11 @@ class ModelTableProxy:
pk: Any pk: Any
get_name: Callable get_name: Callable
_props: Set _props: Set
dict: Callable
def dict(self): # noqa A003
raise NotImplementedError # pragma no cover
def _extract_own_model_fields(self) -> Dict: def _extract_own_model_fields(self) -> Dict:
related_names = self.extract_related_names() related_names = self.extract_related_names()
self_fields = {k: v for k, v in self.dict().items() if k not in related_names} self_fields = self.dict(exclude=related_names)
return self_fields return self_fields
@classmethod @classmethod

View File

@ -1,3 +1,4 @@
import inspect
import json import json
import uuid import uuid
from typing import ( from typing import (
@ -74,7 +75,7 @@ class NewBaseModel(
# noinspection PyMissingConstructor # noinspection PyMissingConstructor
def __init__(self, *args: Any, **kwargs: Any) -> None: # type: ignore def __init__(self, *args: Any, **kwargs: Any) -> None: # type: ignore
caller_name = inspect.currentframe().f_back.f_code.co_name
object.__setattr__(self, "_orm_id", uuid.uuid4().hex) object.__setattr__(self, "_orm_id", uuid.uuid4().hex)
object.__setattr__(self, "_orm_saved", False) object.__setattr__(self, "_orm_saved", False)
object.__setattr__( object.__setattr__(
@ -96,6 +97,8 @@ class NewBaseModel(
if "pk" in kwargs: if "pk" in kwargs:
kwargs[self.Meta.pkname] = kwargs.pop("pk") kwargs[self.Meta.pkname] = kwargs.pop("pk")
# build the models to set them and validate but don't register # build the models to set them and validate but don't register
if self.Meta.include_props_in_dict:
kwargs = {k: v for k, v in kwargs.items() if k not in object.__getattribute__(self, '_props')}
try: try:
new_kwargs: Dict[str, Any] = { new_kwargs: Dict[str, Any] = {
k: self._convert_json( k: self._convert_json(
@ -174,7 +177,7 @@ class NewBaseModel(
return object.__getattribute__(self, item) # pragma: no cover return object.__getattribute__(self, item) # pragma: no cover
def _extract_related_model_instead_of_field( def _extract_related_model_instead_of_field(
self, item: str self, item: str
) -> Optional[Union["T", Sequence["T"]]]: ) -> Optional[Union["T", Sequence["T"]]]:
if item in self._orm: if item in self._orm:
return self._orm.get(item) return self._orm.get(item)
@ -187,9 +190,9 @@ class NewBaseModel(
def __same__(self, other: "NewBaseModel") -> bool: def __same__(self, other: "NewBaseModel") -> bool:
return ( return (
self._orm_id == other._orm_id self._orm_id == other._orm_id
or (self.pk == other.pk and self.pk is not None) or (self.pk == other.pk and self.pk is not None)
or self.dict() == other.dict() or self.dict() == other.dict()
) )
@classmethod @classmethod
@ -223,7 +226,7 @@ class NewBaseModel(
@classmethod @classmethod
def get_properties( def get_properties(
cls, include: Union[Set, Dict, None], exclude: Union[Set, Dict, None] cls, include: Union[Set, Dict, None], exclude: Union[Set, Dict, None]
) -> Set[str]: ) -> Set[str]:
props = cls._props props = cls._props
@ -234,7 +237,7 @@ class NewBaseModel(
return props return props
def _get_related_not_excluded_fields( def _get_related_not_excluded_fields(
self, include: Optional[Dict], exclude: Optional[Dict], self, include: Optional[Dict], exclude: Optional[Dict],
) -> List: ) -> List:
fields = [field for field in self.extract_related_names()] fields = [field for field in self.extract_related_names()]
if include: if include:
@ -249,15 +252,15 @@ class NewBaseModel(
@staticmethod @staticmethod
def _extract_nested_models_from_list( def _extract_nested_models_from_list(
models: MutableSequence, models: MutableSequence,
include: Union[Set, Dict, None], include: Union[Set, Dict, None],
exclude: Union[Set, Dict, None], exclude: Union[Set, Dict, None],
) -> List: ) -> List:
result = [] result = []
for model in models: for model in models:
try: try:
result.append( result.append(
model.dict(nested=True, include=include, exclude=exclude,) model.dict(nested=True, include=include, exclude=exclude, )
) )
except ReferenceError: # pragma no cover except ReferenceError: # pragma no cover
continue continue
@ -265,17 +268,17 @@ class NewBaseModel(
@staticmethod @staticmethod
def _skip_ellipsis( def _skip_ellipsis(
items: Union[Set, Dict, None], key: str items: Union[Set, Dict, None], key: str
) -> Union[Set, Dict, None]: ) -> Union[Set, Dict, None]:
result = Excludable.get_child(items, key) result = Excludable.get_child(items, key)
return result if result is not Ellipsis else None return result if result is not Ellipsis else None
def _extract_nested_models( # noqa: CCR001 def _extract_nested_models( # noqa: CCR001
self, self,
nested: bool, nested: bool,
dict_instance: Dict, dict_instance: Dict,
include: Optional[Dict], include: Optional[Dict],
exclude: Optional[Dict], exclude: Optional[Dict],
) -> Dict: ) -> Dict:
fields = self._get_related_not_excluded_fields(include=include, exclude=exclude) fields = self._get_related_not_excluded_fields(include=include, exclude=exclude)
@ -301,17 +304,19 @@ class NewBaseModel(
return dict_instance return dict_instance
def dict( # type: ignore # noqa A003 def dict( # type: ignore # noqa A003
self, self,
*, *,
include: Union[Set, Dict] = None, include: Union[Set, Dict] = None,
exclude: Union[Set, Dict] = None, exclude: Union[Set, Dict] = None,
by_alias: bool = False, by_alias: bool = False,
skip_defaults: bool = None, skip_defaults: bool = None,
exclude_unset: bool = False, exclude_unset: bool = False,
exclude_defaults: bool = False, exclude_defaults: bool = False,
exclude_none: bool = False, exclude_none: bool = False,
nested: bool = False, nested: bool = False,
) -> "DictStrAny": # noqa: A003' ) -> "DictStrAny": # noqa: A003'
# callable_name = inspect.currentframe().f_back.f_code.co_name
# print('dict', callable_name)
dict_instance = super().dict( dict_instance = super().dict(
include=include, include=include,
exclude=self._update_excluded_with_related_not_required(exclude, nested), exclude=self._update_excluded_with_related_not_required(exclude, nested),
@ -367,6 +372,6 @@ class NewBaseModel(
def _is_conversion_to_json_needed(self, column_name: str) -> bool: def _is_conversion_to_json_needed(self, column_name: str) -> bool:
return ( return (
column_name in self.Meta.model_fields column_name in self.Meta.model_fields
and self.Meta.model_fields[column_name].__type__ == pydantic.Json and self.Meta.model_fields[column_name].__type__ == pydantic.Json
) )

View File

@ -64,7 +64,7 @@ class RandomModel(ormar.Model):
metadata = metadata metadata = metadata
database = database database = database
include_props_in_fields = True include_props_in_dict = True
id: int = ormar.Integer(primary_key=True) id: int = ormar.Integer(primary_key=True)
password: str = ormar.String(max_length=255, default=gen_pass) password: str = ormar.String(max_length=255, default=gen_pass)
@ -139,7 +139,9 @@ async def create_user4(user: User2):
@app.post("/random/", response_model=RandomModel) @app.post("/random/", response_model=RandomModel)
async def create_user5(user: RandomModel): async def create_user5(user: RandomModel):
return await user.save() user = await user.save()
print('returning')
return user
@app.post("/random2/", response_model=RandomModel) @app.post("/random2/", response_model=RandomModel)
@ -148,7 +150,7 @@ async def create_user6(user: RandomModel):
return user.dict() return user.dict()
def test_all_endpoints(): def test_excluding_fields_in_endpoints():
client = TestClient(app) client = TestClient(app)
with client as client: with client as client:
user = { user = {
@ -168,20 +170,24 @@ def test_all_endpoints():
"last_name": "Doe", "last_name": "Doe",
} }
print('before call')
response = client.post("/users/", json=user2) response = client.post("/users/", json=user2)
created_user = User(**response.json()) created_user = User(**response.json())
assert created_user.pk is not None assert created_user.pk is not None
assert created_user.password is None assert created_user.password is None
print('before call')
response = client.post("/users2/", json=user) response = client.post("/users2/", json=user)
created_user2 = User(**response.json()) created_user2 = User(**response.json())
assert created_user2.pk is not None assert created_user2.pk is not None
assert created_user2.password is None assert created_user2.password is None
# response has only 3 fields from UserBase # response has only 3 fields from UserBase
print('before call')
response = client.post("/users3/", json=user) response = client.post("/users3/", json=user)
assert list(response.json().keys()) == ["email", "first_name", "last_name"] assert list(response.json().keys()) == ["email", "first_name", "last_name"]
print('before call')
response = client.post("/users4/", json=user) response = client.post("/users4/", json=user)
assert list(response.json().keys()) == [ assert list(response.json().keys()) == [
"id", "id",
@ -191,32 +197,40 @@ def test_all_endpoints():
"category", "category",
] ]
user3 = {"last_name": "Test"} # user3 = {"last_name": "Test"}
response = client.post("/random/", json=user3) # print('before call')
assert list(response.json().keys()) == [ # response = client.post("/random/", json=user3)
"id", # assert list(response.json().keys()) == [
"password", # "id",
"first_name", # "password",
"last_name", # "first_name",
"created_date", # "last_name",
"full_name", # "created_date",
] # "full_name",
assert response.json().get("full_name") == "John Test" # ]
# assert response.json().get("full_name") == "John Test"
#
# RandomModel.Meta.include_props_in_fields = False
# user3 = {"last_name": "Test"}
# print('before call')
# response = client.post("/random/", json=user3)
# assert list(response.json().keys()) == [
# "id",
# "password",
# "first_name",
# "last_name",
# "created_date",
# "full_name",
# ]
# assert response.json().get("full_name") == "John Test"
RandomModel.Meta.include_props_in_fields = False
user3 = {"last_name": "Test"}
response = client.post("/random/", json=user3)
assert list(response.json().keys()) == [
"id",
"password",
"first_name",
"last_name",
"created_date",
"full_name",
]
def test_adding_fields_in_endpoints():
client = TestClient(app)
with client as client:
RandomModel.Meta.include_props_in_dict = True RandomModel.Meta.include_props_in_dict = True
user3 = {"last_name": "Test"} user3 = {"last_name": "Test"}
print('before call')
response = client.post("/random2/", json=user3) response = client.post("/random2/", json=user3)
assert list(response.json().keys()) == [ assert list(response.json().keys()) == [
"id", "id",
@ -226,3 +240,4 @@ def test_all_endpoints():
"created_date", "created_date",
"full_name", "full_name",
] ]
assert response.json().get("full_name") == "John Test"

View File

@ -28,10 +28,6 @@ class Album(ormar.Model):
def name10(self) -> str: def name10(self) -> str:
return self.name + "_10" return self.name + "_10"
@validator("name")
def test(cls, v):
return v
@pytest.fixture(autouse=True, scope="module") @pytest.fixture(autouse=True, scope="module")
def create_test_database(): def create_test_database():
@ -71,5 +67,4 @@ async def test_pydantic_only_fields():
test_dict = album.dict() test_dict = album.dict()
assert "timestamp" in test_dict assert "timestamp" in test_dict
assert test_dict["timestamp"] is not None assert test_dict["timestamp"] is not None
# key is still there as now it's a field assert test_dict.get("name10", 'aa') == 'aa'
assert test_dict["name10"] is None