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
@ -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,19 +346,22 @@ 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):
@ -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(
@ -257,7 +260,7 @@ class NewBaseModel(
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
@ -312,6 +315,8 @@ class NewBaseModel(
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),

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