Skip to content

Commit

Permalink
queue_job: add mixin to link jobs
Browse files Browse the repository at this point in the history
You can easily link jobs to their related records.
  • Loading branch information
simahawk committed Oct 24, 2023
1 parent 306093d commit 9bc36c1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions queue_job/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from . import queue_job
from . import queue_job_channel
from . import queue_job_function
from . import queue_job_consumer_mixin
14 changes: 12 additions & 2 deletions queue_job/models/queue_job.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2013-2020 Camptocamp SA
# Copyright 2013 Camptocamp SA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)

import logging
Expand Down Expand Up @@ -234,10 +234,20 @@ def create(self, vals_list):
raise exceptions.AccessError(
_("Queue jobs must be created by calling 'with_delay()'.")
)
return super(
records = super(
QueueJob,
self.with_context(mail_create_nolog=True, mail_create_nosubscribe=True),
).create(vals_list)
records._handle_origin_link()
return records

def _handle_origin_link(self):
"""Update job relation on origin records."""
for qjob in self:
model = self.env.get(qjob.model_name)
if not model or model and not hasattr(model, "_collect_queue_job"):
continue
qjob.records._collect_queue_job(qjob)

Check warning on line 250 in queue_job/models/queue_job.py

View check run for this annotation

Codecov / codecov/patch

queue_job/models/queue_job.py#L249-L250

Added lines #L249 - L250 were not covered by tests

def write(self, vals):
if self.env.context.get("_job_edit_sentinel") is not self.EDIT_SENTINEL:
Expand Down
40 changes: 40 additions & 0 deletions queue_job/models/queue_job_consumer_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2023 Camptocamp SA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)

from odoo import fields, models
from odoo.tools import safe_eval


class QueueJobConsumerMixin(models.AbstractModel):
"""Provide common features to models using jobs."""

_name = "queue.job.consumer.mixin"
_description = _name

job_ids = fields.Many2many(
string="Related jobs",
comodel_name="queue.job",
column1="rec_id",
column2="job_id",
)

def _collect_queue_job(self, qjob):
"""Update job relation w/ queue jobs."""
for rec in self:
rec.job_ids |= qjob

Check warning on line 24 in queue_job/models/queue_job_consumer_mixin.py

View check run for this annotation

Codecov / codecov/patch

queue_job/models/queue_job_consumer_mixin.py#L24

Added line #L24 was not covered by tests

def action_view_related_jobs(self):
self.ensure_one()
xmlid = "queue_job.action_queue_job"
action = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
action["domain"] = [("id", "in", self.job_ids.ids)]

Check warning on line 30 in queue_job/models/queue_job_consumer_mixin.py

View check run for this annotation

Codecov / codecov/patch

queue_job/models/queue_job_consumer_mixin.py#L27-L30

Added lines #L27 - L30 were not covered by tests
# Purge default search filters from ctx to avoid hiding records
ctx = action.get("context", {})

Check warning on line 32 in queue_job/models/queue_job_consumer_mixin.py

View check run for this annotation

Codecov / codecov/patch

queue_job/models/queue_job_consumer_mixin.py#L32

Added line #L32 was not covered by tests
if isinstance(ctx, str):
ctx = safe_eval.safe_eval(ctx, self.env.context)

Check warning on line 34 in queue_job/models/queue_job_consumer_mixin.py

View check run for this annotation

Codecov / codecov/patch

queue_job/models/queue_job_consumer_mixin.py#L34

Added line #L34 was not covered by tests
action["context"] = {
k: v for k, v in ctx.items() if not k.startswith("search_default_")
}
# Drop ID otherwise the context will be loaded from the action's record
action.pop("id")
return action

Check warning on line 40 in queue_job/models/queue_job_consumer_mixin.py

View check run for this annotation

Codecov / codecov/patch

queue_job/models/queue_job_consumer_mixin.py#L39-L40

Added lines #L39 - L40 were not covered by tests

0 comments on commit 9bc36c1

Please sign in to comment.