ofxparse update

This commit is contained in:
Graeme Gellatly
2014-02-02 22:27:27 +13:00
parent 50f47bc31b
commit d005ec45b0
2 changed files with 43 additions and 27 deletions

View File

@@ -34,7 +34,7 @@ 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): def __init__(self, parser_name, *args, **kwargs):
""" """
""" """
@@ -60,25 +60,23 @@ class OfxParser(BankStatementImportParser):
""" """
return True return True
def _prepare_lines(self, transactions):
return [{'date': transaction.date,
'amount': transaction.amount,
'ref': transaction.type,
'label': transaction.payee} for transaction in transactions]
def _parse(self, *args, **kwargs): def _parse(self, *args, **kwargs):
""" """
Launch the parsing itself. Launch the parsing itself.
""" """
ofx_file = tempfile.NamedTemporaryFile() with tempfile.NamedTemporaryFile() as ofx_file:
ofx_file.seek(0) ofx_file.seek(0)
ofx_file.write(self.filebuffer) ofx_file.write(self.filebuffer)
ofx_file.flush() ofx_file.flush()
ofx = ofxparse.OfxParser.parse(file(ofx_file.name)) ofx = ofxparse.OfxParser.parse(file(ofx_file.name))
ofx_file.close() ofx_file.close()
res = [] self.result_row_list = self._prepare_lines(ofx.account.statement.transactions)
for transaction in ofx.account.statement.transactions:
res.append({
'date': transaction.date,
'amount': transaction.amount,
'ref': transaction.type,
'label': transaction.payee,
})
self.result_row_list = res
return True return True
def _validate(self, *args, **kwargs): def _validate(self, *args, **kwargs):
@@ -93,12 +91,6 @@ class OfxParser(BankStatementImportParser):
""" """
return True return True
def _post(self, *args, **kwargs):
"""
Nothing to do.
"""
return True
def get_st_line_vals(self, line, *args, **kwargs): def get_st_line_vals(self, line, *args, **kwargs):
""" """
This method must return a dict of vals that can be passed to create This method must return a dict of vals that can be passed to create
@@ -118,3 +110,27 @@ class OfxParser(BankStatementImportParser):
'label': line.get('label', ''), 'label': line.get('label', ''),
} }
class OfxParser2(OfxParser):
"""
Class for defining parser for OFX file format. Only differs from above
by setting ref to be the memo rather than type field
"""
def __init__(self, parser_name, *args, **kwargs):
"""
"""
super(OfxParser2, self).__init__(parser_name, *args, **kwargs)
@classmethod
def parser_for(cls, parser_name):
"""
Used by the new_bank_statement_parser class factory. Return true if
the providen name is 'ofx_alt'.
"""
return parser_name == 'ofx_alt'
def _prepare_lines(self, transactions):
return [{'date': transaction.date,
'amount': transaction.amount,
'ref': transaction.memo,
'label': transaction.payee} for transaction in transactions]

View File

@@ -21,17 +21,17 @@
from openerp.tools.translate import _ from openerp.tools.translate import _
from openerp.osv import fields, orm from openerp.osv import fields, orm
class AccountStatementProfil(orm.Model): class AccountStatementProfile(orm.Model):
_inherit = "account.statement.profile" _inherit = "account.statement.profile"
def get_import_type_selection(self, cr, uid, context=None): def get_import_type_selection(self, cr, uid, context=None):
""" """
Inherited from parent to add parser. Inherited from parent to add parser.
""" """
selection = super(AccountStatementProfil, self selection = super(AccountStatementProfile, self
).get_import_type_selection(cr, uid, ).get_import_type_selection(cr, uid, context=context)
context=context) selection.extend([('ofx_so', _('OFX - Open Financial Exchange')),
selection.append(('ofx_so', _('OFX - Open Financial Exchange'))) ('ofx_alt', _('OFX - (memo rather than type in ref field)'))])
return selection return selection
_columns = { _columns = {