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

[IMP] report_async: Add eta when async reporting #725

Merged
merged 2 commits into from
Aug 24, 2023
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
26 changes: 12 additions & 14 deletions report_async/models/ir_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@

from odoo import api, models


# Define all supported report_type
REPORT_TYPES = ['qweb-pdf', 'qweb-text',
'qweb-xml', 'csv',
'excel', 'xlsx']
REPORT_TYPES = ["qweb-pdf", "qweb-text", "qweb-xml", "csv", "excel", "xlsx"]


class Report(models.Model):
_inherit = 'ir.actions.report'
_inherit = "ir.actions.report"

@api.noguess
def report_action(self, docids, data=None, config=True):
res = super(Report, self).report_action(docids, data=data,
config=config)
if res['context'].get('async_process', False):
rpt_async_id = res['context']['active_id']
report_async = self.env['report.async'].browse(rpt_async_id)
if res['report_type'] in REPORT_TYPES:
report_async.with_delay().run_report(
res['context'].get('active_ids', []), data,
self.id, self._uid)
res = super(Report, self).report_action(docids, data=data, config=config)
if res["context"].get("async_process", False):
rpt_async_id = res["context"]["active_id"]
report_async = self.env["report.async"].browse(rpt_async_id)
if res["report_type"] in REPORT_TYPES:
report_async.with_delay(
eta=res["context"].get("eta", False)
).run_report(
res["context"].get("active_ids", []), data, self.id, self._uid
)
return {}
return res
13 changes: 13 additions & 0 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,6 +71,8 @@
help="List all files created by this report background process",
)

schedule_time = fields.Char(string='Schedule time')

@api.multi
def _compute_job(self):
for rec in self:
Expand Down Expand Up @@ -110,6 +113,8 @@
result = action.read()[0]
ctx = safe_eval(result.get('context', {}))
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 @@ -160,3 +165,11 @@
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 = fields.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 += 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: 2 additions & 0 deletions report_async/views/report_async.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
<field name="allow_async"/>
<field name="email_notify"
attrs="{'invisible': [('allow_async', '=', False)]}"/>
<field name="schedule_time" placeholder="23:30"
attrs="{'invisible': [('allow_async', '=', False)]}"/>
</group>
<group>
<field name="job_status"
Expand Down
Loading