← Back to team overview

banking-addons-team team mailing list archive

lp:~akretion-team/banking-addons/bank-statement-reconcile-70-account-easy-reconcile-add-commit into lp:banking-addons/bank-statement-reconcile-7.0

 

Sébastien BEAU - http://www.akretion.com has proposed merging lp:~akretion-team/banking-addons/bank-statement-reconcile-70-account-easy-reconcile-add-commit into lp:banking-addons/bank-statement-reconcile-7.0.

Requested reviews:
  Guewen Baconnier @ Camptocamp (gbaconnier-c2c)
  Frederic Clementi - Camptocamp (frederic-clementi)

For more details, see:
https://code.launchpad.net/~akretion-team/banking-addons/bank-statement-reconcile-70-account-easy-reconcile-add-commit/+merge/197756

Hi,
I have a customer that want to import all of the historic of bank statement, SO... So at the end I have a lot of account_move_line in my customer account and the reconciliation process failed.
To avoid to loose everything when an error occure, I propose to commit after each reconciliation.

What do you think?
-- 
https://code.launchpad.net/~akretion-team/banking-addons/bank-statement-reconcile-70-account-easy-reconcile-add-commit/+merge/197756
Your team Banking Addons Core Editors is subscribed to branch lp:banking-addons/bank-statement-reconcile-7.0.
=== modified file 'account_easy_reconcile/base_reconciliation.py'
--- account_easy_reconcile/base_reconciliation.py	2013-01-04 08:39:10 +0000
+++ account_easy_reconcile/base_reconciliation.py	2013-12-04 17:16:19 +0000
@@ -21,6 +21,26 @@
 
 from openerp.osv import fields, orm
 from operator import itemgetter, attrgetter
+import logging
+from contextlib import contextmanager
+
+_logger = logging.getLogger(__name__)
+
+@contextmanager
+def commit(cr):
+    """
+    Commit the cursor after the ``yield``, or rollback it if an
+    exception occurs.
+
+    Warning: using this method, the exceptions are logged then discarded.
+    """
+    try:
+        yield
+    except Exception, e:
+        cr.rollback()
+        _logger.exception('Error during an automatic workflow action.')
+    else:
+        cr.commit()
 
 
 class easy_reconcile_base(orm.AbstractModel):
@@ -187,22 +207,24 @@
 
             period_id = self.pool.get('account.period').find(
                 cr, uid, dt=date, context=context)[0]
-
-            ml_obj.reconcile(
-                cr, uid,
-                line_ids,
-                type='auto',
-                writeoff_acc_id=writeoff_account_id,
-                writeoff_period_id=period_id,
-                writeoff_journal_id=rec.journal_id.id,
-                context=rec_ctx)
-            return True, True
+            with commit(cr):
+                ml_obj.reconcile(
+                    cr, uid,
+                    line_ids,
+                    type='auto',
+                    writeoff_acc_id=writeoff_account_id,
+                    writeoff_period_id=period_id,
+                    writeoff_journal_id=rec.journal_id.id,
+                    context=rec_ctx)
+                return True, True
         elif allow_partial:
-            ml_obj.reconcile_partial(
-                cr, uid,
-                line_ids,
-                type='manual',
-                context=rec_ctx)
-            return True, False
+            with commit(cr):
+                ml_obj.reconcile_partial(
+                    cr, uid,
+                    line_ids,
+                    type='manual',
+                    context=rec_ctx)
+                return True, False
 
         return False, False
+