diff --git a/fastapi/error_handlers.py b/fastapi/error_handlers.py index 2e4202d6b..192ef4064 100644 --- a/fastapi/error_handlers.py +++ b/fastapi/error_handlers.py @@ -29,7 +29,7 @@ def convert_exception_to_status_body(exc: Exception) -> tuple[int, dict]: status_code = exc.status_code details = exc.detail elif isinstance(exc, RequestValidationError): - status_code = status.HTTP_422_UNPROCESSABLE_ENTITY + status_code = status.HTTP_422_UNPROCESSABLE_CONTENT details = jsonable_encoder(exc.errors()) elif isinstance(exc, WebSocketRequestValidationError): status_code = status.WS_1008_POLICY_VIOLATION diff --git a/fastapi/tests/test_fastapi.py b/fastapi/tests/test_fastapi.py index fbda932fb..c9eae09b8 100644 --- a/fastapi/tests/test_fastapi.py +++ b/fastapi/tests/test_fastapi.py @@ -156,7 +156,9 @@ def test_request_validation_error(self) -> None: route = "/fastapi_demo/demo/exception?exception_type=BAD&error_message=" response = self.url_open(route, timeout=200) mocked_commit.assert_not_called() - self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) + self.assertEqual( + response.status_code, status.HTTP_422_UNPROCESSABLE_CONTENT + ) def test_no_commit_on_exception(self) -> None: # this test check that the way we mock the cursor is working as expected diff --git a/pydantic/README.rst b/pydantic/README.rst index cddfbb432..4eb22b6a3 100644 --- a/pydantic/README.rst +++ b/pydantic/README.rst @@ -1,7 +1,3 @@ -.. image:: https://odoo-community.org/readme-banner-image - :target: https://odoo-community.org/get-involved?utm_source=readme - :alt: Odoo Community Association - ======== Pydantic ======== @@ -11,13 +7,13 @@ Pydantic !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:bf754ec770116cffb9eee07a08eb62409c1868dfd0f765c017156da1d8242df5 + !! source digest: sha256:0282f433b416c240729625a685a03a9775a264b4628001c237941a3fe506c13b !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png :target: https://odoo-community.org/page/development-status :alt: Beta -.. |badge2| image:: https://img.shields.io/badge/license-LGPL--3-blue.png +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html :alt: License: LGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github @@ -33,13 +29,13 @@ Pydantic |badge1| |badge2| |badge3| |badge4| |badge5| This addon provides a utility method that can be used to map odoo record -to a `Pydantic model `__. +to a `Pydantic model (>= v2) `__. If you need to make your Pydantic models extendable at runtime, takes a look at the python package `extendable-pydantic `__ -and the odoo addon -`extendable `__ +and the `odoo addon +extendable `__ **Table of contents** @@ -51,36 +47,28 @@ Usage To support pydantic models that map to Odoo models, Pydantic model instances can be created from arbitrary odoo model instances by mapping -fields from odoo models to fields defined by the pydantic model. To ease -the mapping, the addon provide a utility class -odoo.addons.pydantic.utils.GenericOdooGetter. +fields from odoo models to fields defined by the pydantic model. + +To ease the mapping, the addon provide an utility class (using +``pydantic>2.0``) ``odoo.addons.pydantic.utils.PydanticOdooBaseModel``: .. code:: python - import pydantic - from odoo.addons.pydantic import utils + from odoo.addons.pydantic.utils import PydanticOdooBaseModel - class Group(pydantic.BaseModel): - name: str - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter + class Group(PydanticOdooBaseModel): + name: str - class UserInfo(pydantic.BaseModel): + class UserInfo(PydanticOdooBaseModel): name: str groups: List[Group] = pydantic.Field(alias="groups_id") - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - user = self.env.user user_info = UserInfo.from_orm(user) -See the official `Pydantic -documentation `__ to discover all -the available functionalities. +See the official `Pydantic documentation `__ +to discover all the available functionalities. Known issues / Roadmap ====================== @@ -112,8 +100,9 @@ Authors Contributors ------------ -- Laurent Mignon -- Tris Doan +- Laurent Mignon +- Tris Doan +- Pierre Verkest Maintainers ----------- diff --git a/pydantic/readme/CONTRIBUTORS.md b/pydantic/readme/CONTRIBUTORS.md index 9b84ef6d3..29e261b02 100644 --- a/pydantic/readme/CONTRIBUTORS.md +++ b/pydantic/readme/CONTRIBUTORS.md @@ -1,2 +1,3 @@ - Laurent Mignon \<\> - Tris Doan \<\> +- Pierre Verkest \<\> diff --git a/pydantic/readme/DESCRIPTION.md b/pydantic/readme/DESCRIPTION.md index 83b7baff0..e05ef18f2 100644 --- a/pydantic/readme/DESCRIPTION.md +++ b/pydantic/readme/DESCRIPTION.md @@ -1,8 +1,7 @@ This addon provides a utility method that can be used to map odoo record -to a [Pydantic model](https://pydantic-docs.helpmanual.io/). +to a [Pydantic model (>= v2)](https://docs.pydantic.dev/). If you need to make your Pydantic models extendable at runtime, takes a look at the python package [extendable-pydantic](https://pypi.org/project/extendable_pydantic/) and -the odoo addon -[extendable](https://github.com/acsone/odoo-addon-extendable) +the [odoo addon extendable](https://pypi.org/project/odoo-addon-extendable) diff --git a/pydantic/readme/USAGE.md b/pydantic/readme/USAGE.md index 92327bb6a..08e54f5aa 100644 --- a/pydantic/readme/USAGE.md +++ b/pydantic/readme/USAGE.md @@ -1,32 +1,25 @@ To support pydantic models that map to Odoo models, Pydantic model instances can be created from arbitrary odoo model instances by mapping -fields from odoo models to fields defined by the pydantic model. To ease -the mapping, the addon provide a utility class -odoo.addons.pydantic.utils.GenericOdooGetter. +fields from odoo models to fields defined by the pydantic model. + + +To ease the mapping, the addon provide an utility class (using `pydantic>2.0`) `odoo.addons.pydantic.utils.PydanticOdooBaseModel`: ``` python -import pydantic -from odoo.addons.pydantic import utils +from odoo.addons.pydantic.utils import PydanticOdooBaseModel -class Group(pydantic.BaseModel): - name: str - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter +class Group(PydanticOdooBaseModel): + name: str -class UserInfo(pydantic.BaseModel): +class UserInfo(PydanticOdooBaseModel): name: str groups: List[Group] = pydantic.Field(alias="groups_id") - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - user = self.env.user user_info = UserInfo.from_orm(user) ``` See the official [Pydantic -documentation](https://pydantic-docs.helpmanual.io/) to discover all the +documentation](https://docs.pydantic.dev/) to discover all the available functionalities. diff --git a/pydantic/static/description/index.html b/pydantic/static/description/index.html index 557bfc29e..4639ee29a 100644 --- a/pydantic/static/description/index.html +++ b/pydantic/static/description/index.html @@ -3,7 +3,7 @@ -README.rst +Pydantic -
+
+

