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.

94 lines
3.7 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. attrib = {'name': field.name}
  34. if field.required:
  35. attrib['required'] = 'True'
  36. view_field = etree.SubElement(
  37. group, 'field', attrib=attrib)
  38. osv.orm.setup_modifiers(
  39. view_field, self.fields_get(field.name))
  40. res['fields'].update(self.fields_get(toupdate_fields))
  41. placeholder = eview.xpath(
  42. "//separator[@string='variables_placeholder']")[0]
  43. placeholder.getparent().replace(
  44. placeholder, group)
  45. res['arch'] = etree.tostring(eview, pretty_print=True)
  46. return res
  47. def export_sql(self):
  48. self.ensure_one()
  49. sql_export = self.sql_export_id
  50. # Manage Params
  51. variable_dict = {}
  52. now_tz = fields.Datetime.context_timestamp(sql_export, datetime.now())
  53. date = now_tz.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
  54. if sql_export.field_ids:
  55. for field in sql_export.field_ids:
  56. variable_dict[field.name] = self[field.name]
  57. if "%(company_id)s" in sql_export.query:
  58. company_id = self.env.context.get(
  59. 'force_company', self.env.user.company_id.id)
  60. variable_dict['company_id'] = company_id
  61. if "%(user_id)s" in sql_export.query:
  62. user_id = self.env.context.get(
  63. 'force_user', self._uid)
  64. variable_dict['user_id'] = user_id
  65. # Execute Request
  66. res = sql_export._execute_sql_request(
  67. params=variable_dict, mode='stdout',
  68. copy_options=sql_export.copy_options)
  69. if self.sql_export_id.encoding:
  70. res = res.decode(self.sql_export_id.encoding)
  71. self.write({
  72. 'binary_file': res,
  73. 'file_name': '%(name)s_%(date)s.csv' % {
  74. 'name': sql_export.name, 'date': date}
  75. })
  76. return {
  77. 'view_mode': 'form',
  78. 'res_model': 'sql.file.wizard',
  79. 'res_id': self.id,
  80. 'type': 'ir.actions.act_window',
  81. 'target': 'new',
  82. 'context': self.env.context,
  83. 'nodestroy': True,
  84. }