-
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] Pydantic: Improve API to be more pythonic
- Loading branch information
Showing
15 changed files
with
685 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from . import builder | ||
from . import models | ||
from . import registry | ||
from . import ir_http |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"external_dependencies": { | ||
"python": [ | ||
"pydantic", | ||
"contextvars", | ||
] | ||
}, | ||
"installable": True, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright 2021 ACSONE SA/NV | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) | ||
|
||
# define context vars to hold the pydantic registry | ||
|
||
from contextvars import ContextVar | ||
|
||
pydantic_registry = ContextVar("pydantic_registry") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2021 ACSONE SA/NV | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) | ||
|
||
from contextlib import contextmanager | ||
|
||
from odoo import models | ||
from odoo.http import request | ||
|
||
from .context import pydantic_registry | ||
from .registry import _pydantic_classes_databases | ||
|
||
|
||
class IrHttp(models.AbstractModel): | ||
_inherit = "ir.http" | ||
|
||
@classmethod | ||
def _dispatch(cls): | ||
with cls._pydantic_context_registry(): | ||
return super()._dispatch() | ||
|
||
@classmethod | ||
@contextmanager | ||
def _pydantic_context_registry(cls): | ||
registry = _pydantic_classes_databases.get(request.env.cr.dbname, {}) | ||
token = pydantic_registry.set(registry) | ||
try: | ||
yield | ||
finally: | ||
pydantic_registry.reset(token) |
Oops, something went wrong.