Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastapi/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion fastapi/tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 18 additions & 29 deletions pydantic/README.rst
Original file line number Diff line number Diff line change
@@ -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
========
Expand All @@ -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
Expand All @@ -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 <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>`__
and the `odoo addon
extendable <https://pypi.org/project/odoo-addon-extendable>`__

**Table of contents**

Expand All @@ -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 <https://pydantic-docs.helpmanual.io/>`__ to discover all
the available functionalities.
See the official `Pydantic documentation <https://docs.pydantic.dev/>`__
to discover all the available functionalities.

Known issues / Roadmap
======================
Expand Down Expand Up @@ -112,8 +100,9 @@ Authors
Contributors
------------

- Laurent Mignon <[email protected]>
- Tris Doan <[email protected]>
- Laurent Mignon <[email protected]>
- Tris Doan <[email protected]>
- Pierre Verkest <[email protected]>

Maintainers
-----------
Expand Down
1 change: 1 addition & 0 deletions pydantic/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Laurent Mignon \<<[email protected]>\>
- Tris Doan \<<[email protected]>\>
- Pierre Verkest \<<[email protected]>\>
5 changes: 2 additions & 3 deletions pydantic/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 9 additions & 16 deletions pydantic/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -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.
64 changes: 25 additions & 39 deletions pydantic/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>README.rst</title>
<title>Pydantic</title>
<style type="text/css">

