Improvement on the account_statement_base_import module to have profile data stored on a variable across parsers methods, in order to be used to customize, for example, parsing with a parameter introduced on profiles.

Dependent modules have been changed accordingly.
This commit is contained in:
Pedro M. Baeza
2014-07-02 13:37:36 +02:00
parent e85ef765fc
commit c45e47d54e
4 changed files with 16 additions and 21 deletions

View File

@@ -42,14 +42,16 @@ class BankStatementImportParser(object):
from the FileParser instead. from the FileParser instead.
""" """
def __init__(self, parser_name, *args, **kwargs): def __init__(self, profile, *args, **kwargs):
# The name of the parser as it will be called # The name of the parser as it will be called
self.parser_name = parser_name self.parser_name = profile.import_type
# The result as a list of row. One row per line of data in the file, but # The result as a list of row. One row per line of data in the file, but
# not the commission one ! # not the commission one !
self.result_row_list = None self.result_row_list = None
# The file buffer on which to work on # The file buffer on which to work on
self.filebuffer = None self.filebuffer = None
# The profile record to access its parameters in any parser method
self.profile = profile
self.balance_start = None self.balance_start = None
self.balance_end = None self.balance_end = None
self.statement_name = None self.statement_name = None
@@ -217,13 +219,13 @@ def itersubclasses(cls, _seen=None):
yield sub yield sub
def new_bank_statement_parser(parser_name, *args, **kwargs): def new_bank_statement_parser(profile, *args, **kwargs):
""" """Return an instance of the good parser class based on the given profile.
Return an instance of the good parser class base on the providen name
:param char: parser_name :param profile: browse_record of import profile.
:return: class instance of parser_name providen. :return: class instance for given profile import type.
""" """
for cls in itersubclasses(BankStatementImportParser): for cls in itersubclasses(BankStatementImportParser):
if cls.parser_for(parser_name): if cls.parser_for(profile.import_type):
return cls(parser_name, *args, **kwargs) return cls(profile, *args, **kwargs)
raise ValueError raise ValueError

View File

@@ -153,7 +153,7 @@ class AccountStatementProfil(Model):
_("You must provide a valid profile to import a bank statement!")) _("You must provide a valid profile to import a bank statement!"))
prof = prof_obj.browse(cr, uid, profile_id, context=context) prof = prof_obj.browse(cr, uid, profile_id, context=context)
parser = new_bank_statement_parser(prof.import_type, ftype=ftype) parser = new_bank_statement_parser(prof, ftype=ftype)
res = [] res = []
for result_row_list in parser.parse(file_stream): for result_row_list in parser.parse(file_stream):
statement_id = self._statement_import(cr, uid, ids, prof, parser, statement_id = self._statement_import(cr, uid, ids, prof, parser,

View File

@@ -31,14 +31,7 @@ except:
raise Exception(_('Please install python lib ofxparse')) raise Exception(_('Please install python lib ofxparse'))
class OfxParser(BankStatementImportParser): class OfxParser(BankStatementImportParser):
""" """Class for defining parser for OFX file format."""
Class for defining parser for OFX file format.
"""
def __init__(self, parser_name, *args, **kwargs):
"""
"""
super(OfxParser, self).__init__(parser_name, *args, **kwargs)
@classmethod @classmethod
def parser_for(cls, parser_name): def parser_for(cls, parser_name):

View File

@@ -27,17 +27,17 @@ class TransactionIDFileParser(FileParser):
bank statement. bank statement.
""" """
def __init__(self, parse_name, ftype='csv', extra_fields=None, header=None, **kwargs): def __init__(self, profile, ftype='csv', extra_fields=None, header=None, **kwargs):
""" """
Add transaction_id in header keys Add transaction_id in header keys
:param char: parse_name: The name of the parser :param char: profile: Reference to the profile
:param char: ftype: extension of the file (could be csv or xls) :param char: ftype: extension of the file (could be csv or xls)
:param dict: extra_fields: extra fields to add to the conversion dict. In the format :param dict: extra_fields: extra fields to add to the conversion dict. In the format
{fieldname: fieldtype} {fieldname: fieldtype}
:param list: header : specify header fields if the csv file has no header :param list: header : specify header fields if the csv file has no header
""" """
extra_fields = {'transaction_id': unicode} extra_fields = {'transaction_id': unicode}
super(TransactionIDFileParser, self).__init__(parse_name, extra_fields=extra_fields, super(TransactionIDFileParser, self).__init__(profile, extra_fields=extra_fields,
ftype=ftype, header=header, **kwargs) ftype=ftype, header=header, **kwargs)
# ref is replaced by transaction_id thus we delete it from check # ref is replaced by transaction_id thus we delete it from check
self.keys_to_validate = [k for k in self.keys_to_validate if k != 'ref'] self.keys_to_validate = [k for k in self.keys_to_validate if k != 'ref']