def __init__(self, name, code, label, constraints=None, defaultValue=None, instruction=None, required=False, parent_field_code=None): if not constraints: constraints = [ TextLengthConstraint(max=20), ShortCodeRegexConstraint("^[a-zA-Z0-9]+$") ] assert isinstance(constraints, list) TextField.__init__(self, name=name, code=code, label=label, instruction=instruction, constraints=constraints, defaultValue=defaultValue, required=required, parent_field_code=parent_field_code) self._dict['type'] = field_attributes.SHORT_CODE_FIELD
def add_regex_constraint_to_short_code(form_model, logger): form_model.entity_question.set_constraints([ TextLengthConstraint(max=20)._to_json(), ShortCodeRegexConstraint("^[a-zA-Z0-9]+$")._to_json() ]) form_model.save() logger.info("migrated form code: %s" % form_model.form_code)
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), ShortCodeRegexConstraint("^[a-zA-Z0-9]+$") ], 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
def test_should_return_lower_case_value_if_short_code_is_upper_case(self): constraint = ShortCodeRegexConstraint("^[a-zA-Z0-9]+$") self.assertEquals('shortcode', constraint.validate('SHORTCODE'))
def test_should_return_valid_short_code_regex_json(self): pattern = "^[A-Za-z0-9]+$" constraint = ShortCodeRegexConstraint(reg=pattern) self.assertEqual(("short_code", pattern), constraint._to_json())
def test_should_validate_values_within_short_code_regex(self): constraint = ShortCodeRegexConstraint("^[a-zA-Z0-9]+$") with self.assertRaises(ShortCodeRegexMismatchException): constraint.validate('shortCode#')
def test_should_validate_values_within_short_code_regex(self): constraint = ShortCodeRegexConstraint("^[a-zA-Z0-9]+$") self.assertEquals('shortCode', constraint.validate('shortCode1'))
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