Support selection method in TranslatedSelection
The Selection field could have a method name as selection so TranslatedSelection must call this method to get the selection values. issue5999 review27791002
This commit is contained in:
1 parent
ec8884d05f
commit
21e4b96fea
3 files changed
+18
-2
No files matched your search
@@ -6,6 +6,7 @@ from sql.conditionals import Case
|
||||
|
||||
from ... import backend
|
||||
from ...transaction import Transaction
|
||||
from ...tools import is_instance_method
|
||||
from .field import Field, SQLType
|
||||
|
||||
|
||||
@@ -84,8 +85,14 @@ class TranslatedSelection(object):
|
||||
if inst is None:
|
||||
return self
|
||||
with Transaction().set_context(getattr(inst, '_context', {})):
|
||||
selection = dict(
|
||||
cls.fields_get([self.name])[self.name]['selection'])
|
||||
selection = cls.fields_get([self.name])[self.name]['selection']
|
||||
if not isinstance(selection, (tuple, list)):
|
||||
sel_func = getattr(cls, selection)
|
||||
if not is_instance_method(cls, selection):
|
||||
selection = sel_func()
|
||||
else:
|
||||
selection = sel_func(inst)
|
||||
selection = dict(selection)
|
||||
value = getattr(inst, self.name)
|
||||
# None and '' are equivalent
|
||||
if value is None or value == '':
|
||||
|
||||
@@ -654,8 +654,11 @@ class Selection(ModelSQL):
|
||||
select_string = select.translated('select')
|
||||
dyn_select = fields.Selection('get_selection',
|
||||
'Instance Dynamic Selection')
|
||||
dyn_select_string = dyn_select.translated('dyn_select')
|
||||
dyn_select_static = fields.Selection('static_selection',
|
||||
'Static Selection')
|
||||
dyn_select_static_string = dyn_select_static.translated(
|
||||
'dyn_select_static')
|
||||
|
||||
@fields.depends('select')
|
||||
def get_selection(self):
|
||||
|
||||
@@ -3192,6 +3192,7 @@ class FieldsTestCase(unittest.TestCase):
|
||||
self.assert_(selection3)
|
||||
self.assertEqual(selection3.select, 'arabic')
|
||||
self.assertEqual(selection3.dyn_select, '1')
|
||||
self.assertEqual(selection3.dyn_select_string, '1')
|
||||
|
||||
selection4, = Selection.create(
|
||||
[{'select': 'hexa', 'dyn_select': '0x3'}])
|
||||
@@ -3210,6 +3211,11 @@ class FieldsTestCase(unittest.TestCase):
|
||||
self.assertRaises(UserError, Selection.create,
|
||||
[{'select': 'hexa', 'dyn_select': '3'}])
|
||||
|
||||
selection7, = Selection.create(
|
||||
[{'dyn_select_static': '1'}])
|
||||
self.assertEqual(selection7.dyn_select_static, '1')
|
||||
self.assertEqual(selection7.dyn_select_static_string, '1')
|
||||
|
||||
self.assertRaises(UserError, SelectionRequired.create, [{}])
|
||||
transaction.rollback()
|
||||
|
||||
|
||||
Reference in new issue
Block a user