OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.4 KiB

  1. # -*- encoding: utf-8 -*-
  2. # Odoo, Open Source Management Solution
  3. # Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import logging
  18. from lxml import etree
  19. from openerp import api, fields, models
  20. _logger = logging.getLogger(__name__)
  21. class ReportAction(models.Model):
  22. _inherit = "ir.actions.report.xml"
  23. report_type = fields.Selection(selection_add=[("qweb-xml", "XML")])
  24. def _lookup_report(self, cr, name):
  25. """Enable ``qweb-xml`` report lookup."""
  26. try:
  27. super(ReportAction, self)._lookup_report(cr, name)
  28. except Exception as ex:
  29. # Somebody thought it was a good idea to use standard exceptions
  30. if "qweb-xml" not in ex.message:
  31. raise ex
  32. else:
  33. cr.execute(
  34. "SELECT * FROM ir_act_report_xml WHERE report_name=%s",
  35. (name,))
  36. return cr.dictfetchone()["report_name"]
  37. @api.model
  38. def render_report(self, res_ids, name, data):
  39. """Special handling for ``qweb-xml`` reports."""
  40. if data.get("report_type") == u"qweb-xml":
  41. new_report = self._lookup_report(name)
  42. recs = self.env[self.env.context["active_model"]].browse(res_ids)
  43. result = self.env["report"].get_html(recs, new_report, data=data)
  44. # XML with spaces before the <?xml tag will fail, and trailing ones
  45. # do nothing, so let's strip them and make everyone happier
  46. result = (result.strip(), "xml")
  47. else:
  48. result = super(ReportAction, self).render_report(
  49. res_ids, name, data)
  50. return result
  51. class ReportGenerator(models.Model):
  52. _inherit = "report"
  53. @api.model
  54. def _get_report_from_name(self, report_name):
  55. """Allow to view ``qweb-xml`` reports as web pages."""
  56. try:
  57. super(ReportGenerator, self)._get_report_from_name(report_name)
  58. except IndexError:
  59. return self.env["ir.actions.report.xml"].search(
  60. [("report_type", "=", "qweb-xml"),
  61. ("report_name", "=", report_name)])[0]
  62. class XSDCheckedReport(models.AbstractModel):
  63. """Check XML report against a XSD schema before downloading it.
  64. This is an Abstract Model to be inherited by the real report models, which
  65. must implement :meth:`.xsd` and have a ``_name`` in the form
  66. ``report.<module>.<report_name>``.
  67. """
  68. _name = "report_xml.xsd_checked_report"
  69. _description = "Base model for reports that need XSD checking"
  70. @api.multi
  71. def xsd(self):
  72. """Return the XSD schema contents."""
  73. raise NotImplementedError
  74. @api.multi
  75. def render_html(self, data=None):
  76. """Return the XML report after checking it against an XSD.
  77. If ``context`` contains a dict called ``docargs``, it will be used as
  78. the Qweb context. The special key ``docs`` will be added to ``docargs``
  79. automatically if missing.
  80. """
  81. # Qweb context
  82. docargs = self.env.context.get("docargs", dict())
  83. if "docs" not in docargs:
  84. docargs["docs"] = (self.env[self.env.context["active_model"]]
  85. .browse(self.env.context["active_ids"]))
  86. # Load XSD
  87. xsd = etree.XML(self.xsd())
  88. _logger.debug("XSD schema contents: %s", etree.tostring(xsd))
  89. xsd = etree.XMLSchema(xsd)
  90. parser = etree.XMLParser(schema=xsd)
  91. # Generate XML report
  92. result = (self.env["report"]
  93. .render(self._name[len("report."):], docargs)
  94. .strip())
  95. # Validate XML with XSD
  96. try:
  97. etree.fromstring(result, parser)
  98. except Exception as error:
  99. _logger.error(result)
  100. raise error
  101. return result