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.

157 lines
6.5 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Authors: Laetitia Gangloff
  5. # Copyright (c) 2015 Acsone SA/NV (http://www.acsone.eu)
  6. # All Rights Reserved
  7. #
  8. # WARNING: This program as such is intended to be used by professional
  9. # programmers who take the whole responsibility of assessing all potential
  10. # consequences resulting from its eventual inadequacies and bugs.
  11. # End users who are looking for a ready-to-use solution with commercial
  12. # guarantees and support are strongly advised to contact a Free Software
  13. # Service Company.
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as
  17. # published by the Free Software Foundation, either version 3 of the
  18. # License, or (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. #
  28. ##############################################################################
  29. from openerp.osv import orm, fields
  30. from openerp.tools.translate import _
  31. from openerp.addons.web.controllers import main
  32. from openerp.tools.safe_eval import safe_eval
  33. import time
  34. import base64
  35. class actions_server(orm.Model):
  36. _inherit = "ir.actions.server"
  37. def __init__(self, pool, cr):
  38. super(actions_server, self).__init__(pool, cr)
  39. # add state 'export_email'
  40. self._columns['state'].selection.append(('export_email',
  41. _('Export data by email')))
  42. return
  43. _columns = {'model': fields.related('model_id', 'model',
  44. type="char", size=256, string='Model'),
  45. 'filter_id': fields.many2one(
  46. 'ir.filters', string='Filter', ondelete='restrict'),
  47. 'email_template_id': fields.many2one(
  48. 'email.template', string='Template', ondelete='restrict'),
  49. 'saved_export_id': fields.many2one(
  50. 'ir.exports', string='Saved export', ondelete='restrict'),
  51. 'fields_to_export': fields.text(
  52. 'Fields to export',
  53. help="The list of fields to export. \
  54. The format is ['name', 'seller_ids/product_name']")
  55. }
  56. _defaults = {'fields_to_export': '[]'}
  57. def onchange_model_id(self, cr, uid, ids, model_id, context=None):
  58. """
  59. Used to set correct domain on filter_id and saved_export_id
  60. """
  61. data = {'model': False,
  62. 'filter_id': False,
  63. 'email_template_id': False,
  64. 'saved_export_id': False}
  65. if model_id:
  66. model = self.pool['ir.model'].browse(cr, uid, model_id,
  67. context=context)
  68. data.update({'model': model.model})
  69. return {'value': data}
  70. def _search_data(self, cr, uid, action, context=None):
  71. """
  72. search data to export
  73. """
  74. obj_pool = self.pool[action.model]
  75. domain = action.filter_id and safe_eval(
  76. action.filter_id.domain, {'time': time}) or []
  77. ctx = action.filter_id and safe_eval(
  78. action.filter_id.context) or context
  79. return obj_pool.search(cr, uid, domain,
  80. offset=0, limit=False, order=False,
  81. context=ctx)
  82. def _export_data(self, cr, uid, obj_ids, action, context=None):
  83. """
  84. export data
  85. """
  86. obj_pool = self.pool[action.model]
  87. export_fields = []
  88. if action.fields_to_export:
  89. export_fields = safe_eval(action.fields_to_export)
  90. if action.saved_export_id:
  91. # retrieve fields of the selected list
  92. export_fields.extend(
  93. [efl.name for efl in
  94. action.saved_export_id.export_fields])
  95. return export_fields, obj_pool.export_data(
  96. cr, uid, obj_ids, export_fields,
  97. context=context).get('datas', [])
  98. def _send_email(self, cr, uid, action, export_fields, export_data,
  99. context=None):
  100. """
  101. Prepare a message with the exported data to send with the
  102. template of the configuration
  103. """
  104. mail_compose = self.pool['mail.compose.message']
  105. values = mail_compose.onchange_template_id(
  106. cr, uid, 0, action.email_template_id, 'comment',
  107. action.model, 0, context=context)['value']
  108. values['partner_ids'] = [
  109. (4, partner_id) for partner_id in values.pop('partner_ids',
  110. [])
  111. ]
  112. export = main.CSVExport()
  113. filename = export.filename(action.model)
  114. content = export.from_data(export_fields, export_data)
  115. if isinstance(content, unicode):
  116. content = content.encode('utf-8')
  117. data_attach = {
  118. 'name': filename,
  119. 'datas': base64.b64encode(str(content)),
  120. 'datas_fname': filename,
  121. 'description': filename,
  122. }
  123. values['attachment_ids'] = [(0, 0, data_attach)]
  124. compose_id = mail_compose.create(
  125. cr, uid, values, context=context)
  126. return mail_compose.send_mail(cr, uid, [compose_id], context=context)
  127. def run(self, cr, uid, ids, context=None):
  128. """
  129. If the state of an action is export_email,
  130. export data related to the configuration and send the result by email
  131. """
  132. for action in self.browse(cr, uid, ids, context):
  133. if action.state == 'export_email':
  134. ids.remove(action.id)
  135. # search data to export
  136. obj_ids = self._search_data(cr, uid, action, context=context)
  137. # export data
  138. export_fields, export_data = self._export_data(
  139. cr, uid, obj_ids, action, context=context)
  140. # Prepare a message with the exported data to send with the
  141. # template of the configuration
  142. self._send_email(
  143. cr, uid, action, export_fields, export_data,
  144. context=context)
  145. return super(actions_server, self).run(cr, uid, ids, context=context)