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.

112 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Yannick Vaucher
  5. # Copyright 2013 Camptocamp SA
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import orm
  22. from openerp import netsvc
  23. from openerp.report.report_sxw import rml_parse
  24. from report_assembler import PDFReportAssembler
  25. def register_report(name, model, parser=rml_parse):
  26. """Register the report into the services"""
  27. name = 'report.%s' % name
  28. if netsvc.Service._services.get(name, False):
  29. service = netsvc.Service._services[name]
  30. if isinstance(service, PDFReportAssembler):
  31. #already instantiated properly, skip it
  32. return
  33. if hasattr(service, 'parser'):
  34. parser = service.parser
  35. del netsvc.Service._services[name]
  36. PDFReportAssembler(name, model, parser=parser)
  37. class ReportAssembleXML(orm.Model):
  38. _name = 'ir.actions.report.xml'
  39. _inherit = 'ir.actions.report.xml'
  40. def __init__(self, pool, cr):
  41. super(ReportAssembleXML, self).__init__(pool, cr)
  42. def register_all(self,cursor):
  43. value = super(ReportAssembleXML, self).register_all(cursor)
  44. cursor.execute("SELECT * FROM ir_act_report_xml WHERE report_type = 'assemblage'")
  45. records = cursor.dictfetchall()
  46. for record in records:
  47. register_report(record['report_name'], record['model'])
  48. return value
  49. def unlink(self, cursor, user, ids, context=None):
  50. """Delete report and unregister it"""
  51. trans_obj = self.pool.get('ir.translation')
  52. trans_ids = trans_obj.search(
  53. cursor,
  54. user,
  55. [('type', '=', 'report'), ('res_id', 'in', ids)]
  56. )
  57. trans_obj.unlink(cursor, user, trans_ids)
  58. # Warning: we cannot unregister the services at the moment
  59. # because they are shared across databases. Calling a deleted
  60. # report will fail so it's ok.
  61. res = super(ReportAssembleXML, self).unlink(
  62. cursor,
  63. user,
  64. ids,
  65. context
  66. )
  67. return res
  68. def create(self, cursor, user, vals, context=None):
  69. "Create report and register it"
  70. res = super(ReportAssembleXML, self).create(cursor, user, vals, context)
  71. if vals.get('report_type','') == 'assemblage':
  72. # I really look forward to virtual functions :S
  73. register_report(
  74. vals['report_name'],
  75. vals['model'],
  76. )
  77. return res
  78. def write(self, cr, uid, ids, vals, context=None):
  79. "Edit report and manage its registration"
  80. if isinstance(ids, (int, long)):
  81. ids = [ids,]
  82. for rep in self.browse(cr, uid, ids, context=context):
  83. if rep.report_type != 'assemblage':
  84. continue
  85. if vals.get('report_name', False) and \
  86. vals['report_name'] != rep.report_name:
  87. report_name = vals['report_name']
  88. else:
  89. report_name = rep.report_name
  90. register_report(
  91. report_name,
  92. vals.get('model', rep.model),
  93. False
  94. )
  95. res = super(ReportAssembleXML, self).write(cr, uid, ids, vals, context)
  96. return res
  97. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: