[IMP] account_statement_import_sheet_file: Use parse_header and fine-tune the UI

Before this commit, the parse_header function was unused. It is now utilized to reduce redundant code and improve maintainability.
Additionally, UI adjustments have been made for better usability and clarity.
This commit is contained in:
Carlos Lopez
2025-01-13 10:20:49 -05:00
parent bb47930f74
commit 6deab7c481
2 changed files with 18 additions and 32 deletions

View File

@@ -39,22 +39,22 @@ class AccountStatementImportSheetParser(models.TransientModel):
_description = "Bank Statement Import Sheet Parser" _description = "Bank Statement Import Sheet Parser"
@api.model @api.model
def parse_header(self, data_file, encoding, csv_options, header_lines_skip_count=0): def parse_header(self, csv_or_xlsx, mapping):
try: if mapping.no_header:
workbook = xlrd.open_workbook( return []
file_contents=data_file, header_line = mapping.header_lines_skip_count
encoding_override=encoding if encoding else None, # prevent negative indexes
) if header_line > 0:
sheet = workbook.sheet_by_index(0) header_line -= 1
values = sheet.row_values(header_lines_skip_count - 1) if isinstance(csv_or_xlsx, tuple):
return [str(value) for value in values] header = [
except xlrd.XLRDError: str(value).strip() for value in csv_or_xlsx[1].row_values(header_line)
_logger.error("Pass this method") ]
else:
data = StringIO(data_file.decode(encoding or "utf-8")) [next(csv_or_xlsx) for _i in range(header_line)]
csv_data = reader(data, **csv_options) header = [value.strip() for value in next(csv_or_xlsx)]
csv_data_lst = list(csv_data) if mapping.offset_column:
header = [value.strip() for value in csv_data_lst[header_lines_skip_count - 1]] header = header[mapping.offset_column :]
return header return header
@api.model @api.model
@@ -177,22 +177,7 @@ class AccountStatementImportSheetParser(models.TransientModel):
) from None ) from None
decoded_file = data_file.decode(detected_encoding) decoded_file = data_file.decode(detected_encoding)
csv_or_xlsx = reader(StringIO(decoded_file), **csv_options) csv_or_xlsx = reader(StringIO(decoded_file), **csv_options)
header = False header = self.parse_header(csv_or_xlsx, mapping)
if not mapping.no_header:
header_line = mapping.header_lines_skip_count
# prevent negative indexes
if header_line > 0:
header_line -= 1
if isinstance(csv_or_xlsx, tuple):
header = [
str(value).strip()
for value in csv_or_xlsx[1].row_values(header_line)
]
else:
[next(csv_or_xlsx) for _i in range(header_line)]
header = [value.strip() for value in next(csv_or_xlsx)]
if mapping.offset_column:
header = header[mapping.offset_column :]
# NOTE no seria necesario debit_column y credit_column ya que tenemos los # NOTE no seria necesario debit_column y credit_column ya que tenemos los
# respectivos campos related # respectivos campos related

View File

@@ -44,6 +44,7 @@
<div <div
class="alert alert-warning" class="alert alert-warning"
role="alert" role="alert"
colspan="2"
attrs="{'invisible': [('no_header', '=', False)]}" attrs="{'invisible': [('no_header', '=', False)]}"
> >
<span <span