Skip to content

Commit 46d62d1

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 46d62d1

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

queue_job/models/base.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,23 @@ 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+
*(keys or ()),
257+
)
252258

253259
def _job_prepare_context_before_enqueue(self):
254260
"""Return the context to store in the jobs
255261
Can be used to keep only safe keys.
256262
"""
263+
if self.env.context.get("queue_job_keep_context"):
264+
return self.env.context
257265
return {
258266
key: value
259267
for key, value in self.env.context.items()

queue_job/tests/test_json_field.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,52 @@ 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 = {
38+
**demo_user.context_get(),
39+
"foo": "bar",
40+
"baz": "zeb",
41+
"queue_job_context_keys": ["foo"],
42+
}
43+
partner = self.env(user=demo_user, context=context).ref("base.main_partner")
44+
value = partner
45+
value_json = json.dumps(value, cls=JobEncoder)
46+
expected_context = context.copy()
47+
expected_context.pop("baz")
48+
expected_context.pop("queue_job_context_keys")
49+
expected_context.pop("uid")
50+
expected = {
51+
"uid": demo_user.id,
52+
"_type": "odoo_recordset",
53+
"model": "res.partner",
54+
"ids": [partner.id],
55+
"su": False,
56+
"context": expected_context,
57+
}
58+
self.assertEqual(json.loads(value_json), expected)
59+
60+
def test_encoder_recordset_keep_context(self):
61+
demo_user = self.env.ref("base.user_demo")
62+
context = {
63+
**demo_user.context_get(),
64+
"foo": "bar",
65+
"queue_job_keep_context": True,
66+
}
67+
partner = self.env(user=demo_user, context=context).ref("base.main_partner")
68+
value = partner
69+
value_json = json.dumps(value, cls=JobEncoder)
70+
expected_context = context.copy()
71+
expected = {
72+
"uid": demo_user.id,
73+
"_type": "odoo_recordset",
74+
"model": "res.partner",
75+
"ids": [partner.id],
76+
"su": False,
77+
"context": expected_context,
78+
}
79+
self.assertEqual(json.loads(value_json), expected)
80+
3581
def test_encoder_recordset_list(self):
3682
demo_user = self.env.ref("base.user_demo")
3783
context = demo_user.context_get()

0 commit comments

Comments
 (0)