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.

59 lines
2.0 KiB

  1. # Copyright 2015 ACSONE SA/NV (<http://acsone.eu>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from io import BytesIO
  4. from odoo import models
  5. import logging
  6. _logger = logging.getLogger(__name__)
  7. try:
  8. import xlsxwriter
  9. except ImportError:
  10. _logger.debug('Can not import xlsxwriter`.')
  11. class ReportXlsxAbstract(models.AbstractModel):
  12. _name = 'report.report_xlsx.abstract'
  13. def _get_objs_for_report(self, docids, data):
  14. """
  15. Returns objects for xlx report. From WebUI these
  16. are either as docids taken from context.active_ids or
  17. in the case of wizard are in data. Manual calls may rely
  18. on regular context, setting docids, or setting data.
  19. :param docids: list of integers, typically provided by
  20. qwebactionmanager for regular Models.
  21. :param data: dictionary of data, if present typically provided
  22. by qwebactionmanager for TransientModels.
  23. :param ids: list of integers, provided by overrides.
  24. :return: recordset of active model for ids.
  25. """
  26. if docids:
  27. ids = docids
  28. elif data and 'context' in data:
  29. ids = data["context"].get('active_ids', [])
  30. else:
  31. ids = self.env.context.get('active_ids', [])
  32. return self.env[self.env.context.get('active_model')].browse(ids)
  33. def create_xlsx_report(self, docids, data):
  34. objs = self._get_objs_for_report(docids, data)
  35. file_data = BytesIO()
  36. workbook = xlsxwriter.Workbook(file_data, self.get_workbook_options())
  37. self.generate_xlsx_report(workbook, data, objs)
  38. workbook.close()
  39. file_data.seek(0)
  40. return file_data.read(), 'xlsx'
  41. def get_workbook_options(self):
  42. """
  43. See https://xlsxwriter.readthedocs.io/workbook.html constructor options
  44. :return: A dictionary of options
  45. """
  46. return {}
  47. def generate_xlsx_report(self, workbook, data, objs):
  48. raise NotImplementedError()