Skip to content

Commit 9496787

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.
1 parent 6f88b31 commit 9496787

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

queue_job/fields.py

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

8282
def _get_record_context(self, obj):
83+
if obj.env["ir.config_parameter"].sudo().get_param("queue_job.keep_context"):
84+
return obj.env.context
8385
return obj._job_prepare_context_before_enqueue()
8486

8587
def default(self, obj):

queue_job/tests/test_json_field.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ def test_encoder_recordset(self):
3232
}
3333
self.assertEqual(json.loads(value_json), expected)
3434

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

0 commit comments

Comments
 (0)