From f3f1223790fcf1aebf0908657365429557e5ed68 Mon Sep 17 00:00:00 2001 From: Jonas Metzener Date: Fri, 22 Jan 2021 08:41:04 +0100 Subject: [PATCH] feat(jexl): allow usage of form meta in question jexl --- caluma/caluma_form/structure.py | 9 ++++++++- caluma/caluma_form/tests/test_jexl.py | 9 +++++++++ caluma/conftest.py | 6 ++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/caluma/caluma_form/structure.py b/caluma/caluma_form/structure.py index 715d742aa..6cfe64dbf 100644 --- a/caluma/caluma_form/structure.py +++ b/caluma/caluma_form/structure.py @@ -22,6 +22,8 @@ def new_method(self, *args, **kwargs): class Element: + aliases = {} + def __init__(self, parent=None): self._parent = weakref.ref(parent) if parent else None @@ -38,12 +40,14 @@ def root(self): return self def get(self, name, default=None): + name = self.aliases.get(name, name) + out = getattr(self, name) # if a method is requested, execute it before continuing if callable(out): out = out() - if isinstance(out, Element): + if isinstance(out, Element) or isinstance(out, dict): return out return str(out) @@ -125,10 +129,13 @@ def children(self): class FieldSet(Element): + aliases = {"formMeta": "form_meta"} + def __init__(self, document, form, question=None, parent=None): super().__init__(parent) self.document = document self.form = form + self.form_meta = form.meta self.question = question self._fields = None self._sub_forms = None diff --git a/caluma/caluma_form/tests/test_jexl.py b/caluma/caluma_form/tests/test_jexl.py index f43168f3b..fc349a5e1 100644 --- a/caluma/caluma_form/tests/test_jexl.py +++ b/caluma/caluma_form/tests/test_jexl.py @@ -203,9 +203,18 @@ def test_reference_missing_question( "question,expr,expectation,features", [ ("sub_question", "info.form == 'sub_form'", True, "subform"), + ("sub_question", "info.formMeta.level == 1", True, "subform"), + ("sub_question", "info.formMeta['is-top-form']", False, "subform"), + ("sub_question", "info.formMeta['non-existent-key'] == null", True, "subform"), ("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"), ("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"), ], ) def test_new_jexl_expressions( diff --git a/caluma/conftest.py b/caluma/conftest.py index 9ea5d47c7..9c97c0d4d 100644 --- a/caluma/conftest.py +++ b/caluma/conftest.py @@ -222,7 +222,7 @@ def form_and_document( """ def factory(use_table=False, use_subform=False): - form = form_factory(slug="top_form") + form = form_factory(slug="top_form", meta={"is-top-form": True, "level": 0}) document = document_factory(form=form) questions = {} @@ -263,7 +263,9 @@ def factory(use_table=False, use_subform=False): answers["table"].documents.add(row_doc) if use_subform: - sub_form = form_factory(slug="sub_form") + sub_form = form_factory( + slug="sub_form", meta={"is-top-form": False, "level": 1} + ) questions["form"] = question_factory( type="form", slug="form",