Skip to content

Commit

Permalink
feat(jexl): allow usage of form meta in question jexl
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Jan 22, 2021
1 parent 12c24eb commit f3f1223
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
9 changes: 8 additions & 1 deletion caluma/caluma_form/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions caluma/caluma_form/tests/test_jexl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions caluma/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit f3f1223

Please sign in to comment.