[IMP] account_statement_base_import: support csv without header

(lp:c2c-financial-addons/6.1 rev 87.1.1)
This commit is contained in:
Benoit Guillot
2012-09-25 10:05:34 +02:00
parent a06beb348f
commit a2cc03e4f1
2 changed files with 15 additions and 7 deletions

View File

@@ -35,7 +35,7 @@ class FileParser(BankStatementImportParser):
Generic abstract class for defining parser for .csv or .xls file format. Generic abstract class for defining parser for .csv or .xls file format.
""" """
def __init__(self, parse_name, keys_to_validate=[], ftype='csv', convertion_dict=None, *args, **kwargs): def __init__(self, parse_name, keys_to_validate=[], ftype='csv', convertion_dict=None, header=None, *args, **kwargs):
""" """
:param char: parse_name : The name of the parser :param char: parse_name : The name of the parser
:param list: keys_to_validate : contain the key that need to be present in the file :param list: keys_to_validate : contain the key that need to be present in the file
@@ -48,6 +48,7 @@ class FileParser(BankStatementImportParser):
'amount': float, 'amount': float,
'commission_amount': float 'commission_amount': float
} }
:param list: header : specify header fields if the csv file has no header
""" """
super(FileParser, self).__init__(parse_name, *args, **kwargs) super(FileParser, self).__init__(parse_name, *args, **kwargs)
@@ -57,6 +58,7 @@ class FileParser(BankStatementImportParser):
raise Exception(_('Invalide file type %s. please use csv or xls') % (ftype)) raise Exception(_('Invalide file type %s. please use csv or xls') % (ftype))
self.keys_to_validate = keys_to_validate self.keys_to_validate = keys_to_validate
self.convertion_dict = convertion_dict self.convertion_dict = convertion_dict
self.fieldnames = header
def _custom_format(self, *args, **kwargs): def _custom_format(self, *args, **kwargs):
""" """
@@ -88,11 +90,16 @@ class FileParser(BankStatementImportParser):
""" """
We check that all the key of the given file (means header) are present We check that all the key of the given file (means header) are present
in the validation key providen. Otherwise, we raise an Exception. in the validation key providen. Otherwise, we raise an Exception.
We skip the validation step if the file header is providen separately
(in the field: fieldnames).
""" """
parsed_cols = self.result_row_list[0].keys() if hasattr(self, 'fieldnames') and self.fieldnames is not None:
for col in self.keys_to_validate: return True
if col not in parsed_cols: else:
raise Exception(_('Column %s not present in file') % (col)) parsed_cols = self.result_row_list[0].keys()
for col in self.keys_to_validate:
if col not in parsed_cols:
raise Exception(_('Column %s not present in file') % (col))
return True return True
def _post(self, *args, **kwargs): def _post(self, *args, **kwargs):
@@ -113,7 +120,8 @@ class FileParser(BankStatementImportParser):
csv_file.seek(0) csv_file.seek(0)
reader = UnicodeDictReader( reader = UnicodeDictReader(
open(csv_file.name).readlines(), open(csv_file.name).readlines(),
delimiter=delimiter delimiter=delimiter,
fieldnames=self.fieldnames
) )
return [x for x in reader] return [x for x in reader]

View File

@@ -164,7 +164,7 @@ class AccountStatementProfil(Model):
parser = new_bank_statement_parser(prof.import_type, ftype=ftype) parser = new_bank_statement_parser(prof.import_type, ftype=ftype)
result_row_list = parser.parse(file_stream) result_row_list = parser.parse(file_stream)
# Check all key are present in account.bank.statement.line !! # Check all key are present in account.bank.statement.line !!
parsed_cols = result_row_list[0].keys() parsed_cols = parser.get_st_line_vals(result_row_list[0]).keys()
for col in parsed_cols: for col in parsed_cols:
if col not in statement_line_obj._columns: if col not in statement_line_obj._columns:
raise osv.except_osv( raise osv.except_osv(