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.

91 lines
3.5 KiB

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