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.1 KiB

  1. # Copyright (C) 2015 Akretion (<http://www.akretion.com>)
  2. # @author: Florian da Costa
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import models, fields
  5. class SqlExport(models.Model):
  6. _name = "sql.export"
  7. _inherit = ['sql.request.mixin']
  8. _description = "SQL export"
  9. _sql_request_groups_relation = 'groups_sqlquery_rel'
  10. _sql_request_users_relation = 'users_sqlquery_rel'
  11. _check_execution_enabled = False
  12. copy_options = fields.Char(
  13. string='Copy Options', required=False,
  14. default="CSV HEADER DELIMITER ';'")
  15. file_format = fields.Selection(
  16. [('csv', 'CSV')],
  17. default='csv',
  18. required=True)
  19. field_ids = fields.Many2many(
  20. 'ir.model.fields',
  21. 'fields_sqlquery_rel',
  22. 'sql_id',
  23. 'field_id',
  24. 'Parameters',
  25. domain=[('model', '=', 'sql.file.wizard')])
  26. encoding = fields.Selection(
  27. [('utf-8', 'utf-8'), ('utf-16', 'utf-16'),
  28. ('windows-1252', 'windows-1252'), ('latin1', 'latin1'),
  29. ('latin2', 'latin2'), ('big5', 'big5'), ('gb18030', 'gb18030'),
  30. ('shift_jis', 'shift_jis'), ('windows-1251', 'windows-1251'),
  31. ('koir8_r', 'koir8_r')], string='Encoding', required=True,
  32. default='utf-8')
  33. def export_sql_query(self):
  34. self.ensure_one()
  35. wiz = self.env['sql.file.wizard'].create({
  36. 'sql_export_id': self.id})
  37. return {
  38. 'view_mode': 'form',
  39. 'res_model': 'sql.file.wizard',
  40. 'res_id': wiz.id,
  41. 'type': 'ir.actions.act_window',
  42. 'target': 'new',
  43. 'context': self.env.context,
  44. 'nodestroy': True,
  45. }
  46. def _get_file_extension(self):
  47. self.ensure_one()
  48. if self.file_format == 'csv':
  49. return 'csv'
  50. def csv_get_data_from_query(self, variable_dict):
  51. self.ensure_one()
  52. # Execute Request
  53. res = self._execute_sql_request(
  54. params=variable_dict, mode='stdout',
  55. copy_options=self.copy_options)
  56. if self.encoding:
  57. res = res.decode(self.encoding)
  58. return res