Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lmignon committed Jul 17, 2023
1 parent 320e94c commit 44f3b92
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions base_rest_demo/pydantic_models/country_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class CountryInfo(NaiveOrmModel):

id: int
name: str
id: int = None
name: str = None
21 changes: 17 additions & 4 deletions base_rest_demo/pydantic_models/naive_orm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
from odoo.addons.pydantic import utils

from pydantic import BaseModel
from pydantic.version import VERSION as PYDANTIC_VERSION

PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")

class NaiveOrmModel(BaseModel, metaclass=ExtendableModelMeta):
class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter
if PYDANTIC_V2:

from pydantic import ConfigDict

class NaiveOrmModel(BaseModel, metaclass=ExtendableModelMeta):
model_config: ConfigDict = ConfigDict(
from_attributes=True, getter_dict=utils.GenericOdooGetter
)

else:

class NaiveOrmModel(BaseModel, metaclass=ExtendableModelMeta):
class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter
4 changes: 2 additions & 2 deletions base_rest_demo/pydantic_models/partner_short_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class PartnerShortInfo(NaiveOrmModel):

id: int
name: str
id: int = None
name: str = None
4 changes: 2 additions & 2 deletions base_rest_demo/pydantic_models/state_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class StateInfo(NaiveOrmModel):

id: int
name: str
id: int = None
name: str = None
8 changes: 5 additions & 3 deletions base_rest_pydantic/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ def from_params(self, service, params):
def to_response(self, service, result):
# do we really need to validate the instance????
json_dict = result.dict()
to_validate = (
json_dict if not result.__config__.orm_mode else result.dict(by_alias=True)
)
if PYDANTIC_V2:
orm_mode = result.model_config.get("from_attributes", None)
else:
orm_mode = result.__config__.orm_mode
to_validate = json_dict if orm_mode else result.dict(by_alias=True)
*_, validation_error = validate_model(self._model_cls, to_validate)
if validation_error:
raise SystemError(_("Invalid Response %s") % validation_error)
Expand Down
2 changes: 1 addition & 1 deletion base_rest_pydantic/tests/test_from_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def setUp(self):
super(TestPydantic, self).setUp()

class Model1(BaseModel):
name: str
name: str = None
description: str = None

self.Model1: BaseModel = Model1
Expand Down
4 changes: 2 additions & 2 deletions base_rest_pydantic/tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def _to_response_list(self, instance: List[BaseModel]):

def test_to_response(self):
class Model1(BaseModel):
name: str
name: str = None

instance = Model1(name="Instance 1")
res = self._to_response(instance)
self.assertEqual(res["name"], instance.name)

def test_to_response_list(self):
class Model1(BaseModel):
name: str
name: str = None

instances = (Model1(name="Instance 1"), Model1(name="Instance 2"))
res = self._to_response_list(instances)
Expand Down
6 changes: 3 additions & 3 deletions fastapi_auth_jwt_demo/routers/auth_jwt_demo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@


class TestData(BaseModel):
name: Union[str, None]
email: Union[str, None]
uid: int
name: Union[str, None] = None
email: Union[str, None] = None
uid: int = None


router = APIRouter()
Expand Down

0 comments on commit 44f3b92

Please sign in to comment.