forked from OCA/server-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_char_transformed.py
46 lines (38 loc) · 1.63 KB
/
field_char_transformed.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
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import fields
from openerp.osv import fields as low_level_fields
class ColumnCharTransformed(low_level_fields.char):
def _transform(self, value):
return low_level_fields._symbol_set_char(self, self.transform(value))
def __init__(self, string="unknown", size=None, **args):
super(ColumnCharTransformed, self).__init__(
string=string, size=size, **args
)
if self.transform:
self._symbol_f = self._symbol_set_char = self._transform
self._symbol_set = (self._symbol_c, self._symbol_f)
class FieldCharTransformed(fields.Char):
_slots = {
# a callable receiving a value and returning its result
'transform': None,
}
def convert_to_read(self, value, use_name_get=True):
result = super(FieldCharTransformed, self).convert_to_read(
value, use_name_get=use_name_get)
if self.transform:
return self.transform(result)
return result
def convert_to_write(self, value, target=None, fnames=None):
result = super(FieldCharTransformed, self).convert_to_write(
value, target=target, fnames=fnames)
if self.transform:
return self.transform(result)
return result
def to_column(self):
result = super(FieldCharTransformed, self).to_column()
if result and isinstance(result, low_level_fields.char):
result = ColumnCharTransformed(
transform=self.transform, **result._args)
return result