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.

70 lines
2.6 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 openerp import api, fields, models
  18. class ReportAction(models.Model):
  19. _inherit = "ir.actions.report.xml"
  20. report_type = fields.Selection(selection_add=[("qweb-xml", "XML")])
  21. def _lookup_report(self, cr, name):
  22. """Enable ``qweb-xml`` report lookup."""
  23. try:
  24. super(ReportAction, self)._lookup_report(cr, name)
  25. except Exception as ex:
  26. # Somebody thought it was a good idea to use standard exceptions
  27. if "qweb-xml" not in ex.message:
  28. raise ex
  29. else:
  30. cr.execute(
  31. "SELECT * FROM ir_act_report_xml WHERE report_name=%s",
  32. (name,))
  33. return cr.dictfetchone()["report_name"]
  34. @api.model
  35. def render_report(self, res_ids, name, data):
  36. """Special handling for ``qweb-xml`` reports."""
  37. if data.get("report_type") == u"qweb-xml":
  38. new_report = self._lookup_report(name)
  39. recs = self.env[self.env.context["active_model"]].browse(res_ids)
  40. result = self.env["report"].get_html(recs, new_report, data=data)
  41. # XML with spaces before the <?xml tag will fail, and trailing ones
  42. # do nothing, so let's strip them and make everyone happier
  43. result = (result.strip(), "xml")
  44. else:
  45. result = super(ReportAction, self).render_report(
  46. res_ids, name, data)
  47. return result
  48. class ReportGenerator(models.Model):
  49. _inherit = "report"
  50. @api.model
  51. def _get_report_from_name(self, report_name):
  52. """Allow to view ``qweb-xml`` reports as web pages."""
  53. try:
  54. super(ReportGenerator, self)._get_report_from_name(report_name)
  55. except IndexError:
  56. return self.env["ir.actions.report.xml"].search(
  57. [("report_type", "=", "qweb-xml"),
  58. ("report_name", "=", report_name)])[0]