Skip to content

Commit

Permalink
Merge PR #695 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Aug 23, 2023
2 parents 646cb05 + 651813a commit 6d888fe
Show file tree
Hide file tree
Showing 99 changed files with 22,169 additions and 0 deletions.
661 changes: 661 additions & 0 deletions report_py3o/LICENSE

Large diffs are not rendered by default.

245 changes: 245 additions & 0 deletions report_py3o/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
==================
Py3o Report Engine
==================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freporting--engine-lightgray.png?logo=github
:target: https://github.com/OCA/reporting-engine/tree/15.0/report_py3o
:alt: OCA/reporting-engine
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/reporting-engine-15-0/reporting-engine-15-0-report_py3o
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/143/15.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

The py3o reporting engine is a reporting engine for Odoo based on `Libreoffice <http://www.libreoffice.org/>`_:

* the report is created with Libreoffice (ODT or ODS),
* the report is stored on the server in OpenDocument format (.odt or .ods file)
* the report is sent to the user in OpenDocument format or in any output format supported by Libreoffice (PDF, HTML, DOC, DOCX, Docbook, XLS, etc.)

The key advantages of a Libreoffice based reporting engine are:

* no need to be a developer to create or modify a report: the report is created and modified with Libreoffice. So this reporting engine has a full WYSIWYG report development tool!
* For a PDF report in A4/Letter format, it's easier to develop it with a tool such as Libreoffice that is designed to create A4/Letter documents than to develop it in HTML/CSS, also some print peculiarities (backgrounds, margin boxes) are not very well supported by the HTML/CSS based solutions.
* If you want your users to be able to modify the document after its generation by Odoo, just configure the document with ODT output (or DOC or DOCX) and the user will be able to modify the document with Libreoffice (or Word) after its generation by Odoo.
* Easy development of spreadsheet reports in ODS format (XLS output possible).

This module *report_py3o* is the base module for the Py3o reporting engine. If used alone, it will spawn a libreoffice process for each ODT to PDF (or ODT to DOCX, ..) document conversion. This is slow and can become a problem if you have a lot of reports to convert from ODT to another format. In this case, you should consider the additionnal module *report_py3o_fusion_server* which is designed to work with a libreoffice daemon. With *report_py3o_fusion_server*, the technical environnement is more complex to setup because you have to install additionnal software components and run 2 daemons, but you have much better performances and you can configure the libreoffice PDF export options in Odoo (allows to generate PDF forms, PDF/A documents, password-protected PDFs, watermarked PDFs, etc.).

This reporting engine is an alternative to `Aeroo <https://github.com/aeroo-community/aeroo_reports>`_: these two reporting engines have similar features but their implementation is entirely different. You cannot use aeroo templates as drop in replacement though, you'll have to change a few details.

**Table of contents**

.. contents::
:local:

Installation
============

Install the required python libs:

.. code::
pip install py3o.template
pip install py3o.formats
To allow the conversion of ODT or ODS reports to other formats (PDF, DOC, DOCX, etc.), install libreoffice:

.. code::
apt-get --no-install-recommends install libreoffice
Configuration
=============

For example, to replace the native invoice report by a custom py3o report, add the following XML file in your custom module:

.. code::
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="account.account_invoices" model="ir.actions.report">
<field name="report_type">py3o</field>
<field name="py3o_filetype">odt</field>
<field name="module">my_custom_module_base</field>
<field name="py3o_template_fallback">report/account_invoice.odt</field>
</record>
</odoo>
where *my_custom_module_base* is the name of the custom Odoo module. In this example, the invoice ODT file is located in *my_custom_module_base/report/account_invoice.odt*.

It's also possible to reference a template located in a trusted path of your
Odoo server. In this case you must let the *module* entry empty and specify
the path to the template as *py3o_template_fallback*.

.. code::
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="account.account_invoices" model="ir.actions.report">
<field name="report_type">py3o</field>
<field name="py3o_filetype">odt</field>
<field name="py3o_template_fallback">/odoo/templates/py3o/report/account_invoice.odt</field>
</record>
</odoo>
Moreover, you must also modify the Odoo server configuration file to declare
the allowed root directory for your py3o templates. Only templates located
into this directory can be loaded by py3o report.

