Esempio n. 1
0
 def test_telephone_number_should_not_trim_the_leading_zeroes(self):
     field = TelephoneNumberField(name="test",
                                  code='MC',
                                  label='question',
                                  constraints=[],
                                  instruction='')
     self.assertEqual(u'020', field.validate(u'020'))
Esempio n. 2
0
 def test_phone_number_field(self):
     field = TelephoneNumberField(
         'phone',
         'phone_code',
         'phone',
         constraints=[
             TextLengthConstraint(min=10, max=12),
             RegexConstraint(reg='^[0-9]+$')
         ],
         instruction=
         "Answer must be country code plus telephone number. Example: 261333745269"
     )
     phone_field = FormField().create(field)
     self.assertTrue(isinstance(phone_field, PhoneNumberField))
     self.assertEqual(len(phone_field.validators), 3)
     self.assertEqual(phone_field.widget.attrs['watermark'],
                      'Between 10 -- 12 characters')
     validator_types = []
     for validator in phone_field.validators:
         validator_types.append(type(validator))
     self.assertTrue(MinLengthValidator in validator_types)
     self.assertTrue(MaxLengthValidator in validator_types)
     self.assertTrue(RegexValidator in validator_types)
     self.assertEqual(
         field.instruction,
         "Answer must be country code plus telephone number. Example: 261333745269"
     )
Esempio n. 3
0
 def test_telephone_number_field_should_return_expected_json(self):
     mobile_number_length = TextLengthConstraint(max=15)
     mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
     field = TelephoneNumberField(
         name="test",
         code='MC',
         label='question',
         constraints=[mobile_number_length, mobile_number_pattern],
         instruction='')
     expected_json = {
         "label": "question",
         "name": "test",
         "code": "MC",
         'parent_field_code': None,
         "type": "telephone_number",
         "instruction": "",
         "constraints": [('length', {
             'max': 15
         }), ('regex', '^[0-9]+$')],
         "defaultValue": '',
         "required": True,
         "appearance": None,
         "constraint_message": None,
         "default": None,
         "hint": None,
         "xform_constraint": None,
         "relevant": None
     }
     self.assertEqual(expected_json, field._to_json())
Esempio n. 4
0
 def _create_telephone_number_question(self, post_dict, code):
     return TelephoneNumberField(
         name=self._get_name(post_dict),
         code=code,
         label=post_dict["title"],
         instruction=post_dict.get("instruction"),
         constraints=(self._create_constraints_for_mobile_number()),
         required=post_dict.get("required"))
Esempio n. 5
0
 def _create_telephone_number_question(self, post_dict, code):
     return TelephoneNumberField(name=self._get_name(post_dict, code), code=code,
                                 label=post_dict["title"],
                                 instruction=post_dict.get("instruction"), constraints=(
             self._create_constraints_for_mobile_number()), required=post_dict.get("required"),
                                 parent_field_code=post_dict.get('parent_field_code'),
                                 hint=post_dict.get('hint'), constraint_message=post_dict.get('constraint_message'),
                                 appearance=post_dict.get('appearance'),
                                 default=post_dict.get('default'),
                                 xform_constraint=post_dict.get('xform_constraint'),
                                 relevant=post_dict.get('relevant'))
Esempio n. 6
0
    def test_should_convert_text_in_epsilon_format_to_expanded_text(self):
        mobile_number_length = TextLengthConstraint(max=15)
        mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
        field = TelephoneNumberField(
            name="test",
            code='MC',
            label='question',
            constraints=[mobile_number_length, mobile_number_pattern],
            instruction='')

        self.assertEqual(u'266123321435', field._clean(u'2.66123321435e+11'))
Esempio n. 7
0
 def test_should_raise_regex_mismatch_exception_if_invalid_phone_number(
         self):
     mobile_number_length = TextLengthConstraint(max=15)
     mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
     field = TelephoneNumberField(
         name="test",
         code='MC',
         label='question',
         constraints=[mobile_number_length, mobile_number_pattern],
         instruction='')
     with self.assertRaises(RegexMismatchException):
         field.validate(u'020321dsa')
Esempio n. 8
0
 def _get_telephone_number_field(self):
     form_model = self._get_form_model()
     phone_number_field = TelephoneNumberField(
         name=self.field_name,
         code='m',
         label=self.field_name,
         ddtype=Mock(spec=DataDictType),
         constraints=[
             TextLengthConstraint(max=15),
             RegexConstraint(reg='^[0-9]+$')
         ])
     return phone_number_field
Esempio n. 9
0
    def test_telephone_number_should_clean_before_validate(self):
        mobile_number_length = TextLengthConstraint(max=15)
        mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
        field = TelephoneNumberField(
            name="test",
            code='MC',
            label='question',
            constraints=[mobile_number_length, mobile_number_pattern],
            instruction='')

        self.assertEqual(u'266123321435', field.validate(u'2.66123321435e+11'))
        self.assertEqual(u'266123321435', field.validate(u'266-123321435'))
        self.assertEqual(u'266123321435', field.validate(u'266123321435.0'))
