Skip to content

Commit

Permalink
[IMP] report_async: add schedule_date field
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardCForgeFlow committed Jul 3, 2024
1 parent f98265e commit 27e4d3b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
43 changes: 25 additions & 18 deletions report_async/models/report_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -113,7 +120,7 @@ def run_async(self):
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

Expand Down Expand Up @@ -165,18 +172,18 @@ def _send_email(self, attachment):
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)

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

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
4 changes: 3 additions & 1 deletion report_async/views/report_async.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 27e4d3b

Please sign in to comment.