From 3985e11d962b17454fd3311d253698d651dc1c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Vi=20Or?= Date: Fri, 17 Feb 2023 11:17:26 +0100 Subject: [PATCH] [IMP] report_async: Change eta to scheduled time --- report_async/models/report_async.py | 15 +++++++++++++-- report_async/views/report_async.xml | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/report_async/models/report_async.py b/report_async/models/report_async.py index 974452a1ef..9906d682ce 100644 --- a/report_async/models/report_async.py +++ b/report_async/models/report_async.py @@ -2,6 +2,7 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) import base64 +from datetime import datetime, timedelta from odoo import api, fields, models, _ from odoo.tools.safe_eval import safe_eval from odoo.exceptions import UserError @@ -70,7 +71,7 @@ class ReportAsync(models.Model): help="List all files created by this report background process", ) - eta = fields.Datetime(string='Execute only after') + schedule_time = fields.Char(string='Schedule time') @api.multi def _compute_job(self): @@ -111,7 +112,9 @@ def run_async(self): action = self.env.ref(self.action_id.xml_id) result = action.read()[0] ctx = safe_eval(result.get('context', {})) - ctx.update({'async_process': True, 'eta': self.eta}) + ctx.update({'async_process': True}) + if self.schedule_time: + ctx.update({'eta': self._get_next_schedule_time()}) result['context'] = ctx return result @@ -162,3 +165,11 @@ def _send_email(self, attachment): template.send_mail(attachment.id, notif_layout='mail.mail_notification_light', force_send=False) + + def _get_next_schedule_time(self): + target_time = datetime.strptime(self.schedule_time, "%H:%M").time() + now = datetime.now() + target_datetime = datetime.combine(now.date(), target_time) + if now.time() > target_time: + target_datetime += datetime.timedelta(days=1) + return target_datetime diff --git a/report_async/views/report_async.xml b/report_async/views/report_async.xml index 680477e3f7..cbf6deaff0 100644 --- a/report_async/views/report_async.xml +++ b/report_async/views/report_async.xml @@ -73,7 +73,7 @@ -