From 63ce1391bd8982eea4734b374a1aa260c51cc0be Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Thu, 18 Feb 2016 11:31:50 +0100 Subject: [PATCH] Refactor to use write multi and remove debug code --- account.py | 7 ------- analytic.py | 35 ++++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/account.py b/account.py index 6270b38..4dd6064 100644 --- a/account.py +++ b/account.py @@ -1,7 +1,5 @@ # The COPYRIGHT file at the top level of this repository contains the full # copyright notices and license terms. -import logging -import time from itertools import chain from trytond.model import ModelView, fields @@ -223,7 +221,6 @@ class MoveLine: pool = Pool() AnalyticLine = pool.get('analytic_account.line') - start_time = time.time() todraft, tovalid = [], [] for line in lines: analytic_lines_by_root = {} @@ -248,10 +245,6 @@ class MoveLine: AnalyticLine.write(tovalid, { 'state': 'valid', }) - logging.getLogger(cls.__name__).debug( - "validate_analytic_lines(): %s seconds" - % (time.time() - start_time)) - return todraft + tovalid @classmethod def create(cls, vlist): diff --git a/analytic.py b/analytic.py index 221025d..5d709ab 100644 --- a/analytic.py +++ b/analytic.py @@ -442,23 +442,32 @@ class AnalyticLine: return lines @classmethod - def write(cls, lines, vals): + def write(cls, *args): MoveLine = Pool().get('account.move.line') - if any(k not in cls._check_modify_exclude for k in vals): - cls.check_modify(lines) + actions = iter(args) + lines_to_check, all_lines = [], [] + for lines, vals in zip(actions, actions): + if any(k not in cls._check_modify_exclude for k in vals): + lines_to_check.extend(lines) + all_lines.extend(lines) + cls.check_modify(lines_to_check) - move_lines = [l.move_line for l in lines if l.move_line] - super(AnalyticLine, cls).write(lines, vals) - move_lines += [l.move_line for l in lines if l.move_line] + move_lines = set([l.move_line for l in all_lines if l.move_line]) + super(AnalyticLine, cls).write(*args) + move_lines |= set([l.move_line for l in all_lines if l.move_line]) - if any(k not in cls._check_modify_exclude for k in vals): - cls.check_modify(lines) - - MoveLine.validate_analytic_lines(list(set(move_lines))) - todraft_lines = [l for l in lines - if (not l.move_line and l.state != 'draft')] - cls.write(todraft_lines, { + lines_to_check = [] + for lines, vals in zip(actions, actions): + if any(k not in cls._check_modify_exclude for k in vals): + lines_to_check.extend(lines) + cls.check_modify(lines_to_check) + MoveLine.validate_analytic_lines(list(move_lines)) + todraft_lines = [l for l in all_lines + if (not l.move_line and l.state != 'draft')] + # Call super to avoid_recursion error: + if todraft_lines: + super(AnalyticLine, cls).write(todraft_lines, { 'state': 'draft', })