From 27e4d3b6f3728d58bbcc4a0a160e250679ebf92f Mon Sep 17 00:00:00 2001 From: RicardCForgeFlow Date: Wed, 3 Jul 2024 15:50:03 +0200 Subject: [PATCH] [IMP] report_async: add schedule_date field --- report_async/models/report_async.py | 41 +++++++++++++++++------------ report_async/views/report_async.xml | 4 ++- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/report_async/models/report_async.py b/report_async/models/report_async.py index d98e862b2..a119197f9 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 @@ -69,8 +70,14 @@ class ReportAsync(models.Model): compute='_compute_file', help="List all files created by this report background process", ) - - schedule_time = fields.Datetime(string='Schedule Time') + schedule_time = fields.Char( + string='Schedule Time', + help="Time when the job will be executed", + ) + schedule_date = fields.Date( + string='Schedule Date', + help="Date when the job will be executed", + ) @api.multi def _compute_job(self): @@ -113,7 +120,7 @@ class ReportAsync(models.Model): ctx = safe_eval(result.get('context', {})) ctx.update({'async_process': True}) if self.schedule_time: - ctx.update({'eta': self.schedule_time}) + ctx.update({'eta': self._get_next_schedule_time()}) result['context'] = ctx return result @@ -165,18 +172,18 @@ class ReportAsync(models.Model): notif_layout='mail.mail_notification_light', force_send=False) - @api.model - def create(self, vals): - if ('schedule_time' in vals and - fields.Datetime.from_string(vals['schedule_time']) - < fields.Datetime.now()): - raise UserError(_('The scheduled time must be in the future.')) - return super(ReportAsync, self).create(vals) + def _get_next_schedule_time(self): + now = fields.Datetime.now() + target_time = datetime.strptime(self.schedule_time, "%H:%M").time() \ + if self.schedule_time else now.time() - @api.multi - def write(self, vals): - if ('schedule_time' in vals and - fields.Datetime.from_string(vals['schedule_time']) - < fields.Datetime.now()): - raise UserError(_('The scheduled time must be in the future.')) - return super(ReportAsync, self).write(vals) + if self.schedule_date: + target_datetime = datetime.combine(self.schedule_date, target_time) + if now > target_datetime: + raise UserError(_('The scheduled time must be in the future.')) + else: + target_datetime = datetime.combine(now.date(), target_time) + if now > target_datetime: + target_datetime += timedelta(days=1) + + return target_datetime diff --git a/report_async/views/report_async.xml b/report_async/views/report_async.xml index 21808683a..f3589e13b 100644 --- a/report_async/views/report_async.xml +++ b/report_async/views/report_async.xml @@ -73,7 +73,9 @@ - +