Repository navigation
Expand file tree
/
Copy pathfields_misc.py
More file actions
135 lines (101 loc) · 4.2 KB
/
Copy pathfields_misc.py
File metadata and controls
135 lines (101 loc) · 4.2 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from __future__ import annotations
import copy
import json
import typing
from psycopg2.extras import Json as PsycopgJson
from odoo.tools import SQL, json_default
from .fields import Field
from .identifiers import IdType
if typing.TYPE_CHECKING:
from .models import BaseModel
from odoo.tools import Query
# integer needs to be imported before Id because of `type` attribute clash
from . import fields_numeric # noqa: F401
class Boolean(Field[bool]):
""" Encapsulates a :class:`bool`. """
type = 'boolean'
_column_type = ('bool', 'bool')
falsy_value = False
def convert_to_column(self, value, record, values=None, validate=True):
return bool(value)
def convert_to_cache(self, value, record, validate=True):
return bool(value)
def convert_to_export(self, value, record):
return bool(value)
def _condition_to_sql(self, field_expr: str, operator: str, value, model: BaseModel, alias: str, query: Query) -> SQL:
if operator not in ('in', 'not in'):
return super()._condition_to_sql(field_expr, operator, value, model, alias, query)
# get field and check access
sql_field = model._field_to_sql(alias, field_expr, query)
# express all conditions as (field_expr, 'in', possible_values)
possible_values = (
{bool(v) for v in value} if operator == 'in' else
{True, False} - {bool(v) for v in value} # operator == 'not in'
)
if len(possible_values) != 1:
return SQL("TRUE") if possible_values else SQL("FALSE")
is_true = True in possible_values
return SQL("%s IS TRUE", sql_field) if is_true else SQL("%s IS NOT TRUE", sql_field)
class Json(Field):
""" JSON Field that contain unstructured information in jsonb PostgreSQL column.
Some features won't be implemented, including:
* searching
* indexing
* mutating the values.
"""
type = 'json'
_column_type = ('jsonb', 'jsonb')
def convert_to_record(self, value, record):
""" Return a copy of the value """
return False if value is None else copy.deepcopy(value)
def convert_to_cache(self, value, record, validate=True):
if not value:
return None
return json.loads(json.dumps(value, ensure_ascii=False, default=json_default))
def convert_to_column(self, value, record, values=None, validate=True):
if validate:
value = self.convert_to_cache(value, record)
if value is None:
return None
return PsycopgJson(value)
def convert_to_export(self, value, record):
if not value:
return ''
return json.dumps(value)
class Id(Field[IdType | typing.Literal[False]]):
""" Special case for field 'id'. """
# Note: This field type is not necessarily an integer!
type = 'integer' # note this conflicts with Integer
column_type = ('int4', 'int4')
string = 'ID'
store = True
readonly = True
prefetch = False
def update_db(self, model, columns):
pass # this column is created with the table
def __get__(self, record, owner=None):
if record is None:
return self # the field is accessed through the class owner
# the code below is written to make record.id as quick as possible
ids = record._ids
size = len(ids)
if size == 0:
return False
elif size == 1:
return ids[0]
raise ValueError("Expected singleton: %s" % record)
def __set__(self, record, value):
raise TypeError("field 'id' cannot be assigned")
def convert_to_column(self, value, record, values=None, validate=True):
return value
def to_sql(self, model: BaseModel, alias: str) -> SQL:
# do not flush, just return the identifier
assert self.store, 'id field must be stored'
# id is never flushed
return SQL.identifier(alias, self.name)
def expression_getter(self, field_expr):
if field_expr != 'id.origin':
return super().expression_getter(field_expr)
def getter(record):
return (id_ := record._ids[0]) or getattr(id_, 'origin', None) or False
return getter