Skip to content

Commit dc35a34

Browse files
committed
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.
1 parent 9f6ecb0 commit dc35a34

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

caluma/caluma_form/structure.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def children(self):
134134

135135

136136
class FieldSet(Element):
137-
aliases = {"formMeta": "form_meta"}
137+
aliases = {"formMeta": "form_meta", "mainCaseForm": "main_case_form"}
138138

139139
def __init__(self, document, form, question=None, parent=None):
140140
super().__init__(parent)
@@ -144,6 +144,18 @@ def __init__(self, document, form, question=None, parent=None):
144144
self.question = question
145145
self._fields = None
146146
self._sub_forms = None
147+
self._main_case_form = "NOTSET"
148+
149+
@property
150+
def main_case_form(self):
151+
if self._main_case_form == "NOTSET":
152+
try:
153+
self._main_case_form = (
154+
self.document.family.work_item.case.family.document.form.slug
155+
)
156+
except Exception:
157+
self._main_case_form = None
158+
return self._main_case_form
147159

148160
@property
149161
def fields(self):

caluma/caluma_form/tests/test_jexl.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,25 @@ def test_reference_missing_question(
197197
("sub_question", "info.parent.form == 'top_form'", True, "subform"),
198198
("sub_question", "info.parent.formMeta.level == 0", True, "subform"),
199199
("sub_question", "info.parent.formMeta['is-top-form']", True, "subform"),
200+
("sub_question", "info.mainCaseForm == 'main-case-form'", True, "subform"),
200201
("column", "info.parent.form == 'top_form'", True, "table"),
201202
("column", "info.parent.formMeta.level == 0", True, "table"),
202203
("column", "info.parent.formMeta['is-top-form']", True, "table"),
203204
("column", "info.root.form == 'top_form'", True, "table"),
204205
("column", "info.root.formMeta.level == 0", True, "table"),
205206
("column", "info.root.formMeta['is-top-form']", True, "table"),
207+
("column", "info.mainCaseForm == 'main-case-form'", True, "table"),
206208
],
207209
)
208210
def test_new_jexl_expressions(
209-
question, expr, expectation, features, info, form_and_document
211+
question,
212+
expr,
213+
expectation,
214+
features,
215+
info,
216+
form_and_document,
217+
case_factory,
218+
work_item_factory,
210219
):
211220
"""Evaluate a JEXL expression in the context of a full document.
212221
@@ -231,6 +240,9 @@ def test_new_jexl_expressions(
231240
use_table=use_table, use_subform=use_subform
232241
)
233242

243+
main_case = case_factory(document__form__slug="main-case-form")
244+
work_item_factory(case=main_case, document=document)
245+
234246
# expression test method: we delete an answer and set it's is_hidden
235247
# to an expression to be tested. If the expression evaluates to True,
236248
# we won't have a ValidationError.

0 commit comments

Comments
 (0)