Esempio n. 10
0
    def test_telephone_number_should_clean_value_to_remove_only_hyphen_from_the_given_value(
            self):
        mobile_number_length = TextLengthConstraint(max=15)
        mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
        field = TelephoneNumberField(
            name="test",
            code='MC',
            label='question',
            constraints=[mobile_number_length, mobile_number_pattern],
            instruction='')

        self.assertEqual('1234321122', field._clean('123-4321122'))
        self.assertEqual('123dsasa4321122', field._clean('123dsasa4321122'))
Esempio n. 11
0
def _create_registration_form(manager, entity_name=None, form_code=None, entity_type=None):
    code_generator = question_code_generator()
    location_type = _get_or_create_data_dict(manager, name='Location Type', slug='location', primitive_type='string')
    geo_code_type = _get_or_create_data_dict(manager, name='GeoCode Type', slug='geo_code', primitive_type='geocode')
    name_type = _get_or_create_data_dict(manager, name='Name', slug='name', primitive_type='string')
    mobile_number_type = _get_or_create_data_dict(manager, name='Mobile Number Type', slug='mobile_number',
        primitive_type='string')

    question1 = TextField(name=FIRSTNAME_FIELD, code=code_generator.next(),
        label=_("What is the %(entity_type)s's first name?") % {'entity_type': entity_name},
        defaultValue="some default value",  ddtype=name_type,
        instruction=_("Enter a %(entity_type)s first name") % {'entity_type': entity_name})

    question2 = TextField(name=NAME_FIELD, code=code_generator.next(),
        label=_("What is the %(entity_type)s's last name?") % {'entity_type': entity_name},
        defaultValue="some default value", ddtype=name_type,
        instruction=_("Enter a %(entity_type)s last name") % {'entity_type': entity_name})
    question3 = HierarchyField(name=LOCATION_TYPE_FIELD_NAME, code=code_generator.next(),
        label=_("What is the %(entity_type)s's location?") % {'entity_type': entity_name},
        ddtype=location_type, instruction=unicode(_("Enter a region, district, or commune")))
    question4 = GeoCodeField(name=GEO_CODE_FIELD_NAME, code=code_generator.next(),
        label=_("What is the %(entity_type)s's GPS co-ordinates?") % {'entity_type': entity_name},
        ddtype=geo_code_type,
        instruction=unicode(_("Answer must be GPS co-ordinates in the following format: xx.xxxx,yy.yyyy Example: -18.1324,27.6547 ")))
    question5 = TelephoneNumberField(name=MOBILE_NUMBER_FIELD, code=code_generator.next(),
        label=_("What is the %(entity_type)s's mobile telephone number?") % {'entity_type': entity_name},
        defaultValue="some default value", ddtype=mobile_number_type,
        instruction=_(
            "Enter the (%(entity_type)s)'s number with the country code and telephone number. Example: 261333745269") % {
            'entity_type': entity_name}, constraints=(
            _create_constraints_for_mobile_number()))
    question6 = TextField(name=SHORT_CODE_FIELD, code=code_generator.next(),
        label=_("What is the %(entity_type)s's Unique ID Number?") % {'entity_type': entity_name},
        defaultValue="some default value", ddtype=name_type,
        instruction=unicode(_("Enter an id, or allow us to generate it")),
        entity_question_flag=True,
        constraints=[TextLengthConstraint(max=20)], required=False)
    questions = [question1, question2, question3, question4, question5, question6]

    form_model = FormModel(manager, name=entity_name, form_code=form_code, fields=questions, is_registration_model=True,
        entity_type=entity_type)
    return form_model
Esempio n. 12
0
 def test_phone_number_field(self):
     field = TelephoneNumberField('phone',
                                  'phone_code',
                                  'phone',
                                  self.ddtype,
                                  constraints=[
                                      TextLengthConstraint(min=10, max=12),
                                      RegexConstraint(reg='^[0-9]+$')
                                  ])
     phone_field = FormField().create(field)
     self.assertTrue(isinstance(phone_field, PhoneNumberField))
     self.assertEqual(len(phone_field.validators), 3)
     self.assertEqual(phone_field.widget.attrs['watermark'],
                      'Between 10 -- 12 characters')
     validator_types = []
     for validator in phone_field.validators:
         validator_types.append(type(validator))
     self.assertTrue(MinLengthValidator in validator_types)
     self.assertTrue(MaxLengthValidator in validator_types)
     self.assertTrue(RegexValidator in validator_types)
