def walk(self, root, values, evaluate=False, include_layout=False): """Will walk the tree recursivley and yields every field node. Optionally you can yield every layout elements too. If evaluate parameter is true, then the function will only return fields which are relevant after the conditionals in the form has been evaluated. :root: Root node :values: Dictionary with values which are used for evaluating conditionals. :evaluate: Flag to indicate if evaluation should be done on conditionals :include_layout: Flag to indicate to include layout elements too. :returns: yields field elements """ if root is None: root = self._tree for child in root: if len(child) > 0: if child.tag == "if": if evaluate: try: rule = Rule(child.attrib.get('expr')) if not rule.evaluate(values): continue except TypeError: # FIXME: This error can happen if the rule # refers to values which are not contained in # the provided values dictionary. The value # might be missing because the converting of the # value failed or the value was missing at # all.(e.g the field was a selection field and # was "disabled" in a conditional. In this case # the value is not sent. (ti) <2015-04-28 16:52> continue for elem in self.walk(child, values, evaluate, include_layout): yield elem elif include_layout and child.tag in ["page", "section", "subsection"]: yield child for elem in self.walk(child, values, evaluate, include_layout): yield elem else: for elem in self.walk(child, values, evaluate, include_layout): yield elem elif child.tag == "snippet": sref = child.attrib.get('ref') if sref: snippet = self._parent.get_element('snippet', sref) for elem in self.walk(snippet, values, evaluate, include_layout): yield elem elif child.tag == "field": yield child
def evaluate(request): """Will return a JSON response with the result of the evaluation of the submitted formbar rule.""" rule = Rule(request.GET.get('rule')) result = rule.evaluate({}) return {"success": True, "data": result, "params": {"msg": rule.msg}}
def walk(self, root, values, evaluate=False, include_layout=False): """Will walk the tree recursivley and yields every field node. Optionally you can yield every layout elements too. If evaluate parameter is true, then the function will only return fields which are relevant after the conditionals in the form has been evaluated. :root: Root node :values: Dictionary with values which are used for evaluating conditionals. :evaluate: Flag to indicate if evaluation should be done on conditionals :include_layout: Flag to indicate to include layout elements too. :returns: yields field elements """ for child in root: if len(child) > 0: if child.tag == "if": if evaluate: try: rule = Rule(child.attrib.get('expr')) if not rule.evaluate(values): continue except TypeError: # FIXME: This error can happen if the rule # refers to values which are not contained in # the provided values dictionary. The value # might be missing because the converting of the # value failed or the value was missing at # all.(e.g the field was a selection field and # was "disabled" in a conditional. In this case # the value is not sent. (ti) <2015-04-28 16:52> continue for elem in self.walk(child, values, evaluate, include_layout): yield elem elif include_layout and child.tag in ["section", "subsection"]: yield child for elem in self.walk(child, values, evaluate, include_layout): yield elem else: for elem in self.walk(child, values, evaluate, include_layout): yield elem elif child.tag == "snippet": sref = child.attrib.get('ref') if sref: snippet = self._parent.get_element('snippet', sref) for elem in self.walk(snippet, values, evaluate, include_layout): yield elem elif child.tag == "field": yield child
def evaluate(request): """Method which will evaluate a formed rule given in the GET request. It will return a JSONResponse with the result of the evaluation.""" try: ruleexpr = request.GET.get('rule').strip() rule = Rule(ruleexpr) result = rule.evaluate({}) return JSONResponse(True, result, {"msg": rule.msg}) except: msg = "Can not evaluate rule '%s'" % ruleexpr log.error(msg) return JSONResponse(False, False, {"msg": msg})