From cd6de978d5f9c3da233c0dcea7a214f32824ed32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Bernardi?= Date: Thu, 4 Feb 2021 11:08:55 -0300 Subject: [PATCH] lims_analysis_sheet_stock: add interface function to obtains the value of a given field from a related record --- lims_analysis_sheet_stock/function.py | 42 ++++++++++++++++++++++++++ lims_analysis_sheet_stock/function.xml | 14 +++++++++ lims_analysis_sheet_stock/sheet.py | 4 +++ lims_analysis_sheet_stock/tryton.cfg | 1 + 4 files changed, 61 insertions(+) create mode 100644 lims_analysis_sheet_stock/function.py create mode 100644 lims_analysis_sheet_stock/function.xml diff --git a/lims_analysis_sheet_stock/function.py b/lims_analysis_sheet_stock/function.py new file mode 100644 index 0000000..6d45443 --- /dev/null +++ b/lims_analysis_sheet_stock/function.py @@ -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 diff --git a/lims_analysis_sheet_stock/function.xml b/lims_analysis_sheet_stock/function.xml new file mode 100644 index 0000000..6248d9c --- /dev/null +++ b/lims_analysis_sheet_stock/function.xml @@ -0,0 +1,14 @@ + + + + + + + + M2O + m2o_field, m2o_name, field_name, dict_key=None + Obtains the value of a given field from a related record. + + + + diff --git a/lims_analysis_sheet_stock/sheet.py b/lims_analysis_sheet_stock/sheet.py index d961bde..60a4351 100644 --- a/lims_analysis_sheet_stock/sheet.py +++ b/lims_analysis_sheet_stock/sheet.py @@ -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): diff --git a/lims_analysis_sheet_stock/tryton.cfg b/lims_analysis_sheet_stock/tryton.cfg index 8ef8360..ae42a10 100644 --- a/lims_analysis_sheet_stock/tryton.cfg +++ b/lims_analysis_sheet_stock/tryton.cfg @@ -6,4 +6,5 @@ xml: configuration.xml stock.xml sheet.xml + function.xml message.xml