.. code::
[options]
...
[report_py3o]
root_tmpl_path=/odoo/templates/py3o
If you want an invoice in PDF format instead of ODT format, the XML file should look like:

.. code::
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="account.account_invoices" model="ir.actions.report">
<field name="report_type">py3o</field>
<field name="py3o_filetype">pdf</field>
<field name="module">my_custom_module_base</field>
<field name="py3o_template_fallback">report/account_invoice.odt</field>
</record>
</odoo>
If you want to add a new py3o PDF report (and not replace a native report), the XML file should look like this:

.. code::
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="partner_summary_report" model="ir.actions.report">
<field name="name">Partner Summary</field>
<field name="model">res.partner</field>
<field name="report_name">res.partner.summary</field>
<field name="report_type">py3o</field>
<field name="py3o_filetype">pdf</field>
<field name="module">my_custom_module_base</field>
<field name="py3o_template_fallback">report/partner_summary.odt</field>
<!-- Add entry in "Print" drop-down list -->
<field name="binding_type">report</field>
<field name="binding_model_id" ref="base.model_res_partner"/>
</record>
</odoo>
Configuration parameters
~~~~~~~~~~~~~~~~~~~~~~~~

py3o.conversion_command
The command to be used to run the conversion, ``libreoffice`` by default. If you change this, whatever you set here must accept the parameters ``--headless --convert-to $ext $file`` and put the resulting file into ``$file``'s directory with extension ``$ext``. The command will be started in ``$file``'s directory.

Usage
=====

The templating language is `extensively documented <http://py3otemplate.readthedocs.io/en/latest/templating.html>`_, the records are exposed in libreoffice as ``objects``, on which you can also call functions.

Available functions and objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

user
Browse record of current user
lang
The user's company's language as string (ISO code)
b64decode
``base64.b64decode``
format_multiline_value(string)
Generate the ODF equivalent of ``<br/>`` and ``&nbsp;`` for multiline fields (ODF is XML internally, so those would be skipped otherwise)
html_sanitize(string)
Sanitize HTML string
time
Python's ``time`` module
display_address(partner)
Return a formatted string of the partner's address
o_format_lang(value, lang_code=False, digits=None, grouping=True, monetary=False, dp=False, currency_obj=False, no_break_space=True)
Return a formatted numeric or monetary value according to the context language and timezone
o_format_date(value, lang_code=False, date_format=False)
Return a formatted date or time value according to the context language and timezone


Sample report templates
~~~~~~~~~~~~~~~~~~~~~~~

Sample py3o report templates for the main Odoo native reports (invoice, sale order, purchase order, picking, etc.) are available on the Github project `odoo-py3o-report-templates <https://github.com/akretion/odoo-py3o-report-templates>`_.

Known issues / Roadmap
======================

* generate barcode ?
* add more detailed example in demo file to showcase features
* add migration guide aeroo -> py3o

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/reporting-engine/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/reporting-engine/issues/new?body=module:%20report_py3o%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* XCG Consulting
* ACSONE SA/NV

Contributors
~~~~~~~~~~~~

* Florent Aide (`XCG Consulting <http://odoo.consulting/>`_)
* Laurent Mignon <[email protected]>,
* Alexis de Lattre <[email protected]>,
* Guewen Baconnier <[email protected]>
* Omar Castiñeira <[email protected]>
* Holger Brunn <[email protected]>
* Phuc Tran Thanh <[email protected]>

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/reporting-engine <https://github.com/OCA/reporting-engine/tree/15.0/report_py3o>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions report_py3o/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import controllers
29 changes: 29 additions & 0 deletions report_py3o/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2013 XCG Consulting (http://odoo.consulting)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Py3o Report Engine",
"summary": "Reporting engine based on Libreoffice (ODT -> ODT, "
"ODT -> PDF, ODT -> DOC, ODT -> DOCX, ODS -> ODS, etc.)",
"version": "16.0.1.0.0",
"category": "Reporting",
"license": "AGPL-3",
"author": "XCG Consulting, ACSONE SA/NV, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/reporting-engine",
"depends": ["web"],
"external_dependencies": {
"python": ["py3o.template", "py3o.formats", "PyPDF2"],
"deb": ["libreoffice"],
},
"assets": {
"web.assets_backend": [
"report_py3o/static/src/js/py3oactionservice.esm.js",
],
},
"data": [
"security/ir.model.access.csv",
"views/py3o_template.xml",
"views/ir_actions_report.xml",
"demo/report_py3o.xml",
],
"installable": True,
}
1 change: 1 addition & 0 deletions report_py3o/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
94 changes: 94 additions & 0 deletions report_py3o/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Copyright 2017 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
import json
import mimetypes

