Skip to content

Commit

Permalink
[IMP] Add queue_job.keep_context ir.config_parameter
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amh-mw committed Dec 21, 2023
1 parent 11744a0 commit 69e66b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions queue_job/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
17 changes: 17 additions & 0 deletions queue_job/tests/test_json_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 69e66b4

Please sign in to comment.