Skip to content

Commit dac76d1

Browse files
committed
[IMP] queue_job: Improve context management.
When the `queue_job_keep_context` context variable is set, always preserve 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. When the `queue_job_context_keys` context variable is set, preserve the given keys in addition to those normally specified by `_job_prepare_context_before_enqueue_keys`.
1 parent 6f88b31 commit dac76d1

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

queue_job/models/base.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,22 @@ def _job_store_values(self, job):
245245

246246
@api.model
247247
def _job_prepare_context_before_enqueue_keys(self):
248-
"""Keys to keep in context of stored jobs
249-
Empty by default for backward compatibility.
250-
"""
251-
return ("tz", "lang", "allowed_company_ids", "force_company", "active_test")
248+
"""Keys to keep in context of stored jobs"""
249+
keys = self.env.context.get("queue_job_context_keys")
250+
return (
251+
"tz",
252+
"lang",
253+
"allowed_company_ids",
254+
"force_company",
255+
"active_test",
256+
) + tuple(keys or ())
252257

253258
def _job_prepare_context_before_enqueue(self):
254259
"""Return the context to store in the jobs
255260
Can be used to keep only safe keys.
256261
"""
262+
if self.env.context.get("queue_job_keep_context"):
263+
return self.env.context
257264
return {
258265
key: value
259266
for key, value in self.env.context.items()

queue_job/tests/test_json_field.py

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

35+
def test_encoder_recordset_context_keys(self):
36+
demo_user = self.env.ref("base.user_demo")
37+
context = demo_user.context_get()
38+
context.update({"foo": "bar", "baz": "zeb", "queue_job_context_keys": ["foo"]})
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_context.pop("baz")
44+
expected_context.pop("queue_job_context_keys")
45+
expected_context.pop("uid")
46+
expected = {
47+
"uid": demo_user.id,
48+
"_type": "odoo_recordset",
49+
"model": "res.partner",
50+
"ids": [partner.id],
51+
"su": False,
52+
"context": expected_context,
53+
}
54+
self.assertEqual(json.loads(value_json), expected)
55+
56+
def test_encoder_recordset_keep_context(self):
57+
demo_user = self.env.ref("base.user_demo")
58+
context = demo_user.context_get()
59+
context.update({"foo": "bar", "queue_job_keep_context": True})
60+
partner = self.env(user=demo_user, context=context).ref("base.main_partner")
61+
value = partner
62+
value_json = json.dumps(value, cls=JobEncoder)
63+
expected_context = context.copy()
64+
expected = {
65+
"uid": demo_user.id,
66+
"_type": "odoo_recordset",
67+
"model": "res.partner",
68+
"ids": [partner.id],
69+
"su": False,
70+
"context": expected_context,
71+
}
72+
self.assertEqual(json.loads(value_json), expected)
73+
3574
def test_encoder_recordset_list(self):
3675
demo_user = self.env.ref("base.user_demo")
3776
context = demo_user.context_get()

0 commit comments

Comments
 (0)