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.

98 lines
4.0 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. if field.ttype == 'many2one':
  57. variable_dict[field.name] = self[field.name].id
  58. elif field.ttype == 'many2many':
  59. variable_dict[field.name] = tuple(self[field.name].ids)
  60. else:
  61. variable_dict[field.name] = self[field.name]
  62. if "%(company_id)s" in sql_export.query:
  63. company_id = self.env.context.get(
  64. 'force_company', self.env.user.company_id.id)
  65. variable_dict['company_id'] = company_id
  66. if "%(user_id)s" in sql_export.query:
  67. user_id = self.env.context.get(
  68. 'force_user', self._uid)
  69. variable_dict['user_id'] = user_id
  70. # Call different method depending on file_type since the logic will be
  71. # different
  72. method_name = '%s_get_data_from_query' % sql_export.file_format
  73. data = getattr(sql_export, method_name)(variable_dict)
  74. extension = sql_export._get_file_extension()
  75. self.write({
  76. 'binary_file': data,
  77. 'file_name': '%(name)s_%(date)s.%(extension)s' % {
  78. 'name': sql_export.name, 'date': date, 'extension': extension}
  79. })
  80. return {
  81. 'view_mode': 'form',
  82. 'res_model': 'sql.file.wizard',
  83. 'res_id': self.id,
  84. 'type': 'ir.actions.act_window',
  85. 'target': 'new',
  86. 'context': self.env.context,
  87. 'nodestroy': True,
  88. }