From bcb913668e2454ca0dd0c8990eba310efef32b96 Mon Sep 17 00:00:00 2001 From: Christian Zosel Date: Wed, 24 Apr 2024 17:21:20 +0200 Subject: [PATCH] feat(jexl): add main_case_form to info object This is convenient when you'd like to write a JEXL expression in a task form attached to some work item, that depends on the main case's form. --- caluma/caluma_form/structure.py | 14 +++++++++++++- caluma/caluma_form/tests/test_jexl.py | 14 +++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/caluma/caluma_form/structure.py b/caluma/caluma_form/structure.py index 80206bed2..310008229 100644 --- a/caluma/caluma_form/structure.py +++ b/caluma/caluma_form/structure.py @@ -134,7 +134,7 @@ def children(self): class FieldSet(Element): - aliases = {"formMeta": "form_meta"} + aliases = {"formMeta": "form_meta", "mainCaseForm": "main_case_form"} def __init__(self, document, form, question=None, parent=None): super().__init__(parent) @@ -144,6 +144,18 @@ def __init__(self, document, form, question=None, parent=None): self.question = question self._fields = None self._sub_forms = None + self._main_case_form = "NOTSET" + + @property + def main_case_form(self): + if self._main_case_form == "NOTSET": + try: + self._main_case_form = ( + self.document.family.work_item.case.family.document.form.slug + ) + except Exception: # pragma: no cover + self._main_case_form = None + return self._main_case_form @property def fields(self): diff --git a/caluma/caluma_form/tests/test_jexl.py b/caluma/caluma_form/tests/test_jexl.py index 8c43dfac0..81d25c01e 100644 --- a/caluma/caluma_form/tests/test_jexl.py +++ b/caluma/caluma_form/tests/test_jexl.py @@ -197,16 +197,25 @@ def test_reference_missing_question( ("sub_question", "info.parent.form == 'top_form'", True, "subform"), ("sub_question", "info.parent.formMeta.level == 0", True, "subform"), ("sub_question", "info.parent.formMeta['is-top-form']", True, "subform"), + ("sub_question", "info.mainCaseForm == 'main-case-form'", True, "subform"), ("column", "info.parent.form == 'top_form'", True, "table"), ("column", "info.parent.formMeta.level == 0", True, "table"), ("column", "info.parent.formMeta['is-top-form']", True, "table"), ("column", "info.root.form == 'top_form'", True, "table"), ("column", "info.root.formMeta.level == 0", True, "table"), ("column", "info.root.formMeta['is-top-form']", True, "table"), + ("column", "info.mainCaseForm == 'main-case-form'", True, "table"), ], ) def test_new_jexl_expressions( - question, expr, expectation, features, info, form_and_document + question, + expr, + expectation, + features, + info, + form_and_document, + case_factory, + work_item_factory, ): """Evaluate a JEXL expression in the context of a full document. @@ -231,6 +240,9 @@ def test_new_jexl_expressions( use_table=use_table, use_subform=use_subform ) + main_case = case_factory(document__form__slug="main-case-form") + work_item_factory(case=main_case, document=document) + # expression test method: we delete an answer and set it's is_hidden # to an expression to be tested. If the expression evaluates to True, # we won't have a ValidationError.