mirror of
https://github.com/Kalenis/kalenislims.git
synced 2023-12-14 07:13:04 +01:00
lims_interface: optimizes calculation of formulas when saving
This commit is contained in:
parent
c936cceda2
commit
4452a09bb0
1 changed files with 27 additions and 13 deletions
|
@ -2,7 +2,7 @@
|
||||||
# This file is part of lims_interface module for Tryton.
|
# This file is part of lims_interface module for Tryton.
|
||||||
# The COPYRIGHT file at the top level of this repository contains
|
# The COPYRIGHT file at the top level of this repository contains
|
||||||
# the full copyright notices and license terms.
|
# the full copyright notices and license terms.
|
||||||
from sql import (Table, Column as SqlColumn, Literal,
|
from sql import (Table as SqlTable, Column as SqlColumn, Literal,
|
||||||
Desc, Asc, NullsFirst, NullsLast)
|
Desc, Asc, NullsFirst, NullsLast)
|
||||||
from sql.aggregate import Count
|
from sql.aggregate import Count
|
||||||
import formulas
|
import formulas
|
||||||
|
@ -660,17 +660,28 @@ class Data(ModelSQL, ModelView):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update_formulas(cls, records=None):
|
def update_formulas(cls, records=None):
|
||||||
Column = Pool().get('lims.interface.column')
|
|
||||||
cursor = Transaction().connection.cursor()
|
cursor = Transaction().connection.cursor()
|
||||||
|
pool = Pool()
|
||||||
|
Compilation = Pool().get('lims.interface.compilation')
|
||||||
|
TableField = pool.get('lims.interface.table.field')
|
||||||
|
Column = pool.get('lims.interface.column')
|
||||||
|
|
||||||
table = cls.get_table()
|
compilation_id = Transaction().context.get(
|
||||||
sql_table = cls.get_sql_table()
|
'lims_interface_compilation')
|
||||||
interface = cls.get_interface()
|
if not compilation_id:
|
||||||
|
return
|
||||||
|
|
||||||
|
compilation = Compilation(compilation_id)
|
||||||
|
table = compilation.table
|
||||||
|
interface = compilation.interface
|
||||||
|
sql_table = SqlTable(table.name)
|
||||||
|
|
||||||
formula_fields = []
|
formula_fields = []
|
||||||
for field in table.fields_:
|
fields = TableField.search([
|
||||||
if not field.formula:
|
('table', '=', table),
|
||||||
continue
|
('formula', 'not in', [None, '']),
|
||||||
|
])
|
||||||
|
for field in fields:
|
||||||
col = Column.search([
|
col = Column.search([
|
||||||
('interface', '=', interface),
|
('interface', '=', interface),
|
||||||
('alias', '=', field.name),
|
('alias', '=', field.name),
|
||||||
|
@ -682,6 +693,7 @@ class Data(ModelSQL, ModelView):
|
||||||
})
|
})
|
||||||
if not formula_fields:
|
if not formula_fields:
|
||||||
return
|
return
|
||||||
|
formula_fields = sorted(formula_fields, key=lambda x: x['order'])
|
||||||
|
|
||||||
if not records:
|
if not records:
|
||||||
records = cls.search([])
|
records = cls.search([])
|
||||||
|
@ -689,7 +701,10 @@ class Data(ModelSQL, ModelView):
|
||||||
vals = {}
|
vals = {}
|
||||||
fields = []
|
fields = []
|
||||||
values = []
|
values = []
|
||||||
for field in sorted(formula_fields, key=lambda x: x['order']):
|
for field in formula_fields:
|
||||||
|
for x in (field['field'].inputs or '').split():
|
||||||
|
if x not in vals:
|
||||||
|
vals[x] = getattr(record, x)
|
||||||
field_name = field['field'].name
|
field_name = field['field'].name
|
||||||
value = record.get_formula_value(field['field'], vals)
|
value = record.get_formula_value(field['field'], vals)
|
||||||
if value is None:
|
if value is None:
|
||||||
|
@ -707,9 +722,8 @@ class Data(ModelSQL, ModelView):
|
||||||
def get_formula_value(self, field, vals={}):
|
def get_formula_value(self, field, vals={}):
|
||||||
ast = field.get_ast()
|
ast = field.get_ast()
|
||||||
inputs = []
|
inputs = []
|
||||||
if field.inputs:
|
for x in (field.inputs or '').split():
|
||||||
for x in field.inputs.split():
|
inputs.append(vals.get(x))
|
||||||
inputs.append(vals.get(x, getattr(self, x)))
|
|
||||||
try:
|
try:
|
||||||
value = ast(*inputs)
|
value = ast(*inputs)
|
||||||
except schedula.utils.exc.DispatcherError as e:
|
except schedula.utils.exc.DispatcherError as e:
|
||||||
|
@ -781,7 +795,7 @@ class Data(ModelSQL, ModelView):
|
||||||
def get_sql_table(cls):
|
def get_sql_table(cls):
|
||||||
table = cls.get_table()
|
table = cls.get_table()
|
||||||
if table:
|
if table:
|
||||||
return Table(table.name)
|
return SqlTable(table.name)
|
||||||
return super().__table__()
|
return super().__table__()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
Loading…
Reference in a new issue