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.

106 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2015 Akretion (<http://www.akretion.com>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. import datetime
  22. from lxml import etree
  23. from openerp import models, fields, api, osv
  24. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
  25. class SqlFileWizard(models.TransientModel):
  26. _name = "sql.file.wizard"
  27. _description = "Allow the user to save the file with sql request's data"
  28. binary_file = fields.Binary('File', readonly=True)
  29. file_name = fields.Char('File Name', readonly=True)
  30. sql_export_id = fields.Many2one(comodel_name='sql.export', required=True)
  31. @api.model
  32. def fields_view_get(self, view_id=None, view_type='form',
  33. toolbar=False, submenu=False):
  34. """
  35. Display dinamicaly parameter fields depending on the sql_export.
  36. """
  37. res = super(SqlFileWizard, self).fields_view_get(
  38. view_id=view_id, view_type=view_type, toolbar=toolbar,
  39. submenu=submenu)
  40. export_obj = self.env['sql.export']
  41. if view_type == 'form':
  42. sql_export = export_obj.browse(self._context.get('active_id'))
  43. if sql_export.field_ids:
  44. eview = etree.fromstring(res['arch'])
  45. group = etree.Element(
  46. 'group', name="variables_group", colspan="4")
  47. toupdate_fields = []
  48. for field in sql_export.field_ids:
  49. kwargs = {'name': "%s" % field.name}
  50. toupdate_fields.append(field.name)
  51. view_field = etree.SubElement(group, 'field', **kwargs)
  52. osv.orm.setup_modifiers(
  53. view_field, self.fields_get(field.name))
  54. res['fields'].update(self.fields_get(toupdate_fields))
  55. placeholder = eview.xpath(
  56. "//separator[@string='variables_placeholder']")[0]
  57. placeholder.getparent().replace(
  58. placeholder, group)
  59. res['arch'] = etree.tostring(eview, pretty_print=True)
  60. return res
  61. @api.multi
  62. def export_sql(self):
  63. self.ensure_one()
  64. sql_export = self.sql_export_id
  65. # Manage Params
  66. variable_dict = {}
  67. today = datetime.datetime.now()
  68. today_tz = fields.Datetime.context_timestamp(
  69. sql_export, today)
  70. date = today_tz.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
  71. if sql_export.field_ids:
  72. for field in sql_export.field_ids:
  73. variable_dict[field.name] = self[field.name]
  74. if "%(company_id)s" in sql_export.query:
  75. variable_dict['company_id'] = self.env.user.company_id.id
  76. if "%(user_id)s" in sql_export.query:
  77. variable_dict['user_id'] = self._uid
  78. # Execute Request
  79. res = sql_export._execute_sql_request(
  80. params=variable_dict, mode='stdout',
  81. copy_options=sql_export.copy_options)
  82. self.write({
  83. 'binary_file': res,
  84. 'file_name': sql_export.name + '_' + date + '.csv'
  85. })
  86. return {
  87. 'view_type': 'form',
  88. 'view_mode': 'form',
  89. 'res_model': 'sql.file.wizard',
  90. 'res_id': self.id,
  91. 'type': 'ir.actions.act_window',
  92. 'target': 'new',
  93. 'context': self._context,
  94. 'nodestroy': True,
  95. }