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.
"""
def __init__(self, parser_name, *args, **kwargs):
def __init__(self, profile, *args, **kwargs):
# 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
# not the commission one !
self.result_row_list = None
# The file buffer on which to work on
self.filebuffer = None
# The profile record to access its parameters in any parser method
self.profile = profile
self.balance_start = None
self.balance_end = None
self.statement_name = None
@@ -217,13 +219,13 @@ def itersubclasses(cls, _seen=None):
yield sub
def new_bank_statement_parser(parser_name, *args, **kwargs):
"""
Return an instance of the good parser class base on the providen name
:param char: parser_name
:return: class instance of parser_name providen.
def new_bank_statement_parser(profile, *args, **kwargs):
"""Return an instance of the good parser class based on the given profile.
:param profile: browse_record of import profile.
:return: class instance for given profile import type.
"""
for cls in itersubclasses(BankStatementImportParser):
if cls.parser_for(parser_name):
return cls(parser_name, *args, **kwargs)
if cls.parser_for(profile.import_type):
return cls(profile, *args, **kwargs)
raise ValueError

View File

@@ -153,7 +153,7 @@ class AccountStatementProfil(Model):
_("You must provide a valid profile to import a bank statement!"))
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 = []
for result_row_list in parser.parse(file_stream):
statement_id = self._statement_import(cr, uid, ids, prof, parser,

View File

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

View File

@@ -27,17 +27,17 @@ class TransactionIDFileParser(FileParser):
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
: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 dict: extra_fields: extra fields to add to the conversion dict. In the format
{fieldname: fieldtype}
:param list: header : specify header fields if the csv file has no header
"""
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)
# 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']