From 30c3906a2b47749155e9c263d6b46f53cf9d568a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Honor=C3=A9?= Date: Mon, 2 Aug 2021 16:12:16 +0200 Subject: [PATCH 1/2] [13.0][IMP] queue_job current company Use the current company to trigger the job (+ add related tests) --- queue_job/job.py | 10 ++++++- test_queue_job/tests/test_job.py | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/queue_job/job.py b/queue_job/job.py index 349a73c8ce..49d69c7b80 100644 --- a/queue_job/job.py +++ b/queue_job/job.py @@ -628,7 +628,15 @@ def db_record(self): @property def func(self): - recordset = self.recordset.with_context(job_uuid=self.uuid) + user = self.env["res.users"].browse(self.user_id) + company_ids = user.company_ids.ids + # Insert the current company in top position + company_ids.insert(0, self.company_id) + # Remove duplicates but keep order + company_ids = list(dict.fromkeys(company_ids)) + recordset = self.recordset.with_context( + job_uuid=self.uuid, allowed_company_ids=company_ids + ) return getattr(recordset, self.method_name) @property diff --git a/test_queue_job/tests/test_job.py b/test_queue_job/tests/test_job.py index e0224ebf3d..7c4bc6f4a7 100644 --- a/test_queue_job/tests/test_job.py +++ b/test_queue_job/tests/test_job.py @@ -200,6 +200,51 @@ def test_store_extra_data(self): stored.invalidate_cache() self.assertEqual(stored.additional_info, "JUST_TESTING_BUT_FAILED") + def test_company_simple(self): + company = self.env.ref("base.main_company") + eta = datetime.now() + timedelta(hours=5) + test_job = Job( + self.method, + args=("o", "k"), + kwargs={"return_context": 1}, + priority=15, + eta=eta, + description="My description", + ) + test_job.worker_pid = 99999 # normally set on "set_start" + test_job.company_id = company.id + test_job.store() + job_read = Job.load(self.env, test_job.uuid) + self.assertEqual(test_job.func, job_read.func) + result_ctx = test_job.func(*tuple(test_job.args), **test_job.kwargs) + self.assertEqual(result_ctx.get("allowed_company_ids"), company.ids) + + def test_company_complex(self): + company1 = self.env.ref("base.main_company") + company2 = company1.create({"name": "Queue job company"}) + companies = company1 | company2 + self.env.user.write({"company_ids": [(6, False, companies.ids)]}) + # Ensure the main company still the first + self.assertEqual(self.env.user.company_id, company1) + eta = datetime.now() + timedelta(hours=5) + test_job = Job( + self.method, + args=("o", "k"), + kwargs={"return_context": 1}, + priority=15, + eta=eta, + description="My description", + ) + test_job.worker_pid = 99999 # normally set on "set_start" + test_job.company_id = company2.id + test_job.store() + job_read = Job.load(self.env, test_job.uuid) + self.assertEqual(test_job.func, job_read.func) + result_ctx = test_job.func(*tuple(test_job.args), **test_job.kwargs) + self.assertEqual( + result_ctx.get("allowed_company_ids"), [company2.id, company1.id] + ) + def test_read(self): eta = datetime.now() + timedelta(hours=5) test_job = Job( From 4d43bd8c76ddaffe5a07b18a7da02b144458e571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Honor=C3=A9?= Date: Wed, 18 Aug 2021 09:22:17 +0200 Subject: [PATCH 2/2] [13.0][FIX] queu_job: allowed_company_ids. Fill allowed_company_ids from context with the job's company instead of every allowed companies of the user. Because most of the time, a job is related to only one company. And adding every allowed companies of the user into the context may load some unexpected records (during search for example). Because standards ir.rule use ['|',('company_id','=',False),('company_id', 'in', company_ids)] and this 'company_ids' is filled with every allowed companies from the context. --- queue_job/job.py | 13 +++++++------ test_queue_job/tests/test_job.py | 4 +--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/queue_job/job.py b/queue_job/job.py index 49d69c7b80..0486a20ae8 100644 --- a/queue_job/job.py +++ b/queue_job/job.py @@ -628,12 +628,13 @@ def db_record(self): @property def func(self): - user = self.env["res.users"].browse(self.user_id) - company_ids = user.company_ids.ids - # Insert the current company in top position - company_ids.insert(0, self.company_id) - # Remove duplicates but keep order - company_ids = list(dict.fromkeys(company_ids)) + # We can fill only one company into allowed_company_ids. + # Because if you have many, you can have unexpected records due to ir.rule. + # ir.rule use allowed_company_ids to load every records in many companies. + # But most of the time, a job should be executed on a single company. + company_ids = [] + if self.company_id: + company_ids = [self.company_id] recordset = self.recordset.with_context( job_uuid=self.uuid, allowed_company_ids=company_ids ) diff --git a/test_queue_job/tests/test_job.py b/test_queue_job/tests/test_job.py index 7c4bc6f4a7..9b161789b5 100644 --- a/test_queue_job/tests/test_job.py +++ b/test_queue_job/tests/test_job.py @@ -241,9 +241,7 @@ def test_company_complex(self): job_read = Job.load(self.env, test_job.uuid) self.assertEqual(test_job.func, job_read.func) result_ctx = test_job.func(*tuple(test_job.args), **test_job.kwargs) - self.assertEqual( - result_ctx.get("allowed_company_ids"), [company2.id, company1.id] - ) + self.assertEqual(result_ctx.get("allowed_company_ids"), company2.ids) def test_read(self): eta = datetime.now() + timedelta(hours=5)