lims_analysis_sheet_stock: add interface function to obtains the value of a
given field from a related record
This commit is contained in:
parent
236432747d
commit
cd6de978d5
|
@ -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
|
|
@ -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>
|
|
@ -8,6 +8,10 @@ from trytond.pool import Pool, PoolMeta
|
|||
from trytond.transaction import Transaction
|
||||
from trytond.exceptions import UserError
|
||||
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):
|
||||
|
|
|
@ -6,4 +6,5 @@ xml:
|
|||
configuration.xml
|
||||
stock.xml
|
||||
sheet.xml
|
||||
function.xml
|
||||
message.xml
|
||||
|
|
Loading…
Reference in New Issue