Skip to content

Commit

Permalink
[IMP] report_async: Change eta to scheduled time
Browse files Browse the repository at this point in the history
  • Loading branch information
HviorForgeFlow committed Feb 17, 2023
1 parent c769dc1 commit 3985e11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 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 @@ -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):
Expand Down Expand Up @@ -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()})

Check warning on line 117 in report_async/models/report_async.py

View check run for this annotation

Codecov / codecov/patch

report_async/models/report_async.py#L117

Added line #L117 was not covered by tests
result['context'] = ctx
return result

Expand Down Expand Up @@ -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)

Check warning on line 172 in report_async/models/report_async.py

View check run for this annotation

Codecov / codecov/patch

report_async/models/report_async.py#L170-L172

Added lines #L170 - L172 were not covered by tests
if now.time() > target_time:
target_datetime += datetime.timedelta(days=1)
return target_datetime

Check warning on line 175 in report_async/models/report_async.py

View check run for this annotation

Codecov / codecov/patch

report_async/models/report_async.py#L174-L175

Added lines #L174 - L175 were not covered by tests
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="eta"
<field name="schedule_time" placeholder="23:30"
attrs="{'invisible': [('allow_async', '=', False)]}"/>
</group>
<group>
Expand Down

0 comments on commit 3985e11

Please sign in to comment.