From 69e66b4cefd3e43e4146959f19cc02e19ec389c7 Mon Sep 17 00:00:00 2001 From: Adam Heinz Date: Thu, 21 Dec 2023 16:44:29 -0500 Subject: [PATCH] [IMP] Add queue_job.keep_context ir.config_parameter When this parameter is set, queue_job always preserves the entire context. This honors the principle of least surprise, in that a developer can easily convert a record.method() call to record.with_delay().method() with the expectation that it will actually execute the same, simply at a later time. Fixes #406. --- queue_job/fields.py | 2 ++ queue_job/tests/test_json_field.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/queue_job/fields.py b/queue_job/fields.py index 2c78c18836..47b607ea8f 100644 --- a/queue_job/fields.py +++ b/queue_job/fields.py @@ -70,6 +70,8 @@ class JobEncoder(json.JSONEncoder): """Encode Odoo recordsets so that we can later recompose them""" def _get_record_context(self, obj): + if obj.env['ir.config_parameter'].sudo().get_param('queue_job.keep_context'): + return obj.env.context return obj._job_prepare_context_before_enqueue() def default(self, obj): diff --git a/queue_job/tests/test_json_field.py b/queue_job/tests/test_json_field.py index 802911c9eb..ae8568fe95 100644 --- a/queue_job/tests/test_json_field.py +++ b/queue_job/tests/test_json_field.py @@ -31,6 +31,23 @@ def test_encoder_recordset(self): } self.assertEqual(json.loads(value_json), expected) + def test_encoder_recordset_keep_context(self): + demo_user = self.env.ref("base.user_demo") + self.env["ir.config_parameter"].sudo().set_param("queue_job.keep_context", True) + context = {"foo": "bar"} | demo_user.context_get() + partner = self.env(user=demo_user, context=context).ref("base.main_partner") + value = partner + value_json = json.dumps(value, cls=JobEncoder) + expected = { + "uid": demo_user.id, + "_type": "odoo_recordset", + "model": "res.partner", + "ids": [partner.id], + "su": False, + "context": {"foo": "bar", "tz": context["tz"], "lang": context["lang"]}, + } + self.assertEqual(json.loads(value_json), expected) + def test_encoder_recordset_list(self): demo_user = self.env.ref("base.user_demo") context = demo_user.context_get()