Skip to content

Commit

Permalink
Merge 52d014c into bd4cad3
Browse files Browse the repository at this point in the history
  • Loading branch information
kittiu committed Jan 13, 2020
2 parents bd4cad3 + 52d014c commit 21ec93d
Show file tree
Hide file tree
Showing 20 changed files with 1,047 additions and 0 deletions.
1 change: 1 addition & 0 deletions oca_dependencies.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
server-tools
server-backend https://github.com/OCA/server-backend
web
queue
# Until merge
server-backend-mig-dbsource-sqlite https://github.com/Eficent/server-backend 12.0-mig-base_external_dbsource_sqlite
server-backend-mig-dbsource-mysql https://github.com/Eficent/server-backend 12.0-mig-base_external_dbsource_mysql
5 changes: 5 additions & 0 deletions report_async/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

from . import models
from . import wizard
26 changes: 26 additions & 0 deletions report_async/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)
{
'name': 'Report Async',
'summary': 'Central place to run reports live or async',
'version': '12.0.1.0.0',
'author': 'Ecosoft, Odoo Community Association (OCA)',
'license': 'AGPL-3',
'website': 'https://github.com/OCA/queue',
'category': 'Generic Modules',
'depends': [
'queue_job',
],
'data': [
'security/ir.model.access.csv',
'security/ir_rule.xml',
'views/report_async.xml',
'wizard/print_report_wizard.xml',
],
'demo': [
'demo/report_async_demo.xml',
],
'installable': True,
'maintainers': ['kittiu'],
'development_status': 'Beta',
}
9 changes: 9 additions & 0 deletions report_async/demo/report_async_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

<record id="report_async_print_document" model="report.async">
<field name="action_id" eval="ref('report_async.action_print_report_wizard')"/>
<field name="allow_async" eval="0"/>
</record>

</odoo>
5 changes: 5 additions & 0 deletions report_async/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

from . import report_async
from . import ir_report
28 changes: 28 additions & 0 deletions report_async/models/ir_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

from odoo import api, models


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


class Report(models.Model):
_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)
return {}
return res
181 changes: 181 additions & 0 deletions report_async/models/report_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

import base64
from odoo import api, fields, models, _
from odoo.tools.safe_eval import safe_eval
from odoo.exceptions import UserError
from odoo.addons.queue_job.job import job


# Define all supported report_type
REPORT_TYPES_FUNC = {'qweb-pdf': 'render_qweb_pdf',
'qweb-text': 'render_qweb_text',
'qweb-xml': 'render_qweb_xml',
'csv': 'render_csv',
'excel': 'render_excel',
'xlsx': 'render_xlsx', }


class ReportAsync(models.Model):
_name = 'report.async'
_description = 'Report Async'

action_id = fields.Many2one(
comodel_name='ir.actions.act_window',
string='Reports',
required=True,
)
allow_async = fields.Boolean(
string='Allow Async',
default=False,
help="This is not automatic field, please check if you want to allow "
"this report in background process",
)
name = fields.Char(
string='Name',
related='action_id.display_name',
)
email_notify = fields.Boolean(
string='Email Notification',
help="Send email with link to report, when it is ready",
)
group_ids = fields.Many2many(
string='Groups',
comodel_name='res.groups',
help="Only user in selected groups can use this report."
"If left blank, everyone can use",
)
job_ids = fields.Many2many(
comodel_name='queue.job',
compute='_compute_job',
help="List all jobs related to this running report",
)
job_status = fields.Selection(
selection=[('pending', 'Pending'),
('enqueued', 'Enqueued'),
('started', 'Started'),
('done', 'Done'),
('failed', 'Failed')],
compute='_compute_job',
help="Latest Job Status",
)
job_info = fields.Text(
compute='_compute_job',
help="Latest Job Error Message",
)
file_ids = fields.Many2many(
comodel_name='ir.attachment',
compute='_compute_file',
help="List all files created by this report background process",
)

@api.multi
def _compute_job(self):
for rec in self:
rec.job_ids = self.sudo().env['queue.job'].search(
[('func_string', 'like', 'report.async(%s,)' % rec.id),
('user_id', '=', self._uid)],
order='id desc')
rec.job_status = (rec.job_ids[0].sudo().state
if rec.job_ids else False)
rec.job_info = (rec.job_ids[0].sudo().exc_info
if rec.job_ids else False)

@api.multi
def _compute_file(self):
for rec in self:
rec.file_ids = self.env['ir.attachment'].search(
[('res_model', '=', 'report.async'),
('res_id', '=', rec.id),
('create_uid', '=', self._uid)],
order='id desc')

def run_now(self):
self.ensure_one()
action = self.env.ref(self.action_id.xml_id)
result = action.read()[0]
ctx = safe_eval(result.get('context', {}))
ctx.update({'async_process': False})
result['context'] = ctx
return result

@api.multi
def run_async(self):
self.ensure_one()
if not self.allow_async:
raise UserError(_('Background process not allowed.'))
action = self.env.ref(self.action_id.xml_id)
result = action.read()[0]
ctx = safe_eval(result.get('context', {}))
ctx.update({'async_process': True})
result['context'] = ctx
return result

