Skip to content
This repository was archived by the owner on Feb 16, 2026. It is now read-only.

Commit 9691bfe

Browse files
committed
fixup! [FIX] queue job pass stored context
1 parent bb7909e commit 9691bfe

3 files changed

Lines changed: 28 additions & 23 deletions

File tree

queue_job/job.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,6 @@ def enqueue(cls, func, args=None, kwargs=None,
358358
def db_record_from_uuid(env, job_uuid):
359359
model = env['queue.job'].sudo()
360360
record = model.search([('uuid', '=', job_uuid)], limit=1)
361-
if record.context:
362-
try:
363-
ctx = json.loads(record.context)
364-
except Exception as e:
365-
_logger.error('\n\nERROR Due to JSON CONVERSION: %s\n\n' % e)
366-
return record.with_env(env)
367-
else:
368-
_logger.info("\n\nCONTEXT: %s\n\n" % ctx)
369-
return record.with_context(ctx).with_env(env)
370361
return record.with_env(env)
371362

372363
def __init__(self, func,
@@ -518,15 +509,13 @@ def store(self):
518509
'date_done': False,
519510
'eta': False,
520511
'identity_key': False,
521-
'context': '{}',
522512
}
523513

524514
dt_to_string = odoo.fields.Datetime.to_string
525515
context = {}
526516
if self.keep_context:
527517
context = self.env.context.copy()
528518
vals.update({"context": json.dumps(context)})
529-
_logger.info("\n\nCURRENT CONTEXT: %s\n\n" % context)
530519
if self.date_enqueued:
531520
vals['date_enqueued'] = dt_to_string(self.date_enqueued)
532521
if self.date_started:
@@ -543,6 +532,9 @@ def store(self):
543532
db_record.write(vals)
544533
else:
545534
date_created = dt_to_string(self.date_created)
535+
# We store the original context used at import on create
536+
ctx = self.env.context.copy() or '{}'
537+
vals.update({'original_context': json.dumps(ctx) or ''})
546538
# The following values must never be modified after the
547539
# creation of the job
548540
vals.update({'uuid': self.uuid,
@@ -560,27 +552,34 @@ def store(self):
560552
vals.update({'channel': self.channel})
561553

562554
job = self.env[self.job_model_name].sudo().create(vals)
563-
_logger.info('\n\nJOB: %s CONTEXT: %s' % (job, context))
564555

565556
def db_record(self):
566557
return self.db_record_from_uuid(self.env, self.uuid)
567558

559+
def _get_abs_context(self, original_ctx, ctx):
560+
try:
561+
import_ctx = json.loads(original_ctx)
562+
current_ctx = json.loads(ctx)
563+
except Exception as e:
564+
_logger.error("\n\nERROR CONTEXT JSON CONVERSION: %s\n\n" % e)
565+
return self.env.context.copy()
566+
else:
567+
if isinstance(import_ctx, dict) and isinstance(current_ctx, dict):
568+
import_ctx.update(current_ctx)
569+
return import_ctx
570+
return self.env.context.copy()
571+
568572
def _get_record_context(self):
569573
"""
570574
Get the context to execute the job
571575
"""
572-
# return {}
573-
company_ids = []
576+
ctx = self._get_abs_context(self.db_record().original_context,
577+
self.db_record().context)
574578
if self.company_id:
575-
company_ids = [self.company_id]
576-
context_txt = self.db_record().context or {}
577-
if isinstance(context_txt, str):
578-
context = json.loads(context_txt)
579-
else:
580-
context = json.loads(context_txt)
581-
context.update(
582-
{"job_uuid": self.uuid, "allowed_company_ids": company_ids})
583-
return context
579+
ctx.update({'allowed_company_ids': [self.company_id]})
580+
if self.uuid:
581+
ctx.update({"job_uuid": self.uuid})
582+
return ctx
584583

585584
@property
586585
def func(self):

queue_job/models/queue_job.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class QueueJob(models.Model):
5151
"(Default: {})",
5252
readonly=True,
5353
)
54+
original_context = fields.Char(
55+
string="Original Context Value",
56+
default="{}",
57+
help="This is the context dictionary that was used on import"
58+
)
5459

5560
model_name = fields.Char(string='Model', readonly=True)
5661
method_name = fields.Char(readonly=True)

queue_job/views/queue_job_views.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
</group>
5656
<group name="context_grp">
5757
<field name="context"/>
58+
<field name="original_context"/>
5859
</group>
5960
<group colspan="4">
6061
<div>

0 commit comments

Comments
 (0)