Example #1
0
def create_question_from(dictionary, dbm):
    """
     Given a dictionary that defines a question, this would create a field with all the validations that are
     defined on it.
    """
    type = dictionary.get("type")
    name = dictionary.get("name")
    code = dictionary.get("code")
    is_entity_question = dictionary.get("entity_question_flag")
    label_dict = dictionary.get("label")
    instruction = dictionary.get("instruction")
    label = None
    if label_dict is not None:
        label = label_dict.get(field_attributes.DEFAULT_LANGUAGE)
    ddtype = DataDictType.create_from_json(dictionary.get("ddtype"), dbm)
    if type == field_attributes.TEXT_FIELD:
        return _get_text_field(code, ddtype, dictionary, is_entity_question, label, name, instruction=instruction)
    elif type == field_attributes.INTEGER_FIELD:
        return _get_integer_field(code, ddtype, dictionary, label, name, instruction=instruction)
    elif type == field_attributes.DATE_FIELD:
        return _get_date_field(code, ddtype, dictionary, label, name, instruction=instruction)
    elif type == field_attributes.LOCATION_FIELD:
        return GeoCodeField(name=name, code=code, label=label, ddtype=ddtype, instruction=instruction)
    elif type == field_attributes.SELECT_FIELD or type == field_attributes.MULTISELECT_FIELD:
        return _get_select_field(code, ddtype, dictionary, label, name, type, instruction=instruction)
    elif type == field_attributes.LIST_FIELD:
        return _get_list_field(name, code, label, ddtype, instruction=instruction)
    return None
Example #2
0
def create_question_from(dictionary, dbm):
    """
     Given a dictionary that defines a question, this would create a field with all the validations that are
     defined on it.
    """
    type = dictionary.get("type")
    name = dictionary.get("name")
    code = dictionary.get("code")
    is_entity_question = dictionary.get("entity_question_flag")
    label_dict = dictionary.get("label")
    instruction = dictionary.get("instruction")
    label = None
    if label_dict is not None:
        label = label_dict.get(field_attributes.DEFAULT_LANGUAGE)
    ddtype = DataDictType.create_from_json(dictionary.get("ddtype"), dbm)
    if type == field_attributes.TEXT_FIELD:
        return _get_text_field(code,
                               ddtype,
                               dictionary,
                               is_entity_question,
                               label,
                               name,
                               instruction=instruction)
    elif type == field_attributes.INTEGER_FIELD:
        return _get_integer_field(code,
                                  ddtype,
                                  dictionary,
                                  label,
                                  name,
                                  instruction=instruction)
    elif type == field_attributes.DATE_FIELD:
        return _get_date_field(code,
                               ddtype,
                               dictionary,
                               label,
                               name,
                               instruction=instruction)
    elif type == field_attributes.LOCATION_FIELD:
        return GeoCodeField(name=name,
                            code=code,
                            label=label,
                            ddtype=ddtype,
                            instruction=instruction)
    elif type == field_attributes.SELECT_FIELD or type == field_attributes.MULTISELECT_FIELD:
        return _get_select_field(code,
                                 ddtype,
                                 dictionary,
                                 label,
                                 name,
                                 type,
                                 instruction=instruction)
    elif type == field_attributes.LIST_FIELD:
        return _get_list_field(name,
                               code,
                               label,
                               ddtype,
                               instruction=instruction)
    return None
    def test_should_create_from_json(self):
        _id = "1"
        first_name = u'First name'
        primitive_type = u'string'
        slug = u'first_Name'
        json = {'_id': _id,
                'constraints': {},
                'created': '2010-01-01T10:11:12+00:00',
                'description': None,
                'document_type': u'DataDict',
                'modified': None,
                'name': first_name,
                'primitive_type': primitive_type,
                'slug': slug,
                'tags': [],
                'void': False}

        ddtype = DataDictType.create_from_json(json, self.dbm)
        self.assertEqual(_id, ddtype.id)
        self.assertEqual(first_name, ddtype.name)
        self.assertEqual(primitive_type, ddtype.primitive_type)
        self.assertEqual(slug, ddtype.slug)
        self.assertEqual({}, ddtype.constraints)
        self.assertEqual([], ddtype.tags)