Skip to content

Commit

Permalink
Merge pull request #2204 from projectcaluma/jexl-main-form
Browse files Browse the repository at this point in the history
feat(jexl): add main_form to info object
  • Loading branch information
winged authored Apr 25, 2024
2 parents 9f6ecb0 + bcb9136 commit 5d9dc6d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 13 additions & 1 deletion caluma/caluma_form/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
14 changes: 13 additions & 1 deletion caluma/caluma_form/tests/test_jexl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 5d9dc6d

Please sign in to comment.