[IMP] account_statement_base_import: Fix PEP8

This commit is contained in:
Pedro M. Baeza
2014-08-04 18:42:00 +02:00
parent aebcbfdd5d
commit d9a943d2d3
7 changed files with 192 additions and 231 deletions

View File

@@ -20,16 +20,14 @@
##############################################################################
import sys
import traceback
from openerp.tools.translate import _
import datetime
from openerp.osv.orm import Model
from openerp.osv import fields, osv
from openerp.osv import fields, orm
from parser import new_bank_statement_parser
from openerp.tools.config import config
class AccountStatementProfil(Model):
class AccountStatementProfil(orm.Model):
_inherit = "account.statement.profile"
def _get_import_type_selection(self, cr, uid, context=None):
@@ -62,52 +60,51 @@ class AccountStatementProfil(Model):
}
def _write_extra_statement_lines(
self, cr, uid, parser, result_row_list, profile, statement_id, context):
self, cr, uid, parser, result_row_list, profile, statement_id,
context):
"""Insert extra lines after the main statement lines.
After the main statement lines have been created, you can override this method to create
extra statement lines.
After the main statement lines have been created, you can override this
method to create extra statement lines.
:param: browse_record of the current parser
:param: result_row_list: [{'key':value}]
:param: profile: browserecord of account.statement.profile
:param: statement_id: int/long of the current importing statement ID
:param: statement_id: int/long of the current importing \
statement ID
:param: context: global context
"""
pass
def write_logs_after_import(self, cr, uid, ids, statement_id, num_lines, context):
"""
Write the log in the logger
def write_logs_after_import(self, cr, uid, ids, statement_id, num_lines,
context):
"""Write the log in the logger
:param int/long statement_id: ID of the concerned account.bank.statement
:param int/long num_lines: Number of line that have been parsed
:return: True
"""
self.message_post(cr,
uid,
ids,
body=_('Statement ID %s have been imported with %s lines.') %
(statement_id, num_lines),
context=context)
self.message_post(
cr, uid, ids,
body=_('Statement ID %s have been imported with %s '
'lines.') % (statement_id, num_lines), context=context)
return True
# Deprecated remove on V8
def prepare_statetement_lines_vals(self, *args, **kwargs):
return self.prepare_statement_lines_vals(*args, **kwargs)
def prepare_statement_lines_vals(
self, cr, uid, parser_vals,
statement_id, context):
"""
Hook to build the values of a line from the parser returned values. At
least it fullfill the statement_id. Overide it to add your
own completion if needed.
def prepare_statement_lines_vals(self, cr, uid, parser_vals,
statement_id, context):
"""Hook to build the values of a line from the parser returned values.
At least it fullfill the statement_id. Overide it to add your own
completion if needed.
:param dict of vals from parser for account.bank.statement.line (called by
parser.get_st_line_vals)
:param dict of vals from parser for account.bank.statement.line \
(called by parser.get_st_line_vals)
:param int/long statement_id: ID of the concerned account.bank.statement
:return: dict of vals that will be passed to create method of statement line.
:return: dict of vals that will be passed to create method of \
statement line.
"""
statement_line_obj = self.pool['account.bank.statement.line']
values = parser_vals
@@ -121,10 +118,8 @@ class AccountStatementProfil(Model):
values['period_id'] = period_memoizer[date]
else:
# This is awfully slow...
periods = self.pool.get('account.period').find(cr, uid,
dt=values.get(
'date'),
context=context)
periods = self.pool.get('account.period').find(
cr, uid, dt=values.get('date'), context=context)
values['period_id'] = periods[0]
period_memoizer[date] = periods[0]
values = statement_line_obj._add_missing_default_values(
@@ -133,8 +128,7 @@ class AccountStatementProfil(Model):
def prepare_statement_vals(self, cr, uid, profile_id, result_row_list,
parser, context=None):
"""
Hook to build the values of the statement from the parser and
"""Hook to build the values of the statement from the parser and
the profile.
"""
vals = {'profile_id': profile_id}
@@ -151,9 +145,8 @@ class AccountStatementProfil(Model):
def multi_statement_import(self, cr, uid, ids, profile_id, file_stream,
ftype="csv", context=None):
"""
Create multiple bank statements from values given by the parser for the
givenprofile.
"""Create multiple bank statements from values given by the parser for
the given profile.
:param int/long profile_id: ID of the profile used to import the file
:param filebuffer file_stream: binary of the providen file
@@ -162,23 +155,26 @@ class AccountStatementProfil(Model):
"""
prof_obj = self.pool['account.statement.profile']
if not profile_id:
raise osv.except_osv(_("No Profile!"),
_("You must provide a valid profile to import a bank statement!"))
raise orm.except_orm(
_("No Profile!"),
_("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, ftype=ftype)
res = []
for result_row_list in parser.parse(file_stream):
statement_id = self._statement_import(cr, uid, ids, prof, parser,
file_stream, ftype=ftype, context=context)
statement_id = self._statement_import(
cr, uid, ids, prof, parser, file_stream, ftype=ftype,
context=context)
res.append(statement_id)
return res
def _statement_import(self, cr, uid, ids, prof, parser, file_stream, ftype="csv", context=None):
"""
Create a bank statement with the given profile and parser. It will fullfill the bank statement
with the values of the file providen, but will not complete data (like finding the partner, or
the right account). This will be done in a second step with the completion rules.
def _statement_import(self, cr, uid, ids, prof, parser, file_stream,
ftype="csv", context=None):
"""Create a bank statement with the given profile and parser. It will
fullfill the bank statement with the values of the file providen, but
will not complete data (like finding the partner, or the right account).
This will be done in a second step with the completion rules.
:param prof : The profile used to import the file
:param parser: the parser
@@ -186,28 +182,25 @@ class AccountStatementProfil(Model):
:param char: ftype represent the file exstension (csv by default)
:return: ID of the created account.bank.statemênt
"""
statement_obj = self.pool.get('account.bank.statement')
statement_line_obj = self.pool.get('account.bank.statement.line')
attachment_obj = self.pool.get('ir.attachment')
statement_obj = self.pool['account.bank.statement']
statement_line_obj = self.pool['account.bank.statement.line']
attachment_obj = self.pool['ir.attachment']
result_row_list = parser.result_row_list
# Check all key are present in account.bank.statement.line!!
if not result_row_list:
raise osv.except_osv(_("Nothing to import"),
raise orm.except_orm(_("Nothing to import"),
_("The file is empty"))
parsed_cols = parser.get_st_line_vals(result_row_list[0]).keys()
for col in parsed_cols:
if col not in statement_line_obj._columns:
raise osv.except_osv(_("Missing column!"),
_("Column %s you try to import is not "
"present in the bank statement line!") % col)
raise orm.except_orm(
_("Missing column!"),
_("Column %s you try to import is not present in the bank "
"statement line!") % col)
statement_vals = self.prepare_statement_vals(
cr, uid, prof.id, result_row_list, parser, context)
statement_id = statement_obj.create(cr, uid,
statement_vals,
context=context)
statement_id = statement_obj.create(
cr, uid, statement_vals, context=context)
try:
# Record every line in the bank statement
statement_store = []
@@ -220,7 +213,6 @@ class AccountStatementProfil(Model):
# Hack to bypass ORM poor perfomance. Sob...
statement_line_obj._insert_lines(
cr, uid, statement_store, context=context)
self._write_extra_statement_lines(
cr, uid, parser, result_row_list, prof, statement_id, context)
# Trigger store field computation if someone has better idea
@@ -229,27 +221,24 @@ class AccountStatementProfil(Model):
start_bal = start_bal['balance_start']
statement_obj.write(
cr, uid, [statement_id], {'balance_start': start_bal})
attachment_data = {
'name': 'statement file',
'datas': file_stream,
'datas_fname': "%s.%s" % (datetime.datetime.now().date(), ftype),
'datas_fname': "%s.%s" % (datetime.datetime.now().date(),
ftype),
'res_model': 'account.bank.statement',
'res_id': statement_id,
}
attachment_obj.create(cr, uid, attachment_data, context=context)
# If user ask to launch completion at end of import, do it!
if prof.launch_import_completion:
statement_obj.button_auto_completion(
cr, uid, [statement_id], context)
# Write the needed log infos on profile
self.write_logs_after_import(cr, uid, prof.id,
statement_id,
len(result_row_list),
context)
except Exception:
error_type, error_value, trbk = sys.exc_info()
st = "Error: %s\nDescription: %s\nTraceback:" % (
@@ -260,6 +249,6 @@ class AccountStatementProfil(Model):
# For now we avoid re-catching error in debug mode
if config['debug_mode']:
raise
raise osv.except_osv(_("Statement import error"),
raise orm.except_orm(_("Statement import error"),
_("The statement cannot be created: %s") % st)
return statement_id