[IMP] report_async: add schedule_date field

This commit is contained in:
RicardCForgeFlow
2024-07-03 15:50:03 +02:00
parent f98265e385
commit 27e4d3b6f3
2 changed files with 27 additions and 18 deletions

View File

@@ -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

View File

@@ -73,7 +73,9 @@
<field name="allow_async"/>
<field name="email_notify"
attrs="{'invisible': [('allow_async', '=', False)]}"/>
<field name="schedule_time" widget="datetime" placeholder="YYYY-MM-DD HH:MM"
<field name="schedule_time" placeholder="23:30"
attrs="{'invisible': [('allow_async', '=', False)]}"/>
<field name="schedule_date" widget="date"
attrs="{'invisible': [('allow_async', '=', False)]}"/>
</group>
<group>