lims_analysis_sheet_stock: add interface function to obtains the value of a

given field from a related record
This commit is contained in:
Adrián Bernardi 2021-02-04 11:08:55 -03:00
parent 236432747d
commit cd6de978d5
4 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
# This file is part of lims_analysis_sheet_stock module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from trytond.pool import Pool
from trytond.transaction import Transaction
custom_functions = {}
def get_m2o_field(m2o_field, m2o_name, field_name, dict_key=None):
pool = Pool()
Field = pool.get('lims.interface.table.field')
if not m2o_field or not m2o_name or not field_name:
return None
if isinstance(m2o_field, int):
table_id = Transaction().context.get('lims_interface_table')
column = Field.search([
('table', '=', table_id),
('name', '=', m2o_name),
('related_model', '!=', None),
])
if not column:
return None
Target = Pool().get(column[0].related_model.model)
m2o_field = Target(m2o_field)
if not hasattr(m2o_field, field_name):
return None
value = getattr(m2o_field, field_name, None)
if isinstance(value, dict):
if not dict_key or dict_key not in value:
return None
value = value[dict_key]
return value
custom_functions['M2O'] = get_m2o_field

View File

@ -0,0 +1,14 @@
<?xml version="1.0"?>
<tryton>
<data>
<!-- Functions -->
<record model="lims.interface.function" id="function_m2o">
<field name="name">M2O</field>
<field name="parameters">m2o_field, m2o_name, field_name, dict_key=None</field>
<field name="help">Obtains the value of a given field from a related record.</field>
</record>
</data>
</tryton>

View File

@ -8,6 +8,10 @@ from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction from trytond.transaction import Transaction
from trytond.exceptions import UserError from trytond.exceptions import UserError
from trytond.i18n import gettext from trytond.i18n import gettext
from trytond.modules.lims_interface.interface import FUNCTIONS
from .function import custom_functions
FUNCTIONS.update(custom_functions)
class TemplateAnalysisSheet(metaclass=PoolMeta): class TemplateAnalysisSheet(metaclass=PoolMeta):

View File

@ -6,4 +6,5 @@ xml:
configuration.xml configuration.xml
stock.xml stock.xml
sheet.xml sheet.xml
function.xml
message.xml message.xml