Esempio n. 13
0
def _create_registration_form(manager,
                              entity_name=None,
                              form_code=None,
                              entity_type=None):
    code_generator = question_code_generator()

    question1 = TextField(
        name=FIRSTNAME_FIELD,
        code=code_generator.next(),
        label=_("What is the %(entity_type)s's first name?") %
        {'entity_type': entity_name},
        defaultValue="some default value",
        instruction=_("Enter a %(entity_type)s first name") %
        {'entity_type': entity_name})

    question2 = TextField(name=NAME_FIELD,
                          code=code_generator.next(),
                          label=_("What is the %(entity_type)s's last name?") %
                          {'entity_type': entity_name},
                          defaultValue="some default value",
                          instruction=_("Enter a %(entity_type)s last name") %
                          {'entity_type': entity_name})
    question3 = HierarchyField(
        name=LOCATION_TYPE_FIELD_NAME,
        code=code_generator.next(),
        label=_("What is the %(entity_type)s's location?") %
        {'entity_type': entity_name},
        instruction=unicode(_("Enter a region, district, or commune")))
    question4 = GeoCodeField(
        name=GEO_CODE_FIELD_NAME,
        code=code_generator.next(),
        label=_("What is the %(entity_type)s's GPS co-ordinates?") %
        {'entity_type': entity_name},
        instruction=unicode(
            _("Answer must be GPS coordinates in the following format (latitude,longitude). Example: -18.1324,27.6547"
              )))
    question5 = TelephoneNumberField(
        name=MOBILE_NUMBER_FIELD,
        code=code_generator.next(),
        label=_("What is the %(entity_type)s's mobile telephone number?") %
        {'entity_type': entity_name},
        defaultValue="some default value",
        instruction=
        _("Enter the (%(entity_type)s)'s number with the country code and telephone number. Example: 261333745269"
          ) % {'entity_type': entity_name},
        constraints=(_create_constraints_for_mobile_number()))
    question6 = ShortCodeField(
        name=SHORT_CODE_FIELD,
        code=code_generator.next(),
        label=_("What is the %(entity_type)s's Unique ID Number?") %
        {'entity_type': entity_name},
        defaultValue="some default value",
        instruction=unicode(_("Enter an id, or allow us to generate it")),
        required=False)
    questions = [
        question1, question2, question3, question4, question5, question6
    ]

    form_model = EntityFormModel(manager,
                                 name=entity_name,
                                 form_code=form_code,
                                 fields=questions,
                                 is_registration_model=True,
                                 entity_type=entity_type)
    return form_model
Esempio n. 14
0
def construct_global_registration_form(manager):
    question1 = HierarchyField(name=ENTITY_TYPE_FIELD_NAME,
                               code=ENTITY_TYPE_FIELD_CODE,
                               label="What is associated subject type?",
                               instruction="Enter a type for the subject")

    question2 = TextField(name=NAME_FIELD,
                          code=NAME_FIELD_CODE,
                          label="What is the subject's name?",
                          defaultValue="some default value",
                          instruction="Enter a subject name",
                          constraints=[TextLengthConstraint(max=80)],
                          required=False)
    question3 = ShortCodeField(
        name=SHORT_CODE_FIELD,
        code=SHORT_CODE,
        label="What is the subject's Unique ID Number",
        defaultValue="some default value",
        instruction="Enter a id, or allow us to generate it",
        constraints=[
            TextLengthConstraint(max=12),
            ShortCodeRegexConstraint(reg='^[a-zA-Z0-9]+$')
        ],
        required=False)
    question4 = HierarchyField(
        name=LOCATION_TYPE_FIELD_NAME,
        code=LOCATION_TYPE_FIELD_CODE,
        label="What is the subject's location?",
        instruction="Enter a region, district, or commune",
        required=False)
    question5 = GeoCodeField(name=GEO_CODE_FIELD_NAME,
                             code=GEO_CODE,
                             label="What is the subject's GPS co-ordinates?",
                             instruction="Enter lat and long. Eg 20.6, 47.3",
                             required=False)
    question6 = TelephoneNumberField(
        name=MOBILE_NUMBER_FIELD,
        code=MOBILE_NUMBER_FIELD_CODE,
        label="What is the mobile number associated with the subject?",
        defaultValue="some default value",
        instruction="Enter the subject's number",
        constraints=(_create_constraints_for_mobile_number()),
        required=True)
    question7 = TextField(name=EMAIL_FIELD,
                          code=EMAIL_FIELD,
                          label="What is the subject's email",
                          defaultValue="",
                          instruction="Enter email id",
                          constraints=[TextLengthConstraint(max=50)],
                          required=False)
    question8 = BooleanField(name=IS_DATASENDER_FIELD_CODE,
                             code=IS_DATASENDER_FIELD_CODE,
                             label="Am I a data sender",
                             defaultValue=True,
                             required=False)
    form_model = EntityFormModel(
        manager,
        name=GLOBAL_REGISTRATION_FORM_CODE,
        form_code=REGISTRATION_FORM_CODE,
        fields=[
            question1, question2, question3, question4, question5, question6,
            question7, question8
        ],
        is_registration_model=True,
        entity_type=["registration"],
        validators=[
            MandatoryValidator(),
            MobileNumberValidationsForReporterRegistrationValidator()
        ])
    return form_model