[MIG] Migrate report_xlsx to 16.0

pull/690/head
Rodrigo 2022-11-30 12:49:15 +01:00
parent 8c53b4d411
commit ca1d627614
7 changed files with 27 additions and 20 deletions

View File

@ -6,7 +6,7 @@
"author": "ACSONE SA/NV," "Creu Blanca," "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/reporting-engine",
"category": "Reporting",
"version": "15.0.1.1.1",
"version": "16.0.1.0.0",
"development_status": "Mature",
"license": "AGPL-3",
"external_dependencies": {"python": ["xlsxwriter", "xlrd"]},

View File

@ -16,12 +16,12 @@ from odoo.http import (
from odoo.tools import html_escape
from odoo.tools.safe_eval import safe_eval, time
from odoo.addons.web.controllers import main as report
from odoo.addons.web.controllers.report import ReportController
_logger = logging.getLogger(__name__)
class ReportController(report.ReportController):
class ReportController(ReportController):
@route()
def report_routes(self, reportname, docids=None, converter=None, **data):
if converter == "xlsx":
@ -34,7 +34,9 @@ class ReportController(report.ReportController):
if data.get("context"):
data["context"] = json.loads(data["context"])
context.update(data["context"])
xlsx = report.with_context(**context)._render_xlsx(docids, data=data)[0]
xlsx = report.with_context(**context)._render_xlsx(
reportname, docids, data=data
)[0]
xlsxhttpheaders = [
(
"Content-Type",
@ -44,9 +46,7 @@ class ReportController(report.ReportController):
("Content-Length", len(xlsx)),
]
return request.make_response(xlsx, headers=xlsxhttpheaders)
return super(ReportController, self).report_routes(
reportname, docids, converter, **data
)
return super().report_routes(reportname, docids, converter, **data)
@route()
def report_download(self, data, context=None):
@ -96,7 +96,7 @@ class ReportController(report.ReportController):
)
return response
else:
return super(ReportController, self).report_download(data, context)
return super().report_download(data, context)
except Exception as e:
_logger.exception("Error while generating report %s", reportname)
se = _serialize_exception(e)

View File

@ -1,8 +1,7 @@
# Copyright 2015 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo import api, fields, models
class ReportAction(models.Model):
@ -13,20 +12,19 @@ class ReportAction(models.Model):
)
@api.model
def _render_xlsx(self, docids, data):
report_model_name = "report.%s" % self.report_name
report_model = self.env.get(report_model_name)
if report_model is None:
raise UserError(_("%s model was not found") % report_model_name)
def _render_xlsx(self, report_ref, docids, data):
report_sudo = self._get_report(report_ref)
report_model_name = "report.%s" % report_sudo.report_name
report_model = self.env[report_model_name]
return (
report_model.with_context(active_model=self.model)
report_model.with_context(active_model=report_sudo.model)
.sudo(False)
.create_xlsx_report(docids, data) # noqa
)
@api.model
def _get_report_from_name(self, report_name):
res = super(ReportAction, self)._get_report_from_name(report_name)
res = super()._get_report_from_name(report_name)
if res:
return res
report_obj = self.env["ir.actions.report"]

View File

@ -16,18 +16,18 @@ except ImportError:
class TestReport(common.TransactionCase):
def setUp(self):
super().setUp()
report_object = self.env["ir.actions.report"]
self.report_object = self.env["ir.actions.report"]
self.xlsx_report = self.env["report.report_xlsx.abstract"].with_context(
active_model="res.partner"
)
self.report_name = "report_xlsx.partner_xlsx"
self.report = report_object._get_report_from_name(self.report_name)
self.report = self.report_object._get_report_from_name(self.report_name)
self.docs = self.env["res.company"].search([], limit=1).partner_id
def test_report(self):
report = self.report
self.assertEqual(report.report_type, "xlsx")
rep = report._render(self.docs.ids, {})
rep = self.report_object._render(self.report_name, self.docs.ids, {})
wb = open_workbook(file_contents=rep[0])
sheet = wb.sheet_by_index(0)
self.assertEqual(sheet.cell(0, 0).value, self.docs.name)

View File

@ -1,2 +1,4 @@
# generated from manifests external_dependencies
lxml
xlrd
xlsxwriter

View File

@ -0,0 +1 @@
../../../../report_xlsx

View File

@ -0,0 +1,6 @@
import setuptools
setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)