From afe50f62ce1549ca1b7b959c73a1c9e74d30eb76 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Wed, 21 Aug 2019 13:57:13 +0200 Subject: [PATCH] Adapt module sql_export_mail to be compatible with multiple file format --- sql_export/wizard/wizard_file.py | 8 ++++++-- sql_export_mail/models/sql_export.py | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/sql_export/wizard/wizard_file.py b/sql_export/wizard/wizard_file.py index fad2c569c..66ec5a1cf 100644 --- a/sql_export/wizard/wizard_file.py +++ b/sql_export/wizard/wizard_file.py @@ -64,9 +64,13 @@ class SqlFileWizard(models.TransientModel): for field in sql_export.field_ids: variable_dict[field.name] = self[field.name] if "%(company_id)s" in sql_export.query: - variable_dict['company_id'] = self.env.user.company_id.id + company_id = self.env.context.get( + 'force_company', self.env.user.company_id.id) + variable_dict['company_id'] = company_id if "%(user_id)s" in sql_export.query: - variable_dict['user_id'] = self.env.uid + user_id = self.env.context.get( + 'force_user', self._uid) + variable_dict['user_id'] = user_id # Execute Request res = sql_export._execute_sql_request( diff --git a/sql_export_mail/models/sql_export.py b/sql_export_mail/models/sql_export.py index a4478c695..760b271cb 100644 --- a/sql_export_mail/models/sql_export.py +++ b/sql_export_mail/models/sql_export.py @@ -66,13 +66,23 @@ class SqlExport(models.Model): params=params, mode='fetchone') if not res: return - binary = self._execute_sql_request( - params=params, mode='stdout', copy_options=self.copy_options) + ctx = self.env.context.copy() + if params: + if 'user_id' in params: + ctx['force_user'] = params['user_id'] + if 'company_id' in params: + ctx['force_company'] = params['company_id'] + wizard = self.env['sql.file.wizard'].create({ + 'sql_export_id': self.id, + }) + wizard.with_context(ctx).export_sql() + binary = wizard.binary_file + filename = wizard.file_name msg_id = mail_template.send_mail(self.id, force_send=False) mail = self.env['mail.mail'].browse(msg_id) attach_vals = { 'name': now_time + ' - ' + self.name, - 'datas_fname': now_time + ' - ' + self.name + '.csv', + 'datas_fname': filename, 'datas': binary, 'res_model': 'mail.mail', 'res_id': mail.id,