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.

87 lines
3.4 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 datetime import datetime
  5. from lxml import etree
  6. from odoo import models, fields, api, osv
  7. from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
  8. class SqlFileWizard(models.TransientModel):
  9. _name = "sql.file.wizard"
  10. _description = "Allow the user to save the file with sql request's data"
  11. binary_file = fields.Binary('File', readonly=True)
  12. file_name = fields.Char('File Name', readonly=True)
  13. sql_export_id = fields.Many2one(comodel_name='sql.export', required=True)
  14. @api.model
  15. def fields_view_get(self, view_id=None, view_type='form',
  16. toolbar=False, submenu=False):
  17. """
  18. Display dynamically parameter fields depending on the sql_export.
  19. """
  20. res = super(SqlFileWizard, self).fields_view_get(
  21. view_id=view_id, view_type=view_type, toolbar=toolbar,
  22. submenu=submenu)
  23. export_obj = self.env['sql.export']
  24. if view_type == 'form':
  25. sql_export = export_obj.browse(self.env.context.get('active_id'))
  26. if sql_export.field_ids:
  27. eview = etree.fromstring(res['arch'])
  28. group = etree.Element(
  29. 'group', name="variables_group", colspan="4")
  30. toupdate_fields = []
  31. for field in sql_export.field_ids:
  32. toupdate_fields.append(field.name)
  33. view_field = etree.SubElement(
  34. group, 'field', name=field.name)
  35. osv.orm.setup_modifiers(
  36. view_field, self.fields_get(field.name))
  37. res['fields'].update(self.fields_get(toupdate_fields))
  38. placeholder = eview.xpath(
  39. "//separator[@string='variables_placeholder']")[0]
  40. placeholder.getparent().replace(
  41. placeholder, group)
  42. res['arch'] = etree.tostring(eview, pretty_print=True)
  43. return res
  44. def export_sql(self):
  45. self.ensure_one()
  46. sql_export = self.sql_export_id
  47. # Manage Params
  48. variable_dict = {}
  49. now_tz = fields.Datetime.context_timestamp(sql_export, datetime.now())
  50. date = now_tz.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
  51. if sql_export.field_ids:
  52. for field in sql_export.field_ids:
  53. variable_dict[field.name] = self[field.name]
  54. if "%(company_id)s" in sql_export.query:
  55. variable_dict['company_id'] = self.env.user.company_id.id
  56. if "%(user_id)s" in sql_export.query:
  57. variable_dict['user_id'] = self.env.uid
  58. # Execute Request
  59. res = sql_export._execute_sql_request(
  60. params=variable_dict, mode='stdout',
  61. copy_options=sql_export.copy_options)
  62. if self.sql_export_id.encoding:
  63. res = res.decode(self.sql_export_id.encoding)
  64. self.write({
  65. 'binary_file': res,
  66. 'file_name': '%(name)s_%(date)s.csv' % {
  67. 'name': sql_export.name, 'date': date}
  68. })
  69. return {
  70. 'view_mode': 'form',
  71. 'res_model': 'sql.file.wizard',
  72. 'res_id': self.id,
  73. 'type': 'ir.actions.act_window',
  74. 'target': 'new',
  75. 'context': self.env.context,
  76. 'nodestroy': True,
  77. }