Exemple #1
0
    def __init__(self, dbm, name=None, label=None, form_code=None, fields=None, entity_type=None, type=None,
                 language="eng", state=attributes.ACTIVE_STATE):
        assert isinstance(dbm, DatabaseManager)
        assert name is None or is_not_empty(name)
        assert fields is None or is_sequence(fields)
        assert form_code is None or (is_string(form_code) and is_not_empty(form_code))
        assert type is None or is_not_empty(type)
        assert entity_type is None or is_sequence(entity_type)

        DataObject.__init__(self, dbm)

        self._form_fields = []
        self.errors = []

        # Are we being constructed from scratch or existing doc?
        if name is None:
            return

        # Not made from existing doc, so build ourselves up
        self._form_fields = fields
        self.validate_fields()

        doc = FormModelDocument()
        doc.name = name
        doc.add_label(language, label)
        doc.form_code = form_code
        doc.entity_type = entity_type
        doc.type = type
        doc.state = state
        doc.active_languages = language
        DataObject._set_document(self, doc)
Exemple #2
0
    def __init__(self,
                 dbm,
                 name=None,
                 label=None,
                 form_code=None,
                 fields=None,
                 entity_type=None,
                 type=None,
                 language="eng",
                 state=attributes.ACTIVE_STATE):
        assert isinstance(dbm, DatabaseManager)
        assert name is None or is_not_empty(name)
        assert fields is None or is_sequence(fields)
        assert form_code is None or (is_string(form_code)
                                     and is_not_empty(form_code))
        assert type is None or is_not_empty(type)
        assert entity_type is None or is_sequence(entity_type)

        DataObject.__init__(self, dbm)

        self._form_fields = []
        self.errors = []

        # Are we being constructed from scratch or existing doc?
        if name is None:
            return

        # Not made from existing doc, so build ourselves up
        self._form_fields = fields
        self.validate_fields()

        doc = FormModelDocument()
        doc.name = name
        doc.add_label(language, label)
        doc.form_code = form_code
        doc.entity_type = entity_type
        doc.type = type
        doc.state = state
        doc.active_languages = language
        DataObject._set_document(self, doc)
 def test_should_create_a_questionnaire_from_dictionary(self):
     fields = [
             {
             "name": "What are you reporting on?",
             "defaultValue": "",
             "label": {
                 "eng": "Entity being reported on"
             },
             "entity_question_flag": True,
             "type": "text",
             "ddtype": self.default_ddtype.to_json(),
             "code": "eid",
             "length": {"min": 1, "max": 10},
             },
             {
             "range": {
                 "max": 10,
                 "min": 0
             },
             "label": {"eng": ""},
             "type": "integer",
             "ddtype": self.default_ddtype.to_json(),
             "name": "What is your age?",
             "code": "AGE"
         },
             {
             "choices": [
                     {
                     "text": {"eng": "Pune"}
                 },
                     {
                     "text": {"eng": "Bangalore"}
                 }
             ],
             "label": {"eng": ""},
             "type": "select",
             "ddtype": self.default_ddtype.to_json(),
             "name": "Where do you live?",
             "code": "PLC"
         }]
     document = FormModelDocument()
     document.json_fields = fields
     document.entity_type = ["Reporter"]
     document.document_type = "FormModel"
     document.form_code = "F1"
     document.name = "New Project"
     document.type = "survey"
     document.type = "survey"
     entityQ = TextField(name="What are you reporting on?", code="eid",
                         label="Entity being reported on", entity_question_flag=True,
                         length=TextConstraint(min=1, max=10), ddtype=self.default_ddtype)
     ageQ = IntegerField(name="What is your age?", code="AGE", label="",
                         range=NumericConstraint(min=0, max=10), ddtype=self.default_ddtype)
     placeQ = SelectField(name="Where do you live?", code="PLC", label="",
                          options=[{"text": {"eng": "Pune"}}, {"text": {"eng": "Bangalore"}}],
                          single_select_flag=False, ddtype=self.default_ddtype)
     questions = [entityQ, ageQ, placeQ]
     questionnaire = FormModel.new_from_doc(self.dbm, document)
     self.maxDiff = None
     self.assertListEqual(questionnaire.entity_type, ["Reporter"])
     self.assertEqual(questionnaire.name, "New Project")
     self.assertEqual(questionnaire.type, "survey")
     for i in range(len(questions)):
         self.assertEqual(questionnaire.fields[i]._to_json(), questions[i]._to_json())
 def test_should_create_a_questionnaire_from_dictionary(self):
     fields = [{
         "name": "What are you reporting on?",
         "defaultValue": "",
         "label": {
             "eng": "Entity being reported on"
         },
         "entity_question_flag": True,
         "type": "text",
         "ddtype": self.default_ddtype.to_json(),
         "code": "eid",
         "length": {
             "min": 1,
             "max": 10
         },
     }, {
         "range": {
             "max": 10,
             "min": 0
         },
         "label": {
             "eng": ""
         },
         "type": "integer",
         "ddtype": self.default_ddtype.to_json(),
         "name": "What is your age?",
         "code": "AGE"
     }, {
         "choices": [{
             "text": {
                 "eng": "Pune"
             }
         }, {
             "text": {
                 "eng": "Bangalore"
             }
         }],
         "label": {
             "eng": ""
         },
         "type":
         "select",
         "ddtype":
         self.default_ddtype.to_json(),
         "name":
         "Where do you live?",
         "code":
         "PLC"
     }]
     document = FormModelDocument()
     document.json_fields = fields
     document.entity_type = ["Reporter"]
     document.document_type = "FormModel"
     document.form_code = "F1"
     document.name = "New Project"
     document.type = "survey"
     document.type = "survey"
     entityQ = TextField(name="What are you reporting on?",
                         code="eid",
                         label="Entity being reported on",
                         entity_question_flag=True,
                         length=TextConstraint(min=1, max=10),
                         ddtype=self.default_ddtype)
     ageQ = IntegerField(name="What is your age?",
                         code="AGE",
                         label="",
                         range=NumericConstraint(min=0, max=10),
                         ddtype=self.default_ddtype)
     placeQ = SelectField(name="Where do you live?",
                          code="PLC",
                          label="",
                          options=[{
                              "text": {
                                  "eng": "Pune"
                              }
                          }, {
                              "text": {
                                  "eng": "Bangalore"
                              }
                          }],
                          single_select_flag=False,
                          ddtype=self.default_ddtype)
     questions = [entityQ, ageQ, placeQ]
     questionnaire = FormModel.new_from_doc(self.dbm, document)
     self.maxDiff = None
     self.assertListEqual(questionnaire.entity_type, ["Reporter"])
     self.assertEqual(questionnaire.name, "New Project")
     self.assertEqual(questionnaire.type, "survey")
     for i in range(len(questions)):
         self.assertEqual(questionnaire.fields[i]._to_json(),
                          questions[i]._to_json())