/*
Expand Down Expand Up @@ -360,28 +360,23 @@
</style>
</head>
<body>
<div class="document">
<div class="document" id="pydantic">
<h1 class="title">Pydantic</h1>


<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
</a>
<div class="section" id="pydantic">
<h1>Pydantic</h1>
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:bf754ec770116cffb9eee07a08eb62409c1868dfd0f765c017156da1d8242df5
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/license-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/18.0/pydantic"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-pydantic"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/18.0/pydantic"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-pydantic"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This addon provides a utility method that can be used to map odoo record
to a <a class="reference external" href="https://pydantic-docs.helpmanual.io/">Pydantic model</a>.</p>
to a <a class="reference external" href="https://docs.pydantic.dev/">Pydantic model (&gt;= v2)</a>.</p>
<p>If you need to make your Pydantic models extendable at runtime, takes a
look at the python package
<a class="reference external" href="https://pypi.org/project/extendable_pydantic/">extendable-pydantic</a>
and the odoo addon
<a class="reference external" href="https://github.com/acsone/odoo-addon-extendable">extendable</a></p>
and the <a class="reference external" href="https://pypi.org/project/odoo-addon-extendable">odoo addon
extendable</a></p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -397,71 +392,63 @@ <h1>Pydantic</h1>
</ul>
</div>
<div class="section" id="usage">
<h2><a class="toc-backref" href="#toc-entry-1">Usage</a></h2>
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>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.</p>
fields from odoo models to fields defined by the pydantic model.</p>
<p>To ease the mapping, the addon provide an utility class (using
<tt class="docutils literal">pydantic&gt;2.0</tt>) <tt class="docutils literal">odoo.addons.pydantic.utils.PydanticOdooBaseModel</tt>:</p>
<pre class="code python literal-block">
<span class="kn">import</span><span class="w"> </span><span class="nn">pydantic</span><span class="w">
</span><span class="kn">from</span><span class="w"> </span><span class="nn">odoo.addons.pydantic</span><span class="w"> </span><span class="kn">import</span> <span class="n">utils</span><span class="w">
<span class="kn">from</span><span class="w"> </span><span class="nn">odoo.addons.pydantic.utils</span><span class="w"> </span><span class="kn">import</span> <span class="n">PydanticOdooBaseModel</span><span class="w">

</span><span class="k">class</span><span class="w"> </span><span class="nc">Group</span><span class="p">(</span><span class="n">pydantic</span><span class="o">.</span><span class="n">BaseModel</span><span class="p">):</span><span class="w">
</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="w">

</span> <span class="k">class</span><span class="w"> </span><span class="nc">Config</span><span class="p">:</span><span class="w">
</span> <span class="n">orm_mode</span> <span class="o">=</span> <span class="kc">True</span><span class="w">
</span> <span class="n">getter_dict</span> <span class="o">=</span> <span class="n">utils</span><span class="o">.</span><span class="n">GenericOdooGetter</span><span class="w">
</span><span class="k">class</span><span class="w"> </span><span class="nc">Group</span><span class="p">(</span><span class="n">PydanticOdooBaseModel</span><span class="p">):</span><span class="w">
</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="w">

</span><span class="k">class</span><span class="w"> </span><span class="nc">UserInfo</span><span class="p">(</span><span class="n">pydantic</span><span class="o">.</span><span class="n">BaseModel</span><span class="p">):</span><span class="w">
</span><span class="k">class</span><span class="w"> </span><span class="nc">UserInfo</span><span class="p">(</span><span class="n">PydanticOdooBaseModel</span><span class="p">):</span><span class="w">
</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="w">
</span> <span class="n">groups</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Group</span><span class="p">]</span> <span class="o">=</span> <span class="n">pydantic</span><span class="o">.</span><span class="n">Field</span><span class="p">(</span><span class="n">alias</span><span class="o">=</span><span class="s2">&quot;groups_id&quot;</span><span class="p">)</span><span class="w">

</span> <span class="k">class</span><span class="w"> </span><span class="nc">Config</span><span class="p">:</span><span class="w">
</span> <span class="n">orm_mode</span> <span class="o">=</span> <span class="kc">True</span><span class="w">
</span> <span class="n">getter_dict</span> <span class="o">=</span> <span class="n">utils</span><span class="o">.</span><span class="n">GenericOdooGetter</span><span class="w">

</span><span class="n">user</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">user</span><span class="w">
</span><span class="n">user_info</span> <span class="o">=</span> <span class="n">UserInfo</span><span class="o">.</span><span class="n">from_orm</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
</pre>
<p>See the official <a class="reference external" href="https://pydantic-docs.helpmanual.io/">Pydantic
documentation</a> to discover all
the available functionalities.</p>
<p>See the official <a class="reference external" href="https://docs.pydantic.dev/">Pydantic documentation</a>
to discover all the available functionalities.</p>
</div>
<div class="section" id="known-issues-roadmap">
<h2><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h2>
<h1><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h1>
<p>The
<a class="reference external" href="https://github.com/OCA/rest-framework/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement+label%3Apydantic">roadmap</a>
and <a class="reference external" href="https://github.com/OCA/rest-framework/issues?q=is%3Aopen+is%3Aissue+label%3Abug+label%3Apydantic">known
issues</a>
can be found on GitHub.</p>
</div>
<div class="section" id="bug-tracker">
<h2><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h2>
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/rest-framework/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20pydantic%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h2><a class="toc-backref" href="#toc-entry-4">Credits</a></h2>
<h1><a class="toc-backref" href="#toc-entry-4">Credits</a></h1>
<div class="section" id="authors">
<h3><a class="toc-backref" href="#toc-entry-5">Authors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
<ul class="simple">
<li>ACSONE SA/NV</li>
</ul>
</div>
<div class="section" id="contributors">
<h3><a class="toc-backref" href="#toc-entry-6">Contributors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<ul class="simple">
<li>Laurent Mignon &lt;<a class="reference external" href="mailto:laurent.mignon&#64;acsone.eu">laurent.mignon&#64;acsone.eu</a>&gt;</li>
<li>Tris Doan &lt;<a class="reference external" href="mailto:tridm&#64;trobz.com">tridm&#64;trobz.com</a>&gt;</li>
<li>Pierre Verkest &lt;<a class="reference external" href="mailto:pierre&#64;verkest.fr">pierre&#64;verkest.fr</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
<h3><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h3>
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
Expand All @@ -476,6 +463,5 @@ <h3><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h3>
</div>
</div>
</div>
</div>
</body>
</html>
</html>
1 change: 1 addition & 0 deletions pydantic/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_pydantic_generic_odoo_getter
Loading