Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from __init__ import ControllerWrap, SetupCacheGlobal, TestWSGIController
import formencode
from formencode.htmlfill import html_quote
def custom_error_formatter(error):
return '<p><span class="pylons-error">%s</span></p>\n' % html_quote(error)
class NetworkForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
new_network = formencode.validators.URL(not_empty=True)
class HelloForm(formencode.Schema):
hello = formencode.ForEach(formencode.validators.Int())
class ValidatingController(WSGIController):
def new_network(self):
return """
<form method="POST" action="/dhcp/new_form">
<table>
<tbody><tr>
<th>Network</th>
<td>
<input value="" type="text" name="new_network" id="new_network">
</td>
</tr>
</tbody></table>
<input value="Save changes" type="submit" name="commit">
</form>
def validate_python(self, value, state):
raise validators.Invalid('ERROR: Description', value, state)
@validate(validators={"a": validators.Int(), "someemail": validators.Email})
def two_validators(self, a=None, someemail=None, *args):
errors = pylons.tmpl_context.form_errors
values = pylons.tmpl_context.form_values
return dict(a=a, someemail=someemail,
errors=str(errors), values=str(values))
# -*- coding: utf-8 -*-
import formencode
from formencode.htmlfill import html_quote
from paste.fixture import TestApp
from paste.registry import RegistryManager
from __init__ import TestWSGIController
def custom_error_formatter(error):
return '<p><span class="pylons-error">%s</span></p>\n' % html_quote(error)
class NetworkForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
new_network = formencode.validators.URL(not_empty=True)
class HelloForm(formencode.Schema):
hello = formencode.ForEach(formencode.validators.Int())
def make_validating_controller():
from pylons.decorators import validate
from pylons.controllers import WSGIController
class ValidatingController(WSGIController):
def new_network(self):
return """
<form method="POST" action="/dhcp/new_form">
<table>
<tbody><tr>
<th>Network</th></tr></tbody></table></form>
from pylons.decorators import validate
from pylons.controllers import WSGIController
from __init__ import ControllerWrap, SetupCacheGlobal, TestWSGIController
import formencode
from formencode.htmlfill import html_quote
def custom_error_formatter(error):
return '<p><span class="pylons-error">%s</span></p>\n' % html_quote(error)
class NetworkForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
new_network = formencode.validators.URL(not_empty=True)
class HelloForm(formencode.Schema):
hello = formencode.ForEach(formencode.validators.Int())
class ValidatingController(WSGIController):
def new_network(self):
return """
<form method="POST" action="/dhcp/new_form">
<table>
<tbody><tr>
<th>Network</th>
<td>
<input value="" type="text" name="new_network" id="new_network">
</td>
</tr></tbody></table></form>
def test_callable_error_handler(self):
class ColorSchema(Schema):
colors = ForEach(validators.String(not_empty=True))
class RootController(object):
@expose()
def errors(self, *args, **kwargs):
return 'There was an error!'
@expose(schema=ColorSchema(),
error_handler=lambda: '/errors',
variable_decode=True)
def index(self, **kwargs):
return 'Success!'
# test with error handler
app = TestApp(make_app(RootController()))
r = app.post('/', {
def test_error_for(self):
if 'mako' not in builtin_renderers:
return
class ColorSchema(Schema):
colors = ForEach(validators.String(not_empty=True))
class RootController(object):
@expose(template='mako:error_for.html')
def errors(self, *args, **kwargs):
return dict()
@expose(template='mako:error_for.html',
schema=ColorSchema(),
variable_decode=True)
def index(self, **kwargs):
return dict()
@expose(schema=ColorSchema(),
error_handler='/errors',
variable_decode=True)
def test_with_variable_decode(self):
class ColorSchema(Schema):
colors = ForEach(validators.String(not_empty=True))
class RootController(object):
@expose()
def errors(self, *args, **kwargs):
return 'Error with %s!' % ', '.join(request.pecan['validation_errors'].keys())
@expose(schema=ColorSchema(),
variable_decode=True)
def index(self, **kwargs):
if request.pecan['validation_errors']:
return ', '.join(request.pecan['validation_errors'].keys())
else:
return 'Success!'
@expose(schema=ColorSchema(),
def test_hooks_with_validation(self):
run_hook = []
class RegistrationSchema(Schema):
first_name = validators.String(not_empty=True)
last_name = validators.String(not_empty=True)
email = validators.Email()
username = validators.PlainText()
password = validators.String()
password_confirm = validators.String()
age = validators.Int()
chained_validators = [
validators.FieldsMatch('password', 'password_confirm')
]
class SimpleHook(PecanHook):
def on_route(self, state):
run_hook.append('on_route')
def before(self, state):
run_hook.append('before')
def after(self, state):
run_hook.append('after')
def test_simple_validation(self):
class RegistrationSchema(Schema):
first_name = validators.String(not_empty=True)
last_name = validators.String(not_empty=True)
email = validators.Email()
username = validators.PlainText()
password = validators.String()
password_confirm = validators.String()
age = validators.Int()
chained_validators = [
validators.FieldsMatch('password', 'password_confirm')
]
class RootController(object):
@expose(schema=RegistrationSchema())
def index(self, first_name,
last_name,
email,
username,
password,
password_confirm,