← Back to team overview

banking-addons-team team mailing list archive

[Merge] lp:~camptocamp/banking-addons/improve_lookup into lp:banking-addons/bank-statement-reconcile-70

 

Nicolas Bessi - Camptocamp has proposed merging lp:~camptocamp/banking-addons/improve_lookup into lp:banking-addons/bank-statement-reconcile-70.

Requested reviews:
  Banking Addons Team (banking-addons-team)

For more details, see:
https://code.launchpad.net/~camptocamp/banking-addons/improve_lookup/+merge/154730

Fixes performance trouble when using bank_statement_label based completion rules by using memoizer pattern.

Add lines in context to be able to acces them in completion rules. It is not mandatory as we can do line.satement_id.line_ids but it is more efficient.

Some minor cleanup
-- 
https://code.launchpad.net/~camptocamp/banking-addons/improve_lookup/+merge/154730
Your team Banking Addons Team is requested to review the proposed merge of lp:~camptocamp/banking-addons/improve_lookup into lp:banking-addons/bank-statement-reconcile-70.
=== modified file 'account_statement_base_completion/statement.py'
--- account_statement_base_completion/statement.py	2013-03-01 13:33:03 +0000
+++ account_statement_base_completion/statement.py	2013-03-21 15:22:52 +0000
@@ -18,6 +18,8 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
+from collections import defaultdict
+
 from tools.translate import _
 from openerp.osv.orm import Model, fields
 from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
@@ -269,35 +271,46 @@
             """
         partner_obj = self.pool.get('res.partner')
         st_obj = self.pool.get('account.bank.statement.line')
+        res = {}
+        # As we have to iterate on each partner for each line,
+        # we memoize the pair to avoid
+        # to redo computation for each line.
+        # Follwing code can be done by a single SQL query
+        # but this option is not really maintanable
+        if not context.get('label_memoizer'):
+            context['label_memoizer'] = defaultdict(list)
+            partner_ids = partner_obj.search(cr,
+                                             uid,
+                                             [('bank_statement_label', '!=', False)])
+            line_ids = tuple(x.id for x in context.get('line_ids', []))
+            for partner in partner_obj.browse(cr, uid, partner_ids, context=context):
+                vals = '|'.join(x.strip() for x in partner.bank_statement_label.split(';'))
+                or_regex = ".*%s*." % vals
+                sql = ("SELECT id from account_bank_statement_line"
+                       " WHERE id in %s"
+                       " AND name ~* %s")
+                cr.execute(sql, (line_ids, or_regex))
+                pairs = cr.fetchall()
+                if not pairs:
+                    continue
+                for pair in pairs:
+                    context['label_memoizer'][pair[0]].append(partner)
         st_line = st_obj.browse(cr, uid, line_id, context=context)
-        res = {}
-        compt = 0
-        if st_line:
-            ids = partner_obj.search(
-                    cr,
-                    uid,
-                    [('bank_statement_label', '!=', False)],
-                    context=context)
-            for partner in partner_obj.browse(cr, uid, ids, context=context):
-                for partner_label in partner.bank_statement_label.split(';'):
-                    if partner_label in st_line.label:
-                        compt += 1
-                        res['partner_id'] = partner.id
-                        if compt > 1:
-                            raise ErrorTooManyPartner(
-                                    _('Line named "%s" (Ref:%s) was matched by '
-                                      'more than one partner.') %
-                                    (st_line.name, st_line.ref))
-            if res:
-                st_vals = st_obj.get_values_for_line(
-                        cr,
-                        uid,
-                        profile_id=st_line.statement_id.profile_id.id,
-                        partner_id=res.get('partner_id', False),
-                        line_type=st_line.type,
-                        amount=st_line.amount,
-                        context=context)
-                res.update(st_vals)
+        if st_line and st_line.id in context['label_memoizer']:
+            found_partner = context['label_memoizer'][st_line.id]
+            if len(found_partner) > 1:
+                raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by '
+                                            'more than one partner.') %
+                                          (st_line.name, st_line.ref))
+            res['partner_id'] = found_partner[0].id
+            st_vals = st_obj.get_values_for_line(cr,
+                                                 uid,
+                                                 profile_id=st_line.statement_id.profile_id.id,
+                                                 partner_id=found_partner[0].id,
+                                                 line_type=st_line.type,
+                                                 amount=st_line.amount,
+                                                 context=context)
+            res.update(st_vals)
         return res
 
     def get_from_label_and_partner_name(self, cr, uid, line_id, context=None):
@@ -332,14 +345,13 @@
                         _('Line named "%s" (Ref:%s) was matched by more '
                           'than one partner.') %
                         (st_line.name, st_line.ref))
-            for id in result[0]:
-                res['partner_id'] = id
+            res['partner_id'] = result[0][0] if result else False
             if res:
                 st_vals = st_obj.get_values_for_line(
                         cr,
                         uid,
                         profile_id=st_line.statement_id.profile_id.id,
-                        partner_id=res.get('partner_id', False),
+                        partner_id=res.get['partner_id'],
                         line_type=st_line.type,
                         amount=st_line.amount,
                         context=context)
@@ -475,6 +487,7 @@
         for stat in self.browse(cr, uid, ids, context=context):
             msg_lines = []
             ctx = context.copy()
+            ctx['line_ids'] = stat.line_ids
             for line in stat.line_ids:
                 res = {}
                 try:


Follow ups