Browse Source

Merge pull request #21 from grupoesoc/report-xml

Report xml
pull/31/head
Alexandre Fayolle 9 years ago
parent
commit
cd26894d06
  1. 101
      report_xml/README.rst
  2. 4
      report_xml/__init__.py
  3. 20
      report_xml/__openerp__.py
  4. 25
      report_xml/controllers.py
  5. 111
      report_xml/models.py
  6. BIN
      report_xml/static/description/icon.png
  7. 3121
      report_xml/static/description/icon.svg
  8. 11
      report_xml/views/report_xml_templates.xml
  9. 69
      report_xml_sample/README.rst
  10. 4
      report_xml_sample/__init__.py
  11. 21
      report_xml_sample/__openerp__.py
  12. 24
      report_xml_sample/models.py
  13. BIN
      report_xml_sample/static/description/icon.png
  14. 3131
      report_xml_sample/static/description/icon.svg
  15. 43
      report_xml_sample/views/res_partner_templates.xml
  16. 22
      report_xml_sample/xsd/sample_report.xsd

101
report_xml/README.rst

@ -0,0 +1,101 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:alt: License: AGPL-3
Qweb XML Reports
================
This module was written to extend the functionality of the reporting engine to
support XML reports and allow modules to generate them by code or by QWeb
templates.
Installation
============
To install this module, you need to:
* Install lxml_ in Odoo's ``$PYTHONPATH``.
* Install the repository `reporting-engine`_.
But this module does nothing for the end user by itself, so if you have it
installed it's probably because there is another module that depends on it.
Configuration
=============
No manual configuration is needed.
Usage
=====
If you are a user
-----------------
You will be able to download XML reports from the *Print* menu found on form
and list views.
If you are a developer
----------------------
To learn from an example, just check the `sample module`_.
To develop with this module, you need to:
* Create a module.
* Make it depend on this one.
* Follow `instructions to create reports`_ having in mind that the
``report_type`` field in your ``ir.actions.report.xml`` record must be
``qweb-xml``.
In case you want to create a `custom report`_, the instructions remain the same
as for HTML reports, and the method that you must override is also called
``render_html``, even when this time you are creating a XML report.
You can make your custom report inherit ``report_xml.xsd_checked_report``, name
it like your XML ``<template>`` id prepended by ``report.``, add a ``xsd()``
method that returns a XSD in a string, and have XSD automatic checking for
free.
You can visit ``http://<server-address>/report/xml/<module.report_name>/<ids>``
to see your XML report online as a web page.
For further information, please visit:
* https://www.odoo.com/forum/help-1
* https://github.com/OCA/reporting-engine
Known issues / Roadmap
======================
None
Credits
=======
* Icon taken from http://commons.wikimedia.org/wiki/File:Text-xml.svg.
Contributors
------------
* Jairo Llopis <j.llopis@grupoesoc.es>
Maintainer
----------
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org
This module is maintained by the OCA.
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.
To contribute to this module, please visit http://odoo-community.org.
.. _custom report: https://www.odoo.com/documentation/8.0/reference/reports.html#custom-reports
.. _instructions to create reports: https://www.odoo.com/documentation/8.0/reference/reports.html
.. _reporting-engine: https://github.com/OCA/reporting-engine
.. _sample module: https://github.com/OCA/reporting-engine/tree/8.0/report_xml_sample
.. _lxml: http://lxml.de/

4
report_xml/__init__.py

@ -0,0 +1,4 @@
# -*- encoding: utf-8 -*-
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
from . import controllers, models

20
report_xml/__openerp__.py

@ -0,0 +1,20 @@
# -*- encoding: utf-8 -*-
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
{
"name": "Qweb XML Reports",
"version": "1.0",
"category": "Reporting",
"author": "Odoo Community Association (OCA), Grupo ESOC",
"license": "AGPL-3",
"website": "https://odoo-community.org/",
"installable": True,
"application": False,
"summary": "Allow to generate XML reports",
"depends": [
"report",
],
"data": [
"views/report_xml_templates.xml",
]
}

25
report_xml/controllers.py