@api.multi
def view_files(self):
self.ensure_one()
action = self.env.ref('report_async.action_view_files')
result = action.read()[0]
result['domain'] = [('id', 'in', self.file_ids.ids)]
return result

@api.multi
def view_jobs(self):
self.ensure_one()
action = self.env.ref('queue_job.action_queue_job')
result = action.read()[0]
result['domain'] = [('id', 'in', self.job_ids.ids)]
result['context'] = {}
return result

@api.model
@job
def run_report(self, docids, data, report_id, user_id):
report = self.env['ir.actions.report'].browse(report_id)
func = REPORT_TYPES_FUNC[report.report_type]
# Run report
out_file, file_ext = getattr(report, func)(docids, data)
out_file = base64.b64encode(out_file)
out_name = '%s.%s' % (report.name, file_ext)
# Save report to attachment
user = self.env['res.users'].browse(user_id)
attachment = self.env['ir.attachment'].sudo().create({
'name': out_name,
'datas': out_file,
'datas_fname': out_name,
'type': 'binary',
'res_model': 'report.async',
'res_id': self.id,
})
self._cr.execute("""
UPDATE ir_attachment SET create_uid = %s, write_uid = %s
WHERE id = %s""", (self._uid, self._uid, attachment.id))
# Send email
if self.email_notify:
self._send_email(user, attachment)

def _send_email(self, user, attachment):
base_url = self.env['ir.config_parameter'].\
sudo().get_param('web.base.url')
url = "{}/web/content/ir.attachment/{}/datas/{}?download=true".\
format(base_url, attachment.id, attachment.name, )

odoo_bot = self.sudo().env.ref('base.partner_root')
email_from = odoo_bot.email
description = attachment.name
self.env['mail.mail'].create({
'email_from': email_from,
'reply_to': email_from,
'recipient_ids': [(6, 0, [user.partner_id.id])],
'subject': _("Export {} {}").format(
description, fields.Date.to_string(fields.Date.today())),
'body_html': _("""
<p>Your report is available <a href="{}">here</a>.</p>
<p>&nbsp;</p>
<p><span style="color: #808080;">
This is an automated message please do not reply.
</span></p>
""").format(url),
'auto_delete': True,
})
1 change: 1 addition & 0 deletions report_async/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Kitti U. <kittiu@ecosoft.co.th>
16 changes: 16 additions & 0 deletions report_async/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
The new menu "Report Center" is the central place to host your reports in one place.
From here, there are 2 ways to launch the report,

1. Run Now - run report immediately as per normal.
2. Run Background - put the report execution to queue job.

By using the queue job, option 2 is great for long running report.
The report file will be saved for later use, with the option to send report
by email as soon as it is ready.

Notes:

* Only user with Technical Feature rights can manage the report.
* Every internal user will have right to execute the report allowed for his/her groups.
* The files created are owned and viewable only by the person who run the report.
* Job queue manager can also see all jobs for each reports.
19 changes: 19 additions & 0 deletions report_async/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Menu: Dashboard > Report Center

As Technical Feature users, you can manage reports for Report Center.

- **Report:** choose the report (a window action). Although the option show all window actions
it only make sense for window actions that launch reports.
- **Allow Async:** check this, if you want the report to run in background too, suitable for
report that return file as result, i.e., pdf/xlsx/csv/txt.
- **Email Notification:** if checked, once the background process is completed, email with link to download
report will be sent.
- **Groups:** select user groups allowed to use this report. If left blank, all user can use.

As normal user, you can run your reports from Report Center

- **Run Now button:** to run report immediately as per normal.
- **Run Background button:** to run report asynchronously. Fall back to run now, if not report that produce file.
- **Job Status:** show status of the latest run job. If job fail, exception error will also shown
- **Files:** show all files being produced by the job as run by the user.
- **Jobs:** show all jobs triggered by this report as run by the user. Only job queue manager have access to this button.
3 changes: 3 additions & 0 deletions report_async/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
access_report_async,report.async.user,model_report_async,base.group_user,1,0,0,0
access_report_async_sudo,report.async.sudo,model_report_async,base.group_no_one,1,1,1,1
23 changes: 23 additions & 0 deletions report_async/security/ir_rule.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="rule_report_async_access_by_group" model="ir.rule">
<field name="name">Report Async by Groups</field>
<field name="model_id" ref="model_report_async"/>
<field name="groups" eval="[(4, ref('base.group_user'))]"/>
<field name="perm_read" eval="True"/>
<field name="perm_create" eval="False"/>
<field name="perm_write" eval="False"/>
<field name="perm_unlink" eval="False"/>
<field name="domain_force">['|', ('group_ids', '=', False), ('group_ids', 'in', [g.id for g in user.groups_id])]</field>
</record>
<record id="rule_report_async_access_all" model="ir.rule">
<field name="name">Report Async by Groups</field>
<field name="model_id" ref="model_report_async"/>
<field name="groups" eval="[(4, ref('base.group_no_one'))]"/>
<field name="perm_read" eval="True"/>
<field name="perm_create" eval="True"/>
<field name="perm_write" eval="True"/>
<field name="perm_unlink" eval="True"/>
<field name="domain_force">[(1,'=', 1)]</field>
</record>
</odoo>
Binary file added report_async/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 21ec93d

Please sign in to comment.