diff --git a/queue_job/models/queue_job_channel.py b/queue_job/models/queue_job_channel.py index c5495ecacd..b91791b196 100644 --- a/queue_job/models/queue_job_channel.py +++ b/queue_job/models/queue_job_channel.py @@ -32,7 +32,10 @@ class QueueJobChannel(models.Model): compute="_compute_complete_name", store=True, readonly=True, recursive=True ) parent_id = fields.Many2one( - comodel_name="queue.job.channel", string="Parent Channel", ondelete="restrict" + comodel_name="queue.job.channel", + string="Parent Channel", + ondelete="restrict", + default=lambda self: self._default_parent_id(), ) job_function_ids = fields.One2many( comodel_name="queue.job.function", @@ -153,6 +156,13 @@ def _root_max_capacity(self): self.env.cr.dbname, rules, default=runner._max_capacity() ) + def _default_parent_id(self): + # empty while the root channel itself is being created at install + return ( + self.env.ref("queue_job.channel_root", raise_if_not_found=False) + or self.browse() + ) + @api.constrains("parent_id", "name") def parent_required(self): for record in self: @@ -169,7 +179,7 @@ def create(self, vals_list): new_vals_list = [] for vals in vals_list: name = vals.get("name") - parent_id = vals.get("parent_id") + parent_id = vals.get("parent_id", self._default_parent_id().id) if name and parent_id: existing = self.search( [("name", "=", name), ("parent_id", "=", parent_id)] diff --git a/queue_job/readme/USAGE.md b/queue_job/readme/USAGE.md index deb6fe2aca..3cbdc1df28 100644 --- a/queue_job/readme/USAGE.md +++ b/queue_job/readme/USAGE.md @@ -168,7 +168,6 @@ Example of channel: ``` XML sale - ``` diff --git a/queue_job/tests/test_model_job_channel.py b/queue_job/tests/test_model_job_channel.py index 19eaf320be..4f4c3b58ba 100644 --- a/queue_job/tests/test_model_job_channel.py +++ b/queue_job/tests/test_model_job_channel.py @@ -33,6 +33,18 @@ def test_channel_create(self): self.assertEqual(channel2.name, "test") self.assertEqual(channel2.complete_name, "root.test.test") + def test_channel_create_default_parent(self): + channel = self.Channel.create({"name": "test"}) + self.assertEqual(channel.parent_id, self.root_channel) + self.assertEqual(channel.complete_name, "root.test") + + def test_channel_write_empty_parent(self): + channel = self.Channel.create({"name": "test"}) + with self.assertRaisesRegex( + exceptions.ValidationError, "Parent channel required." + ): + channel.parent_id = False + @odoo.tools.mute_logger("odoo.sql_db") def test_channel_complete_name_uniq(self): channel = self.Channel.create( diff --git a/queue_job_batch/data/queue_job_channel_data.xml b/queue_job_batch/data/queue_job_channel_data.xml index 04dd328c59..12ac98ff61 100644 --- a/queue_job_batch/data/queue_job_channel_data.xml +++ b/queue_job_batch/data/queue_job_channel_data.xml @@ -1,6 +1,5 @@ queue.job.batch - diff --git a/queue_job_cron/data/data.xml b/queue_job_cron/data/data.xml index 0a56d9df32..94195e9ea8 100644 --- a/queue_job_cron/data/data.xml +++ b/queue_job_cron/data/data.xml @@ -2,7 +2,6 @@ ir_cron - diff --git a/test_queue_job/tests/test_job_channels.py b/test_queue_job/tests/test_job_channels.py index aff250d252..d83191e836 100644 --- a/test_queue_job/tests/test_job_channels.py +++ b/test_queue_job/tests/test_job_channels.py @@ -28,7 +28,9 @@ def test_channel_complete_name(self): def test_channel_tree(self): with self.assertRaises(exceptions.ValidationError): - self.channel_model.create({"name": "sub"}) + self.channel_model.create({"name": "sub", "parent_id": False}) + channel = self.channel_model.create({"name": "sub"}) + self.assertEqual(channel.parent_id, self.root_channel) def test_channel_root(self): with self.assertRaises(exceptions.UserError):