Pydantic

- - -Odoo Community Association - -
-

Pydantic

-

Beta License: LGPL-3 OCA/rest-framework Translate me on Weblate Try me on Runboat

+

Beta License: LGPL-3 OCA/rest-framework Translate me on Weblate Try me on Runboat

This addon provides a utility method that can be used to map odoo record -to a Pydantic model.

+to a Pydantic model (>= v2).

If you need to make your Pydantic models extendable at runtime, takes a look at the python package extendable-pydantic -and the odoo addon -extendable

+and the odoo addon +extendable

Table of contents

    @@ -397,40 +392,31 @@

    Pydantic

-

Usage

+

Usage

To support pydantic models that map to Odoo models, Pydantic model instances can be created from arbitrary odoo model instances by mapping -fields from odoo models to fields defined by the pydantic model. To ease -the mapping, the addon provide a utility class -odoo.addons.pydantic.utils.GenericOdooGetter.

+fields from odoo models to fields defined by the pydantic model.

+

To ease the mapping, the addon provide an utility class (using +pydantic>2.0) odoo.addons.pydantic.utils.PydanticOdooBaseModel:

-import pydantic
-from odoo.addons.pydantic import utils
+from odoo.addons.pydantic.utils import PydanticOdooBaseModel
 
-class Group(pydantic.BaseModel):
-    name: str
 
-    class Config:
-        orm_mode = True
-        getter_dict = utils.GenericOdooGetter
+class Group(PydanticOdooBaseModel):
+    name: str
 
