diff --git a/report_async/migrations/12.0.1.2.0/post-migration.py b/report_async/migrations/12.0.1.2.0/post-migration.py new file mode 100644 index 0000000000..bfba736a8e --- /dev/null +++ b/report_async/migrations/12.0.1.2.0/post-migration.py @@ -0,0 +1,19 @@ +from openupgradelib import openupgrade + + +def populate_report_async_id(env): + openupgrade.logged_query( + env.cr, + """ + UPDATE queue_job qj + SET report_async_id = ra.id + FROM report_async ra + WHERE qj.func_string LIKE '%report.async(' || ra.id || ',%' + AND qj.user_id = ra.create_uid + """ + ) + + +@openupgrade.migrate() +def migrate(env, version): + populate_report_async_id(env) diff --git a/report_async/models/__init__.py b/report_async/models/__init__.py index 2e6988867b..5824676d65 100644 --- a/report_async/models/__init__.py +++ b/report_async/models/__init__.py @@ -3,3 +3,4 @@ from . import report_async from . import ir_report +from . import queue_job diff --git a/report_async/models/ir_report.py b/report_async/models/ir_report.py index ad8df751f8..f58d330410 100644 --- a/report_async/models/ir_report.py +++ b/report_async/models/ir_report.py @@ -17,10 +17,13 @@ def report_action(self, docids, data=None, config=True): rpt_async_id = res["context"]["active_id"] report_async = self.env["report.async"].browse(rpt_async_id) if res["report_type"] in REPORT_TYPES: - report_async.with_delay( + job = report_async.with_delay( eta=res["context"].get("eta", False) ).run_report( res["context"].get("active_ids", []), data, self.id, self._uid ) + if job: + (self.env['queue.job'].search([('uuid', '=', job.uuid)]) + .write({'report_async_id': report_async.id})) return {} return res diff --git a/report_async/models/queue_job.py b/report_async/models/queue_job.py new file mode 100644 index 0000000000..6c26c0df97 --- /dev/null +++ b/report_async/models/queue_job.py @@ -0,0 +1,12 @@ +from odoo import models, fields + + +class QueueJob(models.Model): + _inherit = 'queue.job' + + report_async_id = fields.Many2one( + comodel_name='report.async', + string='Report Async', + index=True, + ondelete='cascade', + ) diff --git a/report_async/models/report_async.py b/report_async/models/report_async.py index b5925abc53..85ca0f2e20 100644 --- a/report_async/models/report_async.py +++ b/report_async/models/report_async.py @@ -47,9 +47,10 @@ class ReportAsync(models.Model): help="Only user in selected groups can use this report." "If left blank, everyone can use", ) - job_ids = fields.Many2many( + job_ids = fields.One2many( comodel_name='queue.job', - compute='_compute_job', + inverse_name='report_async_id', + string='Jobs', help="List all jobs related to this running report", ) job_status = fields.Selection( @@ -58,11 +59,11 @@ class ReportAsync(models.Model): ('started', 'Started'), ('done', 'Done'), ('failed', 'Failed')], - compute='_compute_job', + compute='_compute_job_status_info', help="Latest Job Status", ) job_info = fields.Text( - compute='_compute_job', + compute='_compute_job_status_info', help="Latest Job Error Message", ) file_ids = fields.Many2many( @@ -74,16 +75,11 @@ class ReportAsync(models.Model): schedule_time = fields.Char(string='Schedule time') @api.multi - def _compute_job(self): + def _compute_job_status_info(self): for rec in self: - rec.job_ids = self.sudo().env['queue.job'].search( - [('func_string', 'like', 'report.async(%s,)' % rec.id), - ('user_id', '=', self._uid)], - order='id desc') - rec.job_status = (rec.job_ids[0].sudo().state - if rec.job_ids else False) - rec.job_info = (rec.job_ids[0].sudo().exc_info - if rec.job_ids else False) + jobs = rec.sudo().job_ids + rec.job_status = (jobs[0].state if jobs else False) + rec.job_info = (jobs[0].exc_info if jobs else False) @api.multi def _compute_file(self):