Add translated descriptor for Selection field

issue3679
review10401002
This commit is contained in:
Cédric Krier
2014-08-04 16:50:59 +02:00
parent 2b5e337553
commit cd2ac5c1b1
8 changed files with 49 additions and 3 deletions
+1
View File
@@ -1,3 +1,4 @@
* Add translated descriptor for Selection field
* Add tree_state attribute on tree view
* Allow to sync XML data
* Remove on_change calls in Model.default_get
+7
View File
@@ -413,6 +413,13 @@ A string field with limited values to choice.
If true, the human-readable values will be translated. Default value is
``True``.
Instance methods:
.. method:: Selection.translated([name])
Returns a descriptor for the translated value of the field. The descriptor
must be used on the same class as the field.
Reference
---------
+2
View File
@@ -309,6 +309,8 @@ CLass methods:
Return a list of list of values for each ``records``.
The list of values follows ``fields_names``.
Relational fields are defined with ``/`` at any depth.
Descriptor on fields are available by appending ``.`` and the name of the
method on the field that returns the descriptor.
.. classmethod:: ModelStorage.import_data(fields_names, data)
+26
View File
@@ -63,3 +63,29 @@ class Selection(Field):
for key, value in selections:
whens.append((column == key, value))
return [Case(*whens, else_=column)]
def translated(self, name=None):
"Return a descriptor for the translated value of the field"
if name is None:
name = self.name
if name is None:
raise ValueError('Missing name argument')
return TranslatedSelection(name)
class TranslatedSelection(object):
'A descriptor for translated value of Selection field'
def __init__(self, name):
self.name = name
def __get__(self, inst, cls):
if inst is None:
return self
selection = dict(cls.fields_get([self.name])[self.name]['selection'])
value = getattr(inst, self.name)
# None and '' are equivalent
if value is None or value == '':
if value not in selection:
value = {None: '', '': None}[value]
return selection[value]
+7 -1
View File
@@ -452,6 +452,9 @@ class ModelStorage(Model):
if not isinstance(value, ModelStorage):
break
field_name = fields_tree[i]
descriptor = None
if '.' in field_name:
field_name, descriptor = field_name.split('.')
eModel = pool.get(value.__name__)
field = eModel._fields[field_name]
if field.states and 'invisible' in field.states:
@@ -467,7 +470,10 @@ class ModelStorage(Model):
if invisible:
value = ''
break
value = getattr(value, field_name)
if descriptor:
value = getattr(field, descriptor)().__get__(value, eModel)
else:
value = getattr(value, field_name)
if isinstance(value, (list, tuple)):
first = True
child_fields_names = [(x[:i + 1] == fields_tree[:i + 1] and
+1
View File
@@ -604,6 +604,7 @@ class Selection(ModelSQL):
select = fields.Selection([
('', ''), ('arabic', 'Arabic'), ('hexa', 'Hexadecimal')],
'Selection')
select_string = select.translated('select')
dyn_select = fields.Selection('get_selection',
'Instance Dynamic Selection')
dyn_select_static = fields.Selection('static_selection',
+3 -2
View File
@@ -216,8 +216,9 @@ class ExportDataTestCase(unittest.TestCase):
'selection': 'select1',
}])
self.assertEqual(
self.export_data.export_data([export1], ['selection']),
[['select1']])
self.export_data.export_data([export1], ['selection',
'selection.translated']),
[['select1', 'Select 1']])
export2, = self.export_data.create([{
'selection': None,
+2
View File
@@ -3060,10 +3060,12 @@ class FieldsTestCase(unittest.TestCase):
selection1, = self.selection.create([{'select': 'arabic'}])
self.assert_(selection1)
self.assertEqual(selection1.select, 'arabic')
self.assertEqual(selection1.select_string, 'Arabic')
selection2, = self.selection.create([{'select': None}])
self.assert_(selection2)
self.assertEqual(selection2.select, None)
self.assertEqual(selection2.select_string, '')
self.assertRaises(UserError, self.selection.create,
[{'select': 'chinese'}])