-class UserInfo(pydantic.BaseModel):
+class UserInfo(PydanticOdooBaseModel):
     name: str
     groups: List[Group] = pydantic.Field(alias="groups_id")
 
-    class Config:
-        orm_mode = True
-        getter_dict = utils.GenericOdooGetter
-
 user = self.env.user
 user_info = UserInfo.from_orm(user)
 
-

See the official Pydantic -documentation to discover all -the available functionalities.

+

See the official Pydantic documentation +to discover all the available functionalities.

-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -446,22 +432,23 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • ACSONE SA/NV
-

Contributors

+

Contributors

-

Maintainers

+

Maintainers

This module is maintained by the OCA.

Odoo Community Association @@ -476,6 +463,5 @@

Maintainers

-
- + \ No newline at end of file diff --git a/pydantic/tests/__init__.py b/pydantic/tests/__init__.py new file mode 100644 index 000000000..5cf60de35 --- /dev/null +++ b/pydantic/tests/__init__.py @@ -0,0 +1 @@ +from . import test_pydantic_generic_odoo_getter diff --git a/pydantic/tests/test_pydantic_generic_odoo_getter.py b/pydantic/tests/test_pydantic_generic_odoo_getter.py new file mode 100644 index 000000000..28cc2c6d6 --- /dev/null +++ b/pydantic/tests/test_pydantic_generic_odoo_getter.py @@ -0,0 +1,139 @@ +import datetime + +from odoo import fields +from odoo.tests import TransactionCase + +from pydantic import Field + +from ..utils import PydanticOdooBaseModel as PydanticOrmBaseModel + + +class OdooBaseModel(PydanticOrmBaseModel): + id: int + + +class PartnerModel(OdooBaseModel): + name: str + + +class UserFlatModel(OdooBaseModel): + partner_id: int = Field(title="Partner") + + +class GroupModel(OdooBaseModel): + name: str + + +class UserModel(OdooBaseModel): + partner: PartnerModel = Field(title="Partner", alias="partner_id") + + +class UserDetailsModel(UserModel): + groups: list[GroupModel] = Field(alias="groups_id") + action_id: OdooBaseModel | None = None + signature: str | None = None + active: bool | None = None + share: bool | None = None + write_date: datetime.datetime + + +class CurrencyRateModel(OdooBaseModel): + name: datetime.date | None = None + rate: float + + +class CommonPydanticCase(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.user_demo = cls.env.ref("base.user_demo") + cls.user_demo.action_id = False + cls.user_demo.signature = False + cls.user_demo.share = False + cls.currency_eur = cls.env.ref("base.USD") + cls.currency_rate = cls.env.ref("base.rateUSD") + + +class TestGenericOdooGetterPydanticV2Case(CommonPydanticCase): + def test_user_model_serialization(self): + self.currency_rate.name = None # name is a date field + self.assertEqual( + CurrencyRateModel.model_validate( + self.currency_rate, from_attributes=True + ).model_dump(), + { + "id": self.currency_rate.id, + "name": None, + "rate": self.currency_rate.rate, + }, + ) + + def test_user_model_serialization_date(self): + self.currency_rate.name = fields.Date.today() # name is a date field + self.assertEqual( + CurrencyRateModel.model_validate(self.currency_rate).name, + self.currency_rate.name, + ) + + def test_user_model_details_serialization_datetime(self): + user_demo = self.user_demo.with_context(tz="Asia/Tokyo") + self.assertEqual( + UserDetailsModel.model_validate(user_demo).write_date, + fields.Datetime.context_timestamp(user_demo, user_demo.write_date), + ) + self.assertNotEqual( + UserDetailsModel.model_validate(user_demo).write_date.tzinfo, + fields.Datetime.context_timestamp( + self.user_demo, user_demo.write_date + ).tzinfo, + ) + + def test_user_details_model_serialization(self): + self.assertEqual( + UserDetailsModel.model_validate(self.user_demo).model_dump(), + { + "id": self.user_demo.id, + "partner": { + "id": self.user_demo.partner_id.id, + "name": self.user_demo.partner_id.name, + }, + "groups": [ + { + "id": group.id, + "name": group.name, + } + for group in self.user_demo.groups_id + ], + "action_id": None, + "signature": None, + "active": True, + "share": False, + "write_date": fields.Datetime.context_timestamp( + self.user_demo, self.user_demo.write_date + ), + }, + ) + + def test_user_flat_model_serialization(self): + self.assertEqual( + UserFlatModel.model_validate(self.user_demo).model_dump(), + { + "id": self.user_demo.id, + "partner_id": self.user_demo.partner_id.id, + }, + ) + + def test_not_an_odoo_record(self): + user = UserDetailsModel( + id=666, + partner_id={"id": 66, "name": "test"}, + groups_id=[{"id": 33, "name": "group 1"}], + action_id={"id": 55}, + signature=None, + active=True, + share=False, + write_date=fields.Datetime.now(), + ) + self.assertEqual( + UserDetailsModel.model_validate(user).model_dump(), user.model_dump() + ) diff --git a/pydantic/utils.py b/pydantic/utils.py index 1be3434d3..f1489e386 100644 --- a/pydantic/utils.py +++ b/pydantic/utils.py @@ -5,64 +5,80 @@ from odoo import fields, models -from pydantic.utils import GetterDict +from pydantic import ( + BaseModel, + ConfigDict, + ValidationInfo, + field_validator, + model_validator, +) -class GenericOdooGetter(GetterDict): - """A generic GetterDict for Odoo models +class PydanticOdooBaseModel(BaseModel): + """Pydantic BaseModel for odoo record - The getter take care of casting one2many and many2many - field values to python list to allow the from_orm method from - pydantic class to work on odoo models. This getter is to specify - into the pydantic config. - - Usage: - - .. code-block:: python - - import pydantic - from odoo.addons.pydantic import models, utils - - class Group(models.BaseModel): - name: str - - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - - class UserInfo(models.BaseModel): - name: str - groups: List[Group] = pydantic.Field(alias="groups_id") - - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - - user = self.env.user - user_info = UserInfo.from_orm(user) - - To avoid having to repeat the specific configuration required for the - `from_orm` method into each pydantic model, "odoo_orm_mode" can be used - as parent via the `_inherit` attribute + This aims to help to serialize Odoo record + improving behavior like previous version: + * Avoid False value on non boolean fields + * Convert Datetime to Datetime timezone aware + * using int type on many2one return the foreign key id + (not the odoo record) """ - def get(self, key: Any, default: Any = None) -> Any: - res = getattr(self._obj, key, default) - if isinstance(self._obj, models.BaseModel) and key in self._obj._fields: - field = self._obj._fields[key] - if res is False and field.type != "boolean": - return None - if field.type == "date" and not res: - return None - if field.type == "datetime": - if not res: + model_config = ConfigDict( + from_attributes=True, + ) + + @classmethod + def model_validate( + cls, + obj: Any, + *, + context: Any | None = None, + **kwargs, + ): + if context is None: + context = {} + + if "odoo_records" not in context: + context["odoo_records"] = {} + + return super().model_validate( + obj, + context=context, + **kwargs, + ) + + @field_validator("*", mode="before") + @classmethod + def odoo_validator_before(cls, value: Any, info: ValidationInfo): + odoo_record = info.context and info.context.get("odoo_records").get( + info.config.get("title") + ) + if odoo_record is not None: + if info.field_name in odoo_record._fields: + field = odoo_record._fields[info.field_name] + if value is False and field.type != "boolean": return None - # Get the timestamp converted to the client's timezone. - # This call also add the tzinfo into the datetime object - return fields.Datetime.context_timestamp(self._obj, res) - if field.type == "many2one" and not res: - return None - if field.type in ["one2many", "many2many"]: - return list(res) - return res + if field.type == "datetime": + # Get the timestamp converted to the client's timezone. + # This call also add the tzinfo into the datetime object + return fields.Datetime.context_timestamp(odoo_record, value) + if field.type == "many2one": + if not value: + return None + if issubclass(cls.__annotations__.get(info.field_name), int): + # if field typing is an integer we return the .id + # (not the odoo record) + return value.id + return value + + @model_validator(mode="before") + @classmethod + def odoo_model_validator(cls, data: Any, info: ValidationInfo) -> Any: + if isinstance(info.context, dict): + info.context["odoo_records"][info.config.get("title")] = ( + data if isinstance(data, models.BaseModel) else None + ) + return data