Exemplo n.º 1
0
    def _update_xform_with_select_external_question(self, xform,
                                                    external_choice_question):
        if external_choice_question.parent_field_code:
            parent_node = self._get_parent_node(
                xform.get_body_node(),
                external_choice_question.parent_field_code)
            xform_node = get_node(parent_node, external_choice_question.code)
        else:
            xform_node = get_node(xform.get_body_node(),
                                  external_choice_question.code)

        query_string = xform_node.attrib['query']
        id = re.search("(instance\('(.+?)('\)))", query_string).group(2)
        instance_node = ET.Element("instance", {
            "id": id,
            "src": "jr://file-csv/itemsets.csv"
        })
        model_node = xform._model_node()
        add_node(model_node, instance_node)
        xform_bind_node = xform.bind_node(xform_node)
        xform_bind_node.attrib['type'] = 'select1'
        itemset_node = ET.Element("itemset", {"nodeset": query_string})
        value_node = ET.Element("value", {"ref": "name"})
        label_node = ET.Element("label", {"ref": "label"})
        add_node(itemset_node, value_node)
        add_node(itemset_node, label_node)
        add_node(xform_node, itemset_node)
        remove_attrib(xform_node, "query")
        xform_node.tag = re.sub('input', 'select1', xform_node.tag)
Exemplo n.º 2
0
    def _update_xform_with_unique_id_choices(self, root_node,
                                             uniqueid_ui_field):
        if uniqueid_ui_field.parent_field_code:
            parent_node = self._get_parent_node(
                root_node, uniqueid_ui_field.parent_field_code)
            node = get_node(parent_node, uniqueid_ui_field.code)
        else:
            node = get_node(root_node, uniqueid_ui_field.code)

        for child_node in node._children:
            if 'item' in child_node.tag:
                node.remove(child_node)  # removing the placeholder option

        choice_elements = self._get_choice_elements(uniqueid_ui_field.options)
        for element in choice_elements:
            node.append(element)
Exemplo n.º 3
0
    def _update_xform_with_new_fields(self, new_fields, old_fields,
                                      new_parent_node, old_parent_node,
                                      old_xform, new_xform,
                                      activity_log_detail):
        for new_field in new_fields:
            new_node = get_node(new_parent_node, new_field.code)
            old_field = [
                old_field for old_field in old_fields
                if old_field.code == new_field.code
            ]
            if old_field:
                if isinstance(new_field, FieldSet):
                    old_node = get_node(old_parent_node, new_field.code)
                    self._update_xform_with_new_fields(new_field.fields,
                                                       old_field[0].fields,
                                                       new_node, old_node,
                                                       old_xform, new_xform,
                                                       activity_log_detail)
            else:
                if isinstance(new_field, FieldSet):
                    return

                bind_node = new_xform.get_bind_node_by_name(
                    new_field.code
                ) if new_node is None else new_xform.bind_node(new_node)
                instance_node = new_xform.instance_node_given_name(
                    new_field.code).next(
                    ) if new_node is None else new_xform.instance_node(
                        new_node)
                self.add(old_parent_node, new_node, bind_node, instance_node,
                         old_xform)

                if isinstance(new_field, SelectField) and new_field.is_cascade:
                    for translation_node in new_xform.get_translation_nodes(
                            new_node):
                        old_xform.add_translation_node(translation_node)
                    old_xform.add_cascade_instance_node(
                        new_xform.get_cascade_instance_node(new_node))

                activity_log_detail["added"] = [new_field.label] if activity_log_detail.get("added") is None \
                    else activity_log_detail.get("added") + [new_field.label]
Exemplo n.º 4
0
 def _update_xform(self, new_fields, old_fields, parent_node, old_xform,
                   new_xform, activity_log_detail):
     for old_field in old_fields:
         node = get_node(parent_node, old_field.code)
         new_field = [
             new_field for new_field in new_fields
             if new_field.code == old_field.code
         ]
         if new_field:
             if hasattr(old_field,
                        'is_calculated') and old_field.is_calculated:
                 continue
             if isinstance(old_field, FieldSet):
                 self._update_xform(new_field[0].fields, old_field.fields,
                                    node, old_xform, new_xform,
                                    activity_log_detail)
             self.edit(node, old_field, new_field[0], old_xform, new_xform,
                       activity_log_detail)
         else:
             self.remove(parent_node, node, old_xform, old_field,
                         activity_log_detail)
Exemplo n.º 5
0
 def _get_field_node(self, root_node, field_code, type='select1'):
     node = get_node(root_node, field_code)
     if type == 'repeat':
         return node._children[1]
     return node