from werkzeug import exceptions
from werkzeug.urls import url_decode

from odoo.http import content_disposition, request, route, serialize_exception
from odoo.tools import html_escape

from odoo.addons.web.controllers.report import ReportController


class ReportController(ReportController):
@route()
def report_routes(self, reportname, docids=None, converter=None, **data):
if converter != "py3o":
return super().report_routes(
reportname=reportname, docids=docids, converter=converter, **data
)
context = dict(request.env.context)

if docids:
docids = [int(i) for i in docids.split(",")]
if data.get("options"):
data.update(json.loads(data.pop("options")))
if data.get("context"):
# Ignore 'lang' here, because the context in data is the
# one from the webclient *but* if the user explicitely wants to
# change the lang, this mechanism overwrites it.
data["context"] = json.loads(data["context"])
if data["context"].get("lang"):
del data["context"]["lang"]
context.update(data["context"])

ir_action = request.env["ir.actions.report"]
action_py3o_report = ir_action.get_from_report_name(
reportname, "py3o"
).with_context(**context)
if not action_py3o_report:
raise exceptions.HTTPException(
description="Py3o action report not found for report_name "
"%s" % reportname
)
res, filetype = ir_action._render(reportname, docids, data)
filename = action_py3o_report.gen_report_download_filename(docids, data)
if not filename.endswith(filetype):
filename = "{}.{}".format(filename, filetype)
content_type = mimetypes.guess_type("x." + filetype)[0]
http_headers = [
("Content-Type", content_type),
("Content-Length", len(res)),
("Content-Disposition", content_disposition(filename)),
]
return request.make_response(res, headers=http_headers)

@route()
def report_download(self, data, context=None, token=None):
"""This function is used by 'qwebactionmanager.js' in order to trigger
the download of a py3o/controller report.
:param data: a javascript array JSON.stringified containg report
internal url ([0]) and type [1]
:returns: Response with a filetoken cookie and an attachment header
"""
requestcontent = json.loads(data)
url, report_type = requestcontent[0], requestcontent[1]
if "py3o" not in report_type:
return super().report_download(data, context=context, token=token)
try:
reportname = url.split("/report/py3o/")[1].split("?")[0]
docids = None
if "/" in reportname:
reportname, docids = reportname.split("/")

if docids:
# Generic report:
response = self.report_routes(
reportname, docids=docids, converter="py3o"
)
else:
# Particular report:
# decoding the args represented in JSON
data = list(url_decode(url.split("?")[1]).items())
response = self.report_routes(
reportname, converter="py3o", **dict(data)
)
response.set_cookie("fileToken", context)
return response
except Exception as e:
se = serialize_exception(e)
error = {"code": 200, "message": "Odoo Server Error", "data": se}
return request.make_response(html_escape(json.dumps(error)))
20 changes: 20 additions & 0 deletions report_py3o/demo/report_py3o.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2016 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="res_users_report_py3o" model="ir.actions.report">
<field name="name">Py3o Demo Report</field>
<field name="type">ir.actions.report</field>
<field name="model">res.users</field>
<field name="report_name">py3o_user_info</field>
<field name="report_type">py3o</field>
<field name="py3o_filetype">odt</field>
<field name="module">report_py3o</field>
<field name="py3o_template_fallback">demo/res_user.odt</field>
<field
name="print_report_name"
>object.name.replace(' ', '_') + '-demo.odt'</field>
<field name="binding_model_id" ref="base.model_res_users" />
<field name="binding_type">report</field>
</record>
</odoo>
Binary file added report_py3o/demo/res_user.odt
Binary file not shown.
Loading

0 comments on commit 6d888fe

Please sign in to comment.