@ -0,0 +1,25 @@
# -*- encoding: utf-8 -*-
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
from openerp.http import route
from openerp.addons.report.controllers import main as report
class ReportController(report.ReportController):
@route()
def report_routes(self, reportname, docids=None, converter=None, **data):
# Trick the main reporter to think we want an HTML report
new_converter = converter if converter != "xml" else "html"
response = super(ReportController, self).report_routes(
reportname, docids, new_converter, **data)
# If it was an XML report, just download the generated response
if converter == "xml":
# XML header must be before any spaces, and it is a common error,
# so let's fix that here and make developers happier
response.data = response.data.strip()
# XML files should be downloaded
response.headers.set("Content-Type", "text/xml")
return response

111
report_xml/models.py

@ -0,0 +1,111 @@
# -*- encoding: utf-8 -*-
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
import logging
from lxml import etree
from openerp import api, fields, models
_logger = logging.getLogger(__name__)
class ReportAction(models.Model):
_inherit = "ir.actions.report.xml"
report_type = fields.Selection(selection_add=[("qweb-xml", "XML")])
def _lookup_report(self, cr, name):
"""Enable ``qweb-xml`` report lookup."""
try:
return super(ReportAction, self)._lookup_report(cr, name)
except Exception as ex:
# Somebody thought it was a good idea to use standard exceptions
if "qweb-xml" not in ex.message:
raise ex
else:
cr.execute(
"SELECT * FROM ir_act_report_xml WHERE report_name=%s",
(name,))
return cr.dictfetchone()["report_name"]
@api.model
def render_report(self, res_ids, name, data):
"""Special handling for ``qweb-xml`` reports."""
if data.get("report_type") == u"qweb-xml":
new_report = self._lookup_report(name)
recs = self.env[self.env.context["active_model"]].browse(res_ids)
result = self.env["report"].get_html(recs, new_report, data=data)
# XML with spaces before the <?xml tag will fail, and trailing ones
# do nothing, so let's strip them and make everyone happier
result = (result.strip(), "xml")
else:
result = super(ReportAction, self).render_report(
res_ids, name, data)
return result
class ReportGenerator(models.Model):
_inherit = "report"
@api.model
def _get_report_from_name(self, report_name):
"""Allow to view ``qweb-xml`` reports as web pages."""
try:
return (super(ReportGenerator, self)
._get_report_from_name(report_name))
except IndexError:
return self.env["ir.actions.report.xml"].search(
[("report_type", "=", "qweb-xml"),
("report_name", "=", report_name)])[0]
class XSDCheckedReport(models.AbstractModel):
"""Check XML report against a XSD schema before downloading it.
This is an Abstract Model to be inherited by the real report models, which
must implement :meth:`.xsd` and have a ``_name`` in the form
``report.<module>.<report_name>``.
"""
_name = "report_xml.xsd_checked_report"
_description = "Base model for reports that need XSD checking"
@api.multi
def xsd(self):
"""Return the XSD schema contents."""
raise NotImplementedError
@api.multi
def render_html(self, data=None):
"""Return the XML report after checking it against an XSD.
If ``context`` contains a dict called ``docargs``, it will be used as
the Qweb context. The special key ``docs`` will be added to ``docargs``
automatically if missing.
"""
# Qweb context
docargs = self.env.context.get("docargs", dict())
if "docs" not in docargs:
docargs["docs"] = (self.env[self.env.context["active_model"]]
.browse(self.env.context["active_ids"]))
# Load XSD
xsd = etree.XML(self.xsd())
_logger.debug("XSD schema contents: %s", etree.tostring(xsd))
xsd = etree.XMLSchema(xsd)
parser = etree.XMLParser(schema=xsd)
# Generate XML report
result = (self.env["report"]
.render(self._name[len("report."):], docargs)
.strip())
# Validate XML with XSD
try:
etree.fromstring(result, parser)
except Exception as error:
_logger.error(result)
raise error
return result

BIN
report_xml/static/description/icon.png

After

Width: 100  |  Height: 100  |  Size: 4.2 KiB

3121
report_xml/static/description/icon.svg
File diff suppressed because it is too large
View File

11
report_xml/views/report_xml_templates.xml

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<template id="utf8_header">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
<t t-raw="0"/>
</template>
</data>
</openerp>

69
report_xml_sample/README.rst

@ -0,0 +1,69 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:alt: License: AGPL-3
Qweb XML Reports Sample Module
==============================
This module was written to serve as a sample for developers that want to
generate Qweb XML reports in their modules. It is useless for the final user.
Installation
============
To install this module, you need to:
* Install lxml_ in Odoo's ``$PYTHONPATH``.
* Install the repository `reporting-engine`_.
Configuration
=============
No manual configuration is needed.
Usage
=====
Go to *Messaging > Organizer > Contacts*, enter any partner (or switch to *list
view* and select one or more of them), and under the *Print* menu download any
of the XML sample reports.
For further information, please visit:
* https://www.odoo.com/documentation/8.0/reference/qweb.html
* https://www.odoo.com/documentation/8.0/reference/reports.html
* https://www.odoo.com/forum/help-1
* https://github.com/OCA/reporting-engine
Known issues / Roadmap
======================
None
Credits
=======
* Icon taken from http://commons.wikimedia.org/wiki/File:Text-xml.svg.
Contributors
------------
* Jairo Llopis <j.llopis@grupoesoc.es>
Maintainer
----------
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org
This module is maintained by the OCA.
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.
To contribute to this module, please visit http://odoo-community.org.
.. _reporting-engine: https://github.com/OCA/reporting-engine
.. _lxml: http://lxml.de/

4
report_xml_sample/__init__.py

@ -0,0 +1,4 @@
# -*- encoding: utf-8 -*-
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
from . import models

21
report_xml_sample/__openerp__.py

@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
{
"name": "Qweb XML Sample Report",
"version": "1.0",
"category": "Reporting",
"author": "Odoo Community Association (OCA), Grupo ESOC",
"license": "AGPL-3",
"website": "https://odoo-community.org/",
"installable": True,
"application": False,
"summary": "For developers who want an example",
"depends": [
"contacts",
"report",
],
"data": [
"views/res_partner_templates.xml",
]
}

24
report_xml_sample/models.py

@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
from os import path
from openerp import api, models
class XSDSampleReport(models.AbstractModel):
"""This report is checked against an XSD before downloading.
The XML itself is declared in ``./views/res_partner_templates.xml``, and
the XSD can be found in ``./xsd/sample_report.xsd``.
This model's :attr:`._name` must be ``report.<XML ID of template>``.
"""
_name = "report.report_xml_sample.xsd_sample_report"
_inherit = "report_xml.xsd_checked_report"
@api.multi
def xsd(self):
"""Return the XSD schema contents."""
file = path.join(path.dirname(__file__), "xsd", "sample_report.xsd")
with open(file) as xsd:
return xsd.read()

BIN
report_xml_sample/static/description/icon.png

After

Width: 100  |  Height: 100  |  Size: 6.7 KiB

3131
report_xml_sample/static/description/icon.svg
File diff suppressed because it is too large
View File

43
report_xml_sample/views/res_partner_templates.xml

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Basic XML report -->
<report
id="sample_report_action"
name="report_xml_sample.sample_report"
string="XML Report Sample"
report_type="qweb-xml"
model="res.partner"/>
<template id="sample_report">
<t t-call="report_xml.utf8_header">
<partners>
<partner t-foreach="docs" t-as="p">
<id t-esc="p.id"/>
<name t-esc="p.name"/>
<children t-if="p.child_ids">
<partner t-foreach="p.child_ids" t-as="c">
<id t-esc="c.id"/>
<name t-esc="c.name"/>
</partner>
</children>
</partner>
</partners>
</t>
</template>
<!-- Report checked against an XSD (see also ``../models.py``) -->
<report
id="xsd_sample_report_action"
name="report_xml_sample.xsd_sample_report"
string="XSD-Checked XML Report Sample"
report_type="qweb-xml"
model="res.partner"/>
<template id="xsd_sample_report">
<t t-call="report_xml_sample.sample_report"/>
</template>
</data>
</openerp>

22
report_xml_sample/xsd/sample_report.xsd

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Complex types -->
<xs:complexType name="t_partners">
<xs:sequence>
<xs:element name="partner"
type="t_partner"
minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="t_partner">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="children" type="t_partners" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<!-- Schema -->
<xs:element name="partners" type="t_partners"/>
</xs:schema>
Loading…
Cancel
Save