def form_fields(self): context = self.context home_path_field = schemaish.String( validator=karlvalidators.PathExists(context), description=('The first page to show after logging in. ' 'Leave blank to show a community or the ' 'community list.')) password_field = schemaish.String(validator=( validator.All(karlvalidators.PasswordLength(min_pw_length()), validator.Required()))) fields = [('login', login_field), ('groups', groups_field), ('home_path', home_path_field), ('password', password_field)] fields += super(AddUserFormController, self).form_fields() # Get rid of unique email address validation, because we need to do # that in the controller so we can manage whether or not email belongs # to deactivated user. email_field = schemaish.String( validator=validator.All(validator.Required(), validator.Email())) for i in xrange(len(fields)): if fields[i][0] == 'email': fields[i] = ('email', email_field) break return fields
def form_fields(self): min_pw_length = int(get_setting(self.context, 'min_pw_length', 6)) pwlen = validator.Length(min_pw_length) username = karlvalidators.RegularExpression( r'^[\w-]+$', 'Username must contain only letters, numbers, and dashes') fields = [ ('username', schemaish.String( validator=validator.All(validator.Required(), username))), ('password', schemaish.String( validator=validator.All(validator.Required(), pwlen))), ('password_confirm', schemaish.String( validator=validator.All(validator.Required(), pwlen))), ('firstname', schemaish.String(validator=validator.Required())), ('lastname', schemaish.String(validator=validator.Required())) ] member_fields = get_setting(self.context, 'member_fields') for field_name in member_fields: if field_name in self.fields: fields.append((field_name, self.fields[field_name])) r = queryMultiAdapter((self.context, self.request), IInvitationBoilerplate) if r is None: r = DefaultInvitationBoilerplate(self.context) if r.terms_and_conditions: fields.append(('terms_and_conditions', schemaish.Boolean(validator=validator.Equal(True)))) if r.privacy_statement: fields.append(('accept_privacy_policy', schemaish.Boolean(validator=validator.Equal(True)))) return fields
def test_inside_any_on_own(self): """ Check inside an Any, on own. """ assert util.validation_includes(validator.Any(validator.Required()), validator.Required) assert not util.validation_includes( validator.Any(validator.Required()), validator.Email)
def test_inside_all(self): """ Check when inside an All. """ assert util.validation_includes(validator.All(validator.Required()), validator.Required) assert not util.validation_includes( validator.All(validator.Required()), validator.Email)
class InvoiceEntrySchema(schemaish.Structure): id = schemaish.Integer() description = schemaish.String(validator=validator.Required()) currency_code = schemaish.String(validator=validator.Required()) vat = schemaish.Integer(validator=validator.Required()) unit_price = schemaish.Decimal(validator=validator.Required()) units = schemaish.Decimal( validator=validator.All(validator.Required(), validator.Range( min=0.1)))
def test_immediate(self): """ Check that the hunt succeeds when the searched for validator is exactly the same type. """ assert util.validation_includes(validator.Required(), validator.Required) assert not util.validation_includes(validator.Required(), validator.Email)
def test_inside_any_only_type(self): """ Check inside an Any, amongst validators of same type. """ assert util.validation_includes( validator.Any(validator.Required(), validator.Required()), validator.Required) assert not util.validation_includes( validator.Any(validator.Required(), validator.Required()), validator.Email)
def test_inside_any_with_others(self): """ Check when inside an Any with other types. """ assert not util.validation_includes( validator.Any(validator.Required(), validator.Email()), validator.Required) assert not util.validation_includes( validator.Any(validator.Required(), validator.Email()), validator.Email)
def form_fields(self): min_pw_length = get_setting(None, 'min_pw_length') login_field = schemaish.String(validator=validator.Required()) password_field = schemaish.String(validator=validator.All( validator.Required(), karlvalidators.PasswordLength(min_pw_length)), title='New Password') fields = [ ('login', login_field), ('password', password_field), ] return fields
class SignupSchema(schemaish.Structure): login = schemaish.String(validator=validator.All( validator.Required(), validator.Length(min=5, max=32), validator.PlainText(), form.AvailableLogin())) password = schemaish.String(validator=validator.All( validator.Required(), validator.Length(min=5, max=255))) email = schemaish.String( validator=validator.All(validator.Required(), validator.Length( max=255), validator.Email())) firstname = schemaish.String(validator=validator.Length(max=255)) surname = schemaish.String(validator=validator.Length(max=255)) terms = schemaish.Boolean(validator=form.MustAgree())
class InvoiceSchema(schemaish.Structure): customer_contact_id = schemaish.Integer(validator=validator.Required()) project_description = schemaish.String(validator=validator.Required()) date = schemaish.Date(validator=validator.Required()) invoice_number = schemaish.Integer() recurring_term = schemaish.Integer() recurring_stop = schemaish.Date() payment_term = schemaish.Integer(validator=validator.Required()) currency = schemaish.String(validator=validator.Required()) tax = schemaish.Float() payment_date = schemaish.Date() item_list = schemaish.Sequence(invoice_item_schema, validator=validatish.Length(min=1))
def test_with_functions(self): """ Check hunting doesn't explode with plain old functions. """ def f(v): pass assert not util.validation_includes(f, validator.Required) assert not util.validation_includes(validator.All(f, f), validator.Required) assert util.validation_includes(validator.All(f, validator.Required()), validator.Required) assert not util.validation_includes( validator.Any(f, validator.Required()), validator.Required)
def form_fields(self): required = validator.Required() min_pw_length = int(get_setting(self.context, 'min_pw_length', 6)) pwlen = validator.Length(min_pw_length) username = karlvalidators.RegularExpression( r'^[\w-]+$', 'Username must contain only letters, numbers, and dashes') fields = [ ('username', schemaish.String(validator=validator.All(required, username))), ('password', schemaish.String(validator=validator.All(required, pwlen))), ('password_confirm', schemaish.String(validator=validator.All(required, pwlen))), ('firstname', schemaish.String(validator=required)), ('lastname', schemaish.String(validator=required)), ('phone', schemaish.String()), ('extension', schemaish.String()), ('organization', schemaish.String()), ( 'country', schemaish.String(validator=validator.All( validator.OneOf(countries.as_dict.keys()), validator.Required()), ), ), ('location', schemaish.String()), ('department', schemaish.String()), ('position', schemaish.String()), ('websites', schemaish.Sequence(schemaish.String(validator=validator.URL()))), ('languages', schemaish.String()), ('biography', schemaish.String()), ('photo', schemaish.File()), ('date_format', schemaish.String( validator=validator.OneOf(cultures.as_dict.keys()))), ] r = queryMultiAdapter((self.context, self.request), IInvitationBoilerplate) if r is None: r = DefaultInvitationBoilerplate(self.context) if r.terms_and_conditions: fields.append(('terms_and_conditions', schemaish.Boolean(validator=validator.Equal(True)))) if r.privacy_statement: fields.append(('accept_privacy_policy', schemaish.Boolean(validator=validator.Equal(True)))) return fields
class Test_RequiredAndIntegerOrString(unittest.TestCase): type = 'RequiredAndIntegerOrString' fn = validator.All(validator.Required(), validator.Any(validator.String(), validator.Integer())) def test_validate_pass(self): self.section = 'pass' values = [ '1', 1, 1L, ] check_pass('class', self, self.fn, values) def test_validate_fail(self): self.section = 'fail' values = [ 0.5, datetime.now(), None, ] check_fail('class', self, self.fn, values) def test_messages(self): try: self.fn(datetime.now()) except error.Invalid, e: self.assertEquals(len(e.errors), 2) assert 'string' in ''.join(e.errors) assert 'integer' in ''.join(e.errors)
class TestAll_StringRequired(unittest.TestCase): type = 'All' fn = validator.All(validator.String(), validator.Required()) def test_validate_pass(self): self.section = 'pass' values = ['1', '4', u'3'] check_pass('class', self, self.fn, values) def test_validate_fail(self): self.section = 'fail' values = [ '', u'', 0, ] check_fail('class', self, self.fn, values) def test_messages(self): try: self.fn(None) except error.Invalid, e: self.assertEquals(len(e.errors), 1) self.assertEquals('is required', e.errors[0]) try: self.fn(1) except error.Invalid, e: self.assertEquals(len(e.errors), 1) self.assertEquals('must be a string', e.errors[0])
class TestRequired(unittest.TestCase): type = 'Required' fn = staticmethod(validate.is_required) class_fn = validator.Required() def test_validate_pass(self): self.section = 'pass' values = [ 0.0, ' ', [''], '0', [None], 'None', ] check_pass('function', self, self.fn, values) check_pass('class', self, self.class_fn, values) def test_validate_fail(self): self.section = 'fail' values = [ '', [], u'', ] check_fail('function', self, self.fn, values) check_fail('class', self, self.class_fn, values)
def form_fields(self): email = schemaish.String(validator=validator.Email()) return [ ('email_addresses', schemaish.Sequence(email, validator=validator.Required())), ('text', schemaish.String()), ]
def form_fields(self): return [ ('users', schemaish.Sequence( schemaish.String(validator=validator.Required()))), ('text', schemaish.String()), ]
def test_complex_error_all(self): schema = schemaish.Structure([ ("one", schemaish.Integer(validator=v.All( v.Required(), v.Integer(), v.Range(min=18), v.Range(min=20), ))), ]) f = formish.Form(schema, name="form", add_default_action=False) f.add_action('submit', "Submit Me") r = webob.Request.blank('http://localhost/', environ={'REQUEST_METHOD': 'POST'}) r.POST['__formish_form__'] = 'form' r.POST['one'] = '9' try: f.validate(r) except fv.FormError: assert str( f.errors['one'] ) == 'must be greater than or equal to 18; must be greater than or equal to 20' assert str(f['one'].field.errors.exceptions[0] ) == 'must be greater than or equal to 18'
class InvoiceItemSchema(schemaish.Structure): item_id = schemaish.Integer() service_title = schemaish.String(validator=validator.Required()) service_description = schemaish.String(validator=validator.Required()) amount = schemaish.Float(description="Enter the amout") hours = schemaish.Float( description= "Or hours (will be multiplied by your standard or the customers special rate)" ) days = schemaish.Float( description= "Or days (will be multiplied by your standard or the customers special rate)" ) # Additional schema wide validator. validator = ItemAmountValidator()
def form_fields(self): email_field = schemaish.String( validator=validator.All(validator.Required(), validator.Email(), karlvalidators.UniqueEmail(self.context))) fields = [('firstname', firstname_field), ('lastname', lastname_field), ('email', email_field), ('photo', photo_field)] for field_name in get_setting(self.context, 'member_fields'): if field_name in self.fields: fields.append((field_name, self.fields[field_name])) return fields
def form_fields(self): users = find_users(self.context) userid = self.context.__name__ user = users.get_by_id(userid) old_password_field = schemaish.String( title="Old Password", validator=validator.All( validator.Required(), karlvalidators.CorrectUserPassword(user), )) new_password_field = schemaish.String( title="New Password", validator=validator.All( karlvalidators.PasswordLength(min_pw_length()), validator.Required())) fields = [ ('old_password', old_password_field), ('password', new_password_field), ] return fields
def form_fields(self): fields = shared_fields() title_field = schemaish.String( validator=validator.All(validator.Length( max=100), validator.Required())) fields.insert(0, ('title', title_field)) security_states = self._get_security_states() if security_states: fields.insert(4, ('security_state', security_field)) fields.append(('default_tool', default_tool_field)) return fields
def form_fields(self): title_field = schemaish.String( validator=validator.All( validator.Length(max=100), validator.Required(), )) fields = [('title', title_field), ('tags', tags_field), ('description', description_field), ] return fields
def form_fields(self): title_field = schemaish.String(validator=validator.All( validator.Length(max=100), validator.Required(), )) fields = [ ('title', title_field), ('tags', tags_field), ('text', text_field), ('attachments', attachments_field), ] return fields
def test_form_error_css(self): schema = schemaish.Structure([ ('a', schemaish.String(validator=v.Required())) ]) form = formish.Form(schema) soup = BeautifulSoup(form()) self.assertTrue('error' not in soup.find('form').get('class').split()) try: form.validate(webob.Request.blank('/', POST={})) except formish.FormError: pass soup = BeautifulSoup(form()) self.assertTrue('error' in soup.find('form').get('class').split())
class ProfileSchema(schemaish.Structure): firstname = schemaish.String( validator=validator.All(validator.Required(), validator.Length( max=64))) surname = schemaish.String( validator=validator.All(validator.Required(), validator.Length( max=64))) email = schemaish.String( validator=validator.All(validator.Required(), validator.Length( max=256), validator.Email())) password = schemaish.String(validator=validator.All( validator.Length(min=4, max=256), validator.Length(max=256))) company = schemaish.String( validator=validator.All(validator.Required(), validator.Length( max=64))) ein = schemaish.String() logo = schemaish.String(validator=form.Image()) address = schemaish.String() postal_code = schemaish.String(validator=validator.Length(max=16)) city = schemaish.String(validator=validator.Length(max=64)) country = schemaish.String(validator=validator.Length(max=3))
def test_complex_form(self): one = schemaish.Structure([ ("a", schemaish.String(validator=v.All(v.Email(), v.Required()))), ("b", schemaish.String()), ("c", schemaish.Sequence(schemaish.Integer())) ]) two = schemaish.Structure([("a", schemaish.String()), ("b", schemaish.Date()),\ ('c', schemaish.Sequence(schemaish.String())), ("d", schemaish.String()), \ ("e", schemaish.Integer(validator=v.Required())), ("f", schemaish.String(validator=v.Required())) ]) schema = schemaish.Structure([("one", one), ("two", two)]) f = formish.Form(schema, name="form", add_default_action=False) f['one.b'].widget = formish.TextArea() f['two.a'].widget = formish.SelectChoice( [('opt1', "Options 1"), ('opt2', "Option 2")], none_option=('-select option-', None)) f['two.b'].widget = formish.DateParts() f['two.c'].widget = formish.CheckboxMultiChoice([('opt1', "Options 1"), ('opt2', "Option 2")]) f['two.d'].widget = formish.RadioChoice([('opt1', "Options 1"), ('opt2', "Option 2")]) f['two.f'].widget = formish.CheckedPassword() f.add_action('submit', "Submit Me") f.defaults = { 'one': { 'a': 'ooteenee', 'c': ['3', '4', '5'] }, 'two': { 'a': 'opt1', 'b': date(1966, 1, 3), 'c': ['opt2'], 'd': 'opt2' } } f()
def form_fields(self): fields = shared_fields() title_field = schemaish.String(validator=validator.All( validator.Length(max=100), validator.Required(), karlvalidators.FolderNameAvailable(self.context), )) fields.insert(0, ('title', title_field)) security_states = self._get_security_states() if security_states: fields.insert(4, ('security_state', security_field)) fields.append(('default_tool', default_tool_field)) fields.append(('sendalert_default', sendalert_default_field)) return fields
def form_fields(self): fields = [] title_field = schemaish.String(validator=validator.All( validator.Length(max=100), validator.Required(), )) fields.append(('title', title_field)) fields.append(('tags', tags_field)) fields.append(('text', text_field)) fields.append(('attachments', attachments_field)) security_states = self._get_security_states() if security_states: fields.append(('security_state', security_field)) return fields