Skip to content

Commit 69e66b4

Browse files
committed
[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.
1 parent 11744a0 commit 69e66b4

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

queue_job/fields.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class JobEncoder(json.JSONEncoder):
7070
"""Encode Odoo recordsets so that we can later recompose them"""
7171

7272
def _get_record_context(self, obj):
73+
if obj.env['ir.config_parameter'].sudo().get_param('queue_job.keep_context'):
74+
return obj.env.context
7375
return obj._job_prepare_context_before_enqueue()
7476

7577
def default(self, obj):

queue_job/tests/test_json_field.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ def test_encoder_recordset(self):
3131
}
3232
self.assertEqual(json.loads(value_json), expected)
3333

34+
def test_encoder_recordset_keep_context(self):
35+
demo_user = self.env.ref("base.user_demo")
36+
self.env["ir.config_parameter"].sudo().set_param("queue_job.keep_context", True)
37+
context = {"foo": "bar"} | demo_user.context_get()
38+
partner = self.env(user=demo_user, context=context).ref("base.main_partner")
39+
value = partner
40+
value_json = json.dumps(value, cls=JobEncoder)
41+
expected = {
42+
"uid": demo_user.id,
43+
"_type": "odoo_recordset",
44+
"model": "res.partner",
45+
"ids": [partner.id],
46+
"su": False,
47+
"context": {"foo": "bar", "tz": context["tz"], "lang": context["lang"]},
48+
}
49+
self.assertEqual(json.loads(value_json), expected)
50+
3451
def test_encoder_recordset_list(self):
3552
demo_user = self.env.ref("base.user_demo")
3653
context = demo_user.context_get()

0 commit comments

Comments
 (0)