Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][IMP] report_async: add schedule_date field #904

Merged
merged 2 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions report_async/models/report_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# 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 @@ -71,7 +70,7 @@ class ReportAsync(models.Model):
help="List all files created by this report background process",
)

schedule_time = fields.Char(string='Schedule time')
schedule_time = fields.Datetime(string='Schedule Time')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would need migration scripts. Can we adapt the code in a way to avoid this change?


@api.multi
def _compute_job(self):
Expand Down Expand Up @@ -114,7 +113,7 @@ def run_async(self):
ctx = safe_eval(result.get('context', {}))
ctx.update({'async_process': True})
if self.schedule_time:
ctx.update({'eta': self._get_next_schedule_time()})
ctx.update({'eta': self.schedule_time})
result['context'] = ctx
return result

Expand Down Expand Up @@ -166,10 +165,18 @@ def _send_email(self, attachment):
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 = fields.Datetime.now()
target_datetime = datetime.combine(now.date(), target_time)
if now.time() > target_time:
target_datetime += timedelta(days=1)
return target_datetime
@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)
2 changes: 1 addition & 1 deletion report_async/views/report_async.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<field name="allow_async"/>
<field name="email_notify"
attrs="{'invisible': [('allow_async', '=', False)]}"/>
<field name="schedule_time" placeholder="23:30"
<field name="schedule_time" widget="datetime" placeholder="YYYY-MM-DD HH:MM"
attrs="{'invisible': [('allow_async', '=', False)]}"/>
</group>
<group>
Expand Down
Loading