Support default value from context
This commit is contained in:
1 parent
92b3416d79
commit
d8530fc867
5 files changed
+106
-2
No files matched your search
@@ -1,3 +1,4 @@
|
||||
* Support default value from context
|
||||
* Add autocomplete method on ModelView
|
||||
* Add likify tools
|
||||
* Remove user from contextual key of Cache
|
||||
|
||||
@@ -90,6 +90,9 @@ Class methods:
|
||||
The ``default_rec_name`` key in the context can be used to define the value
|
||||
of the :attr:`~Model._rec_name` field.
|
||||
|
||||
The ``default_<field name>`` keys in the context can be used to define the
|
||||
value of the field.
|
||||
|
||||
.. classmethod:: Model.fields_get([fields_names[, level]])
|
||||
|
||||
Return the definition of each field on the model.
|
||||
|
||||
@@ -136,8 +136,9 @@ class Model(URLMixin, PoolBase, metaclass=ModelMeta):
|
||||
'''
|
||||
pool = Pool()
|
||||
value = {}
|
||||
context = Transaction().context
|
||||
|
||||
default_rec_name = Transaction().context.get('default_rec_name')
|
||||
default_rec_name = context.get('default_rec_name')
|
||||
if (default_rec_name
|
||||
and cls._rec_name in cls._fields
|
||||
and cls._rec_name in fields_names):
|
||||
@@ -145,7 +146,10 @@ class Model(URLMixin, PoolBase, metaclass=ModelMeta):
|
||||
|
||||
# get the default values defined in the object
|
||||
for field_name in fields_names:
|
||||
if field_name in cls._defaults:
|
||||
default_name = f'default_{field_name}'
|
||||
if field_name in cls._fields and default_name in context:
|
||||
value[field_name] = context.get(default_name)
|
||||
elif field_name in cls._defaults:
|
||||
value[field_name] = cls._defaults[field_name]()
|
||||
field = cls._fields[field_name]
|
||||
if (field._type == 'boolean'
|
||||
|
||||
@@ -55,6 +55,19 @@ class ModelContextParent(Model):
|
||||
})
|
||||
|
||||
|
||||
class ModelDefault(Model):
|
||||
"Model with default"
|
||||
__name__ = 'test.model.default'
|
||||
|
||||
name = fields.Char("Name")
|
||||
description = fields.Char("Description")
|
||||
target = fields.Many2One('test.model', "Target")
|
||||
|
||||
@classmethod
|
||||
def default_description(cls):
|
||||
return "Test"
|
||||
|
||||
|
||||
class Singleton(ModelSingleton, ModelSQL):
|
||||
'Singleton'
|
||||
__name__ = 'test.singleton'
|
||||
@@ -155,6 +168,7 @@ def register(module):
|
||||
ModelChildChild,
|
||||
ModelContext,
|
||||
ModelContextParent,
|
||||
ModelDefault,
|
||||
Singleton,
|
||||
URLObject,
|
||||
Model4Union1,
|
||||
|
||||
@@ -283,6 +283,88 @@ class ModelTestCase(unittest.TestCase):
|
||||
self.assertEqual(record.name, 'foo')
|
||||
self.assertEqual(record_copied.name, 'bar')
|
||||
|
||||
@with_transaction()
|
||||
def test_default_get(self):
|
||||
"Test default_get"
|
||||
pool = Pool()
|
||||
Model = pool.get('test.model.default')
|
||||
|
||||
self.assertEqual(
|
||||
Model.default_get(['name', 'description']), {
|
||||
'description': "Test",
|
||||
})
|
||||
|
||||
@with_transaction()
|
||||
def test_default_get_with_context(self):
|
||||
"Test default_get with context"
|
||||
pool = Pool()
|
||||
Model = pool.get('test.model.default')
|
||||
|
||||
with Transaction().set_context(default_name="foo"):
|
||||
self.assertEqual(
|
||||
Model.default_get(['name']), {
|
||||
'name': "foo",
|
||||
})
|
||||
|
||||
@with_transaction()
|
||||
def test_default_get_with_context_and_method(self):
|
||||
"Test default_get with context and method"
|
||||
pool = Pool()
|
||||
Model = pool.get('test.model.default')
|
||||
|
||||
with Transaction().set_context(default_description="foo"):
|
||||
self.assertEqual(
|
||||
Model.default_get(['description']), {
|
||||
'description': "foo",
|
||||
})
|
||||
|
||||
@with_transaction()
|
||||
def test_default_get_with_context_rec_name(self):
|
||||
"Test default_get with context rec_name"
|
||||
pool = Pool()
|
||||
Model = pool.get('test.model.default')
|
||||
|
||||
with Transaction().set_context(default_rec_name="foo"):
|
||||
self.assertEqual(
|
||||
Model.default_get(['name']), {
|
||||
'name': "foo",
|
||||
})
|
||||
|
||||
@with_transaction()
|
||||
def test_default_get_with_rec_name(self):
|
||||
"Test default_get with rec_name"
|
||||
pool = Pool()
|
||||
Model = pool.get('test.model.default')
|
||||
Target = pool.get('test.model')
|
||||
|
||||
target = Target(name="Target")
|
||||
target.save()
|
||||
|
||||
with Transaction().set_context(default_target=target.id):
|
||||
self.assertEqual(
|
||||
Model.default_get(['target'], with_rec_name=True), {
|
||||
'target': target.id,
|
||||
'target.': {
|
||||
'rec_name': "Target",
|
||||
},
|
||||
})
|
||||
|
||||
@with_transaction()
|
||||
def test_default_get_without_rec_name(self):
|
||||
"Test default_get without rec_name"
|
||||
pool = Pool()
|
||||
Model = pool.get('test.model.default')
|
||||
Target = pool.get('test.model')
|
||||
|
||||
target = Target(name="Target")
|
||||
target.save()
|
||||
|
||||
with Transaction().set_context(default_target=target.id):
|
||||
self.assertEqual(
|
||||
Model.default_get(['target'], with_rec_name=False), {
|
||||
'target': target.id,
|
||||
})
|
||||
|
||||
|
||||
class ModelTranslationTestCase(unittest.TestCase):
|
||||
"Test Model translation"
|
||||
|
||||
Reference in new issue
Block a user