diff --git a/trytond/CHANGELOG b/trytond/CHANGELOG index 9bb43797a3..7d01f255c6 100644 --- a/trytond/CHANGELOG +++ b/trytond/CHANGELOG @@ -1,3 +1,4 @@ +* Support default value from context * Add autocomplete method on ModelView * Add likify tools * Remove user from contextual key of Cache diff --git a/trytond/doc/ref/models.rst b/trytond/doc/ref/models.rst index c95133ff57..209a81c954 100644 --- a/trytond/doc/ref/models.rst +++ b/trytond/doc/ref/models.rst @@ -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_`` 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. diff --git a/trytond/trytond/model/model.py b/trytond/trytond/model/model.py index f1169c3971..6994ef26f7 100644 --- a/trytond/trytond/model/model.py +++ b/trytond/trytond/model/model.py @@ -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' diff --git a/trytond/trytond/tests/model.py b/trytond/trytond/tests/model.py index 7be0edc1f3..6d7239264d 100644 --- a/trytond/trytond/tests/model.py +++ b/trytond/trytond/tests/model.py @@ -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, diff --git a/trytond/trytond/tests/test_model.py b/trytond/trytond/tests/test_model.py index ef1babffe6..62217f7056 100644 --- a/trytond/trytond/tests/test_model.py +++ b/trytond/trytond/tests/test_model.py @@ -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"