1111from datetime import datetime , timedelta
1212
1313import odoo
14+ from odoo .tools .safe_eval import safe_eval
1415
1516from .exception import FailedJobError , NoSuchJobError , RetryableJobError
1617
@@ -59,6 +60,7 @@ def __init__(
5960 description = None ,
6061 channel = None ,
6162 identity_key = None ,
63+ keep_context = False ,
6264 ):
6365 self .recordset = recordset
6466 self .priority = priority
@@ -67,6 +69,7 @@ def __init__(
6769 self .description = description
6870 self .channel = channel
6971 self .identity_key = identity_key
72+ self .keep_context = keep_context
7073
7174 def __getattr__ (self , name ):
7275 if name in self .recordset :
@@ -88,6 +91,7 @@ def delay(*args, **kwargs):
8891 description = self .description ,
8992 channel = self .channel ,
9093 identity_key = self .identity_key ,
94+ keep_context = self .keep_context ,
9195 )
9296
9397 return delay
@@ -339,6 +343,7 @@ def enqueue(
339343 description = None ,
340344 channel = None ,
341345 identity_key = None ,
346+ keep_context = False ,
342347 ):
343348 """Create a Job and enqueue it in the queue. Return the job uuid.
344349
@@ -359,6 +364,7 @@ def enqueue(
359364 description = description ,
360365 channel = channel ,
361366 identity_key = identity_key ,
367+ keep_context = keep_context ,
362368 )
363369 if new_job .identity_key :
364370 existing = new_job .job_record_with_same_identity_key ()
@@ -399,6 +405,7 @@ def __init__(
399405 description = None ,
400406 channel = None ,
401407 identity_key = None ,
408+ keep_context = False ,
402409 ):
403410 """ Create a Job
404411
@@ -423,8 +430,8 @@ def __init__(
423430 :param identity_key: A hash to uniquely identify a job, or a function
424431 that returns this hash (the function takes the job
425432 as argument)
426- :param env: Odoo Environment
427- :type env : :class:`odoo.api.Environment`
433+ :param keep_context: Determine if the current context should be restored
434+ :type keep_context : :bool
428435 """
429436 if args is None :
430437 args = ()
@@ -445,6 +452,7 @@ def __init__(
445452 self .recordset = recordset
446453
447454 self .env = env
455+ self .keep_context = keep_context
448456 self .job_model = self .env ["queue.job" ]
449457 self .job_model_name = "queue.job"
450458
@@ -594,8 +602,11 @@ def _store_values(self, create=False):
594602 "records" : self .recordset ,
595603 "args" : self .args ,
596604 "kwargs" : self .kwargs ,
605+ "context" : "{}" ,
597606 }
598607 )
608+ if self .keep_context :
609+ vals .update ({"context" : str (self .env .context .copy ())})
599610
600611 vals_from_model = self ._store_values_from_model ()
601612 # Sanitize values: make sure you cannot screw core values
@@ -615,6 +626,22 @@ def _store_values_from_model(self):
615626 vals = handler (self )
616627 return vals
617628
629+ def _get_record_context (self ):
630+ """
631+ Get the context to execute the job
632+ """
633+ # return {}
634+ company_ids = []
635+ if self .company_id :
636+ company_ids = [self .company_id ]
637+ context_txt = self .db_record ().context or {}
638+ if isinstance (context_txt , str ):
639+ context = safe_eval (context_txt )
640+ else :
641+ context = context_txt
642+ context .update ({"job_uuid" : self .uuid , "allowed_company_ids" : company_ids })
643+ return context
644+
618645 @property
619646 def func_string (self ):
620647 model = repr (self .recordset )
@@ -632,12 +659,8 @@ def func(self):
632659 # Because if you have many, you can have unexpected records due to ir.rule.
633660 # ir.rule use allowed_company_ids to load every records in many companies.
634661 # But most of the time, a job should be executed on a single company.
635- company_ids = []
636- if self .company_id :
637- company_ids = [self .company_id ]
638- recordset = self .recordset .with_context (
639- job_uuid = self .uuid , allowed_company_ids = company_ids
640- )
662+ context = self ._get_record_context ()
663+ recordset = self .recordset .with_context (** context )
641664 return getattr (recordset , self .method_name )
642665
643666 @property
0 commit comments