-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
utils.py
68 lines (51 loc) · 2.17 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)
from typing import Any
from odoo import fields, models
from pydantic.utils import GetterDict
class GenericOdooGetter(GetterDict):
"""A generic GetterDict for Odoo models
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
"""
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:
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