Skip to content

Commit

Permalink
Merge 0dd8d9b into cfb54b9
Browse files Browse the repository at this point in the history
  • Loading branch information
yajo committed Jul 22, 2015
2 parents cfb54b9 + 0dd8d9b commit 084db5e
Show file tree
Hide file tree
Showing 16 changed files with 6,706 additions and 0 deletions.
101 changes: 101 additions & 0 deletions report_xml/README.rst
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions report_xml/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- encoding: utf-8 -*-
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>

from . import controllers, models
20 changes: 20 additions & 0 deletions report_xml/__openerp__.py
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions report_xml/controllers.py
Original file line number Diff line number Diff line change
@@ -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
110 changes: 110 additions & 0 deletions report_xml/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# -*- 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:
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:
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
Binary file added report_xml/static/description/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 084db5e

Please sign in to comment.