[IMP] report_batch: black, isort, prettier
parent
912fc102cc
commit
45d3686a7b
|
@ -3,20 +3,15 @@
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
{
|
{
|
||||||
'name': 'Batch Report Printing',
|
"name": "Batch Report Printing",
|
||||||
'summary': 'Ability to print multiple QWeb reports in a single batch.',
|
"summary": "Ability to print multiple QWeb reports in a single batch.",
|
||||||
'version': '11.0.1.0.0',
|
"version": "11.0.1.0.0",
|
||||||
'license': 'AGPL-3',
|
"license": "AGPL-3",
|
||||||
'author': 'Open Source Integrators, Odoo Community Association (OCA)',
|
"author": "Open Source Integrators, Odoo Community Association (OCA)",
|
||||||
'category': 'Reporting',
|
"category": "Reporting",
|
||||||
'website': 'http://www.opensourceintegrators.com',
|
"website": "http://www.opensourceintegrators.com",
|
||||||
'depends': [
|
"depends": ["stock",],
|
||||||
'stock',
|
"data": ["security/ir.model.access.csv", "views/ir_action_report_view.xml",],
|
||||||
],
|
"installable": True,
|
||||||
'data': [
|
"maintainers": ["bodedra"],
|
||||||
'security/ir.model.access.csv',
|
|
||||||
'views/ir_action_report_view.xml',
|
|
||||||
],
|
|
||||||
'installable': True,
|
|
||||||
'maintainers': ['bodedra'],
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,86 +2,98 @@
|
||||||
# Copyright (C) 2019 Open Source Integrators
|
# Copyright (C) 2019 Open Source Integrators
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
from odoo import api, fields, models
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
from odoo import api, fields, models
|
||||||
|
|
||||||
|
|
||||||
class IrActionsReportSubreport(models.Model):
|
class IrActionsReportSubreport(models.Model):
|
||||||
_name = 'ir.actions.report.subreport'
|
_name = "ir.actions.report.subreport"
|
||||||
_description = 'Report Subreport'
|
_description = "Report Subreport"
|
||||||
_order = 'sequence'
|
_order = "sequence"
|
||||||
|
|
||||||
parent_report_id = fields.Many2one('ir.actions.report', ondelete='cascade')
|
parent_report_id = fields.Many2one("ir.actions.report", ondelete="cascade")
|
||||||
sequence = fields.Integer(default=10)
|
sequence = fields.Integer(default=10)
|
||||||
model = fields.Char(related='parent_report_id.model')
|
model = fields.Char(related="parent_report_id.model")
|
||||||
subreport_id = fields.Many2one(
|
subreport_id = fields.Many2one(
|
||||||
'ir.actions.report', string='Subreport', required=True)
|
"ir.actions.report", string="Subreport", required=True
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class IrActionsReport(models.Model):
|
class IrActionsReport(models.Model):
|
||||||
_inherit = 'ir.actions.report'
|
_inherit = "ir.actions.report"
|
||||||
|
|
||||||
subreport_ids = fields.One2many(
|
subreport_ids = fields.One2many("ir.actions.report.subreport", "parent_report_id")
|
||||||
'ir.actions.report.subreport', 'parent_report_id')
|
model_id = fields.Many2one("ir.model", string="Model")
|
||||||
model_id = fields.Many2one('ir.model', string='Model')
|
|
||||||
|
|
||||||
@api.onchange('model_id')
|
@api.onchange("model_id")
|
||||||
def onchange_model_id(self):
|
def onchange_model_id(self):
|
||||||
if self.model_id:
|
if self.model_id:
|
||||||
self.model = self.model_id.model
|
self.model = self.model_id.model
|
||||||
|
|
||||||
def generate_top_part(self):
|
def generate_top_part(self):
|
||||||
return """<?xml version="1.0"?>\n\t<t t-name="%s">\n\t
|
return (
|
||||||
""" % self.report_name
|
"""<?xml version="1.0"?>\n\t<t t-name="%s">\n\t
|
||||||
|
"""
|
||||||
|
% self.report_name
|
||||||
|
)
|
||||||
|
|
||||||
def generate_bottom_part(self):
|
def generate_bottom_part(self):
|
||||||
return """\n
|
return """\n
|
||||||
\t\t</t>\n\t\t"""
|
\t\t</t>\n\t\t"""
|
||||||
|
|
||||||
def generate_custom_content(self, report_name):
|
def generate_custom_content(self, report_name):
|
||||||
return """\n
|
return (
|
||||||
\t<t t-call="%s"/>""" % report_name
|
"""\n
|
||||||
|
\t<t t-call="%s"/>"""
|
||||||
|
% report_name
|
||||||
|
)
|
||||||
|
|
||||||
def _generate_batch_qweb_report(self, update_batch_qweb=False):
|
def _generate_batch_qweb_report(self, update_batch_qweb=False):
|
||||||
report_name = self.report_name
|
report_name = self.report_name
|
||||||
if '.' in report_name:
|
if "." in report_name:
|
||||||
module = self.report_name.split('.')[0]
|
module = self.report_name.split(".")[0]
|
||||||
report_name = self.report_name.split('.')[1]
|
report_name = self.report_name.split(".")[1]
|
||||||
else:
|
else:
|
||||||
# Generate random number to avoid IntegrityError
|
# Generate random number to avoid IntegrityError
|
||||||
module = random.randint(1, 1000000)
|
module = random.randint(1, 1000000)
|
||||||
self.report_name = "%s.%s" % (module, report_name)
|
self.report_name = "{}.{}".format(module, report_name)
|
||||||
if self.subreport_ids:
|
if self.subreport_ids:
|
||||||
if update_batch_qweb:
|
if update_batch_qweb:
|
||||||
report_name = self.report_name.split('.')[1]
|
report_name = self.report_name.split(".")[1]
|
||||||
# Delete old Qweb batch report
|
# Delete old Qweb batch report
|
||||||
model_data = self.env['ir.model.data'].search(
|
model_data = self.env["ir.model.data"].search(
|
||||||
[('res_id', '=', self.id)])
|
[("res_id", "=", self.id)]
|
||||||
|
)
|
||||||
model_data.unlink()
|
model_data.unlink()
|
||||||
ui_view = self.env['ir.ui.view'].search(
|
ui_view = self.env["ir.ui.view"].search([("name", "=", report_name)])
|
||||||
[('name', '=', report_name)])
|
|
||||||
ui_view.unlink()
|
ui_view.unlink()
|
||||||
template_header = self.generate_top_part()
|
template_header = self.generate_top_part()
|
||||||
template_footer = self.generate_bottom_part()
|
template_footer = self.generate_bottom_part()
|
||||||
template_content = ''
|
template_content = ""
|
||||||
|
|
||||||
for subreport in self.subreport_ids:
|
for subreport in self.subreport_ids:
|
||||||
template_content += self.generate_custom_content(
|
template_content += self.generate_custom_content(
|
||||||
subreport.subreport_id.report_name
|
subreport.subreport_id.report_name
|
||||||
)
|
)
|
||||||
data = "%s%s%s" % (
|
data = "{}{}{}".format(template_header, template_content, template_footer)
|
||||||
template_header, template_content, template_footer)
|
ui_view = self.env["ir.ui.view"].create(
|
||||||
ui_view = self.env['ir.ui.view'].create(
|
{
|
||||||
{'name': report_name,
|
"name": report_name,
|
||||||
'type': 'qweb',
|
"type": "qweb",
|
||||||
'model': self.model,
|
"model": self.model,
|
||||||
'mode': 'primary',
|
"mode": "primary",
|
||||||
'arch_base': data})
|
"arch_base": data,
|
||||||
self.env['ir.model.data'].create(
|
}
|
||||||
{'module': module,
|
)
|
||||||
'name': report_name,
|
self.env["ir.model.data"].create(
|
||||||
'res_id': ui_view.id,
|
{
|
||||||
'model': 'ir.ui.view'})
|
"module": module,
|
||||||
|
"name": report_name,
|
||||||
|
"res_id": ui_view.id,
|
||||||
|
"model": "ir.ui.view",
|
||||||
|
}
|
||||||
|
)
|
||||||
# Register batch report option
|
# Register batch report option
|
||||||
if not self.binding_model_id:
|
if not self.binding_model_id:
|
||||||
self.create_action()
|
self.create_action()
|
||||||
|
@ -97,7 +109,7 @@ class IrActionsReport(models.Model):
|
||||||
@api.multi
|
@api.multi
|
||||||
def write(self, vals):
|
def write(self, vals):
|
||||||
res = super(IrActionsReport, self).write(vals)
|
res = super(IrActionsReport, self).write(vals)
|
||||||
if 'subreport_ids' in vals or 'model' in vals:
|
if "subreport_ids" in vals or "model" in vals:
|
||||||
for report in self:
|
for report in self:
|
||||||
report._generate_batch_qweb_report(update_batch_qweb=True)
|
report._generate_batch_qweb_report(update_batch_qweb=True)
|
||||||
return res
|
return res
|
||||||
|
|
|
@ -1,39 +1,40 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
<odoo>
|
<odoo>
|
||||||
<record id="act_batch_report_xml_view" model="ir.ui.view">
|
<record id="act_batch_report_xml_view" model="ir.ui.view">
|
||||||
<field name="name">ir.actions.batch.report.form</field>
|
<field name="name">ir.actions.batch.report.form</field>
|
||||||
<field name="model">ir.actions.report</field>
|
<field name="model">ir.actions.report</field>
|
||||||
<field name="inherit_id" ref="base.act_report_xml_view"/>
|
<field name="inherit_id" ref="base.act_report_xml_view" />
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<field name="report_type" position="after">
|
<field name="report_type" position="after">
|
||||||
<field name="model_id"/>
|
<field name="model_id" />
|
||||||
</field>
|
</field>
|
||||||
<page name="advanced" position="after">
|
<page name="advanced" position="after">
|
||||||
<page name="batch_report" string="Batch Report">
|
<page name="batch_report" string="Batch Report">
|
||||||
<field name="subreport_ids">
|
<field name="subreport_ids">
|
||||||
<tree string="Batch Reports" editable="bottom">
|
<tree string="Batch Reports" editable="bottom">
|
||||||
<field name="sequence" widget="handle"/>
|
<field name="sequence" widget="handle" />
|
||||||
<field name="subreport_id" domain="[('model', '=', parent.model)]"/>
|
<field
|
||||||
|
name="subreport_id"
|
||||||
|
domain="[('model', '=', parent.model)]"
|
||||||
|
/>
|
||||||
</tree>
|
</tree>
|
||||||
</field>
|
</field>
|
||||||
</page>
|
</page>
|
||||||
</page>
|
</page>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
||||||
<record id="view_ir_actions_subreport_tree" model="ir.ui.view">
|
<record id="view_ir_actions_subreport_tree" model="ir.ui.view">
|
||||||
<field name="name">ir.actions.report.subreport.tree</field>
|
<field name="name">ir.actions.report.subreport.tree</field>
|
||||||
<field name="model">ir.actions.report.subreport</field>
|
<field name="model">ir.actions.report.subreport</field>
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<tree string="Batch Reports" editable="bottom">
|
<tree string="Batch Reports" editable="bottom">
|
||||||
<field name="sequence"/>
|
<field name="sequence" />
|
||||||
<field name="parent_report_id"/>
|
<field name="parent_report_id" />
|
||||||
<field name="model" invisible="1"/>
|
<field name="model" invisible="1" />
|
||||||
<field name="subreport_id" domain="[('model', '=', model)]"/>
|
<field name="subreport_id" domain="[('model', '=', model)]" />
|
||||||
</tree>
|
</tree>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
||||||
<record id="action_ir_actions_subreport" model="ir.actions.act_window">
|
<record id="action_ir_actions_subreport" model="ir.actions.act_window">
|
||||||
<field name="name">Batch Reports</field>
|
<field name="name">Batch Reports</field>
|
||||||
<field name="type">ir.actions.act_window</field>
|
<field name="type">ir.actions.act_window</field>
|
||||||
|
@ -41,10 +42,10 @@
|
||||||
<field name="view_type">form</field>
|
<field name="view_type">form</field>
|
||||||
<field name="help">Allows to add multi QWeb reports in a single batch</field>
|
<field name="help">Allows to add multi QWeb reports in a single batch</field>
|
||||||
</record>
|
</record>
|
||||||
|
<menuitem
|
||||||
<menuitem action="action_ir_actions_subreport"
|
action="action_ir_actions_subreport"
|
||||||
id="menu_action_ir_actions_subreport"
|
id="menu_action_ir_actions_subreport"
|
||||||
parent="base.menu_users"
|
parent="base.menu_users"
|
||||||
sequence="10"/>
|
sequence="10"
|
||||||
|
/>
|
||||||
</odoo>
|
</odoo>
|
||||||
|
|
Loading…
Reference in New Issue