Exemplo n.º 1
0
    def test_should_convert_field_with_constraints_to_json(self):
        constraints = [
            TextLengthConstraint(min=10, max=12),
            RegexConstraint("^[A-Za-z0-9]+$")
        ]
        field = TextField(name="test",
                          code='MC',
                          label='question',
                          constraints=constraints)
        expected_json = {
            "code": "MC",
            "name": "test",
            "defaultValue": "",
            "instruction": None,
            "label": "question",
            "type": "text",
            'parent_field_code': None,
            "length": {
                'max': 12,
                'min': 10
            },
            "regex": "^[A-Za-z0-9]+$",
            "required": True
        }

        self.assertEqual(expected_json, field_to_json(field))
Exemplo n.º 2
0
 def test_should_convert_field_with_apostrophe_to_json(self):
     field = TextField(name="Test's", code="AA", label="test")
     expected_json = {
         "code": "AA",
         "name": "Test\'s",
         "defaultValue": "",
         "instruction": None,
         'parent_field_code': None,
         "label": "test",
         "type": "text",
         "required": True
     }
     self.assertEqual(expected_json, field_to_json(field))
Exemplo n.º 3
0
 def test_should_convert_field_with_apostrophe_to_json(self):
     field = TextField(name="Test's", code="AA", label="test")
     expected_json = {
         "code": "AA",
         "name": "Test\'s",
         "defaultValue": "",
         "instruction": None,
         "parent_field_code": None,
         "label": "test",
         "type": "text",
         "required": True,
         "appearance": None,
         "constraint_message": None,
         "default": None,
         "hint": None,
         "xform_constraint": None,
         "relevant": None
     }
     self.assertEqual(expected_json, field_to_json(field))
def generate_template_data():
    templates = []
    #database which has template specific projects.
    test_dbm = get_db_manager('hni_templates_aoi959205')
    projects = get_all_projects(test_dbm)
    for doc in projects:
        form_model = FormModel.get(test_dbm, doc.id)
        json_obj = {}
        json_obj.update({"name": form_model.name})
        json_obj.update({"language": form_model.activeLanguages[0]})
        json_obj.update({"category": _get_category(form_model)})
        json_obj.update({"form_code": form_model.form_code})
        fields = _remove_entity_field(form_model)
        json_obj.update({"json_fields": [field_to_json(f) for f in fields]})
        json_obj.update({"validators": [validator.to_json() for validator in form_model.validators]})
        templates.append(json_obj)

    file = os.path.dirname(__file__) + '/../datawinners/questionnaire/template_data.json'
    with codecs.open(file, 'w', encoding='utf-8') as outfile:
        json.dump(templates, outfile, ensure_ascii=False, indent=4)