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.

75 lines
3.2 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Authors: Laetitia Gangloff
  5. # Copyright (c) 2015 Acsone SA/NV (http://www.acsone.eu)
  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, fields
  22. from openerp.tools.translate import _
  23. class actions_server(orm.Model):
  24. _inherit = "ir.actions.server"
  25. def __init__(self, pool, cr):
  26. super(actions_server, self).__init__(pool, cr)
  27. # add state 'sql_export_email'
  28. new_state = ('sql_export_email', _('Export SQL data by email'))
  29. if new_state not in self._columns['state'].selection:
  30. self._columns['state'].selection.append(new_state)
  31. return
  32. _columns = {
  33. 'sql_export_id': fields.many2one(
  34. 'sql.export', string='SQL export', ondelete='restrict'),
  35. }
  36. def onchange_state(self, cr, uid, ids, state, context=None):
  37. res = {}
  38. if state == 'sql_export_email':
  39. # model is not used in this type of action, but it is a required
  40. # field, so set the model 'ir.actions.server'
  41. model = self.pool['ir.model'].search(
  42. cr, uid, [('model', '=', 'ir.actions.server')],
  43. context=context)
  44. res['value'] = {'model_id': model[0]}
  45. return res
  46. def run(self, cr, uid, ids, context=None):
  47. """
  48. If the state of an action is sql_export_email,
  49. get data related to the sql_export and send the result by email
  50. """
  51. for action in self.browse(cr, uid, ids, context):
  52. if action.state == 'sql_export_email':
  53. # get data to export
  54. res_id = self.pool['sql.export'].export_sql_query(
  55. cr, uid, [action.sql_export_id.id],
  56. context=context)['res_id']
  57. data = self.pool['sql.file.wizard'].browse(cr, uid, res_id,
  58. context=context)
  59. if context is None:
  60. context = {}
  61. # data is already encoded in base64
  62. context['encoded_base_64'] = True
  63. # Prepare a message with the exported data to send with the
  64. # template of the configuration
  65. self._send_email(
  66. cr, uid, action, data['file_name'], data['file'],
  67. context=context)
  68. return super(actions_server, self).run(cr, uid, ids, context=context)