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.

101 lines
3.7 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. from lxml import etree
  18. from openerp import api, fields, models
  19. class ReportAction(models.Model):
  20. _inherit = "ir.actions.report.xml"
  21. report_type = fields.Selection(selection_add=[("qweb-xml", "XML")])
  22. def _lookup_report(self, cr, name):
  23. """Enable ``qweb-xml`` report lookup."""
  24. try:
  25. super(ReportAction, self)._lookup_report(cr, name)
  26. except Exception as ex:
  27. # Somebody thought it was a good idea to use standard exceptions
  28. if "qweb-xml" not in ex.message:
  29. raise ex
  30. else:
  31. cr.execute(
  32. "SELECT * FROM ir_act_report_xml WHERE report_name=%s",
  33. (name,))
  34. return cr.dictfetchone()["report_name"]
  35. @api.model
  36. def render_report(self, res_ids, name, data):
  37. """Special handling for ``qweb-xml`` reports."""
  38. if data.get("report_type") == u"qweb-xml":
  39. new_report = self._lookup_report(name)
  40. recs = self.env[self.env.context["active_model"]].browse(res_ids)
  41. result = self.env["report"].get_html(recs, new_report, data=data)
  42. # XML with spaces before the <?xml tag will fail, and trailing ones
  43. # do nothing, so let's strip them and make everyone happier
  44. result = (result.strip(), "xml")
  45. else:
  46. result = super(ReportAction, self).render_report(
  47. res_ids, name, data)
  48. return result
  49. class ReportGenerator(models.Model):
  50. _inherit = "report"
  51. @api.model
  52. def _get_report_from_name(self, report_name):
  53. """Allow to view ``qweb-xml`` reports as web pages."""
  54. try:
  55. super(ReportGenerator, self)._get_report_from_name(report_name)
  56. except IndexError:
  57. return self.env["ir.actions.report.xml"].search(
  58. [("report_type", "=", "qweb-xml"),
  59. ("report_name", "=", report_name)])[0]
  60. class XSDCheckedReport(models.AbstractModel):
  61. """Check XML report against a XSD schema before downloading it.
  62. This is an Abstract Model to be inherited by the real report models, which
  63. must implement :meth:`.xsd` and have a ``_name`` in the form
  64. ``report.<module>.<report_name>``.
  65. """
  66. _name = "report_xml.xsd_checked_report"
  67. _description = "Base model for reports that need XSD checking"
  68. @api.multi
  69. def xsd(self):
  70. """Return the XSD schema contents."""
  71. raise NotImplementedError
  72. @api.multi
  73. def render_html(self, data=None):
  74. """Return the XML report after checking it against an XSD."""
  75. docargs = {"docs": (self.env[self.env.context["active_model"]]
  76. .browse(self.env.context["active_ids"]))}
  77. xsd = etree.XMLSchema(etree.XML(self.xsd()))
  78. parser = etree.XMLParser(schema=xsd)
  79. result = (self.env["report"]
  80. .render(self._name[len("report."):], docargs)
  81. .strip())
  82. etree.fromstring(result, parser)
  83. return result