cashbook: optimize for speed for checking rows

This commit is contained in:
Frederik Jaeckel 2023-12-29 22:55:41 +01:00
parent 85a96c12f8
commit c6832e1f19

28
book.py
View file

@ -4,7 +4,7 @@
# full copyright notices and license terms. # full copyright notices and license terms.
from trytond.model import Workflow, ModelView, ModelSQL, fields, Check, tree from trytond.model import Workflow, ModelView, ModelSQL, fields, Check, tree
from trytond.pyson import Eval, Or, Bool, Id, Len from trytond.pyson import Eval, Or, Bool, Id
from trytond.exceptions import UserError from trytond.exceptions import UserError
from trytond.i18n import gettext from trytond.i18n import gettext
from trytond.transaction import Transaction from trytond.transaction import Transaction
@ -62,8 +62,8 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
states={ states={
'readonly': Or( 'readonly': Or(
STATES['readonly'], STATES['readonly'],
Len(Eval('lines')) > 0), Eval('has_lines', False))},
}, depends=DEPENDS+['lines']) depends=DEPENDS+['has_lines'])
feature = fields.Function(fields.Char( feature = fields.Function(fields.Char(
string='Feature', readonly=True, string='Feature', readonly=True,
states={'invisible': True}), 'on_change_with_feature') states={'invisible': True}), 'on_change_with_feature')
@ -85,6 +85,9 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
string='Lines', field='cashbook', string='Lines', field='cashbook',
model_name='cashbook.line', model_name='cashbook.line',
states=STATES, depends=DEPENDS) states=STATES, depends=DEPENDS)
has_lines = fields.Function(fields.Boolean(
string='Has Lines', readonly=True, states={'invisible': True}),
'on_change_with_has_lines')
reconciliations = fields.One2Many( reconciliations = fields.One2Many(
string='Reconciliations', string='Reconciliations',
field='cashbook', model_name='cashbook.recon', field='cashbook', model_name='cashbook.recon',
@ -111,10 +114,10 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
states={ states={
'readonly': Or( 'readonly': Or(
STATES2['readonly'], STATES2['readonly'],
Len(Eval('lines')) > 0), Eval('has_lines', False)),
'invisible': STATES2['invisible'], 'invisible': STATES2['invisible'],
'required': ~STATES2['invisible'], 'required': ~STATES2['invisible'],
}, depends=DEPENDS2+['lines']) }, depends=DEPENDS2+['has_lines'])
value_store = fields.One2Many( value_store = fields.One2Many(
string='Values', model_name='cashbook.values', field='cashbook', string='Values', model_name='cashbook.values', field='cashbook',
@ -154,8 +157,8 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
states={ states={
'readonly': Or( 'readonly': Or(
STATES2['readonly'], STATES2['readonly'],
Len(Eval('lines', [])) > 0), Eval('has_lines', False))},
}, depends=DEPENDS2+['lines']) depends=DEPENDS2+['has_lines'])
currency_digits = fields.Function(fields.Integer( currency_digits = fields.Function(fields.Integer(
string='Currency Digits', string='Currency Digits',
readonly=True), 'on_change_with_currency_digits') readonly=True), 'on_change_with_currency_digits')
@ -519,6 +522,17 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
record[2], record[5], record[3]) record[2], record[5], record[3])
return result return result
@fields.depends('id')
def on_change_with_has_lines(self, name=None):
""" return True if cashbook has lines
(we dont use 'if self.lines:' this would slow down the client)
"""
Line = Pool().get('cashbook.line')
if Line.search_count([('cashbook', '=', self.id)]):
return True
return False
@fields.depends('btype') @fields.depends('btype')
def on_change_with_feature(self, name=None): def on_change_with_feature(self, name=None):
""" get feature-set """ get feature-set