Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions queue_job/models/queue_job_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand All @@ -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)]
Expand Down
1 change: 0 additions & 1 deletion queue_job/readme/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ Example of channel:
``` XML
<record id="channel_sale" model="queue.job.channel">
<field name="name">sale</field>
<field name="parent_id" ref="queue_job.channel_root" />
</record>
```

Expand Down
12 changes: 12 additions & 0 deletions queue_job/tests/test_model_job_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion queue_job_batch/data/queue_job_channel_data.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<odoo noupdate="1">
<record model="queue.job.channel" id="channel_queue_job_batch">
<field name="name">queue.job.batch</field>
<field name="parent_id" ref="queue_job.channel_root" />
</record>
</odoo>
1 change: 0 additions & 1 deletion queue_job_cron/data/data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<odoo>
<record model="queue.job.channel" id="channel_root_ir_cron">
<field name="name">ir_cron</field>
<field name="parent_id" ref="queue_job.channel_root" />
</record>
<record model="queue.job.function" id="job_function_ir_cron_run_job_as_queue_job">
<field name="model_id" ref="base.model_ir_cron" />
Expand Down
4 changes: 3 additions & 1 deletion test_queue_job/tests/test_job_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading