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.

90 lines
3.5 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. variable_dict['company_id'] = self.env.user.company_id.id
  59. if "%(user_id)s" in sql_export.query:
  60. variable_dict['user_id'] = self.env.uid
  61. # Execute Request
  62. res = sql_export._execute_sql_request(
  63. params=variable_dict, mode='stdout',
  64. copy_options=sql_export.copy_options)
  65. if self.sql_export_id.encoding:
  66. res = res.decode(self.sql_export_id.encoding)
  67. self.write({
  68. 'binary_file': res,
  69. 'file_name': '%(name)s_%(date)s.csv' % {
  70. 'name': sql_export.name, 'date': date}
  71. })
  72. return {
  73. 'view_mode': 'form',
  74. 'res_model': 'sql.file.wizard',
  75. 'res_id': self.id,
  76. 'type': 'ir.actions.act_window',
  77. 'target': 'new',
  78. 'context': self.env.context,
  79. 'nodestroy': True,
  80. }