[IMP] sql_export_excel: black, isort, prettier

This commit is contained in:
hkapatel
2021-06-21 18:18:03 +05:30
committed by Maksym Yankin
parent 7cce5a0286
commit 81ba189c52
4 changed files with 114 additions and 83 deletions

View File

@@ -1,71 +1,79 @@
# Copyright 2019 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api, exceptions, fields, models, _
from io import BytesIO
import logging
import base64
import logging
from io import BytesIO
from odoo import _, api, exceptions, fields, models
_logger = logging.getLogger(__name__)
try:
import openpyxl
except ImportError:
_logger.debug('Can not import openpyxl')
_logger.debug("Can not import openpyxl")
class SqlExport(models.Model):
_inherit = 'sql.export'
_inherit = "sql.export"
file_format = fields.Selection(
selection_add=[('excel', 'Excel')])
file_format = fields.Selection(selection_add=[("excel", "Excel")])
header = fields.Boolean(
default=True,
help="Indicate if the header should be exported to the file.")
default=True, help="Indicate if the header should be exported to the file."
)
attachment_id = fields.Many2one(
'ir.attachment', string='Excel Template',
"ir.attachment",
string="Excel Template",
help="If you configure an excel file (in xlsx format) here, the "
"result of the query will be injected in it.\nIt is usefull to "
"feed data in a excel file pre-configured with calculation")
"result of the query will be injected in it.\nIt is usefull to "
"feed data in a excel file pre-configured with calculation",
)
sheet_position = fields.Integer(
default=1,
help="Indicate the sheet's position of the excel template where the "
"result of the sql query should be injected.")
"result of the sql query should be injected.",
)
row_position = fields.Integer(
default=1,
help="Indicate from which row the result of the query should be "
"injected.")
help="Indicate from which row the result of the query should be " "injected.",
)
col_position = fields.Integer(
string="Column Position",
default=1,
help="Indicate from which column the result of the query should be "
"injected.")
"injected.",
)
@api.constrains('sheet_position')
@api.constrains("sheet_position")
def check_sheet_position(self):
for export in self:
if export.sheet_position < 1:
raise exceptions.ValidationError(
_("The sheet position can't be less than 1."))
_("The sheet position can't be less than 1.")
)
@api.constrains('row_position')
@api.constrains("row_position")
def check_row_position(self):
for export in self:
if export.row_position < 1:
raise exceptions.ValidationError(
_("The row position can't be less than 1."))
_("The row position can't be less than 1.")
)
@api.constrains('col_position')
@api.constrains("col_position")
def check_column_position(self):
for export in self:
if export.col_position < 1:
raise exceptions.ValidationError(
_("The column position can't be less than 1."))
_("The column position can't be less than 1.")
)
@api.multi
def _get_file_extension(self):
self.ensure_one()
if self.file_format == 'excel':
return 'xlsx'
if self.file_format == "excel":
return "xlsx"
else:
return super()._get_file_extension()
@@ -73,7 +81,8 @@ class SqlExport(models.Model):
def excel_get_data_from_query(self, variable_dict):
self.ensure_one()
res = self._execute_sql_request(
params=variable_dict, mode='fetchall', header=self.header)
params=variable_dict, mode="fetchall", header=self.header
)
# Case we insert data in an existing excel file.
if self.attachment_id:
datas = self.attachment_id.datas
@@ -86,8 +95,11 @@ class SqlExport(models.Model):
ws = sheets[self.sheet_position - 1]
except IndexError:
raise exceptions.ValidationError(
_("The Excel Template file contains less than %s sheets "
"Please, adjust the Sheet Position parameter."))
_(
"The Excel Template file contains less than %s sheets "
"Please, adjust the Sheet Position parameter."
)
)
row_position = self.row_position or 1
col_position = self.col_position or 1
# Case of excel file creation