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 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(),
class DirectoryGetSessionsValidator(Schema):
"""Directory get Sessions validator"""
auth_request = validators.String()
date_created = ValidateISODate()
service_icon = validators.String()
service_id = validators.String()
service_name = validators.String()
allow_extra_fields = True
class DirectoryValidator(Schema):
"""Directory entity validator"""
id = validators.String()
service_ids = ForEach(validators.String())
sdk_keys = ForEach(validators.String())
premium = validators.Bool()
name = validators.String()
android_key = validators.String()
ios_certificate_fingerprint = validators.String()
active = validators.Bool()
denial_context_inquiry_enabled = validators.Bool(if_empty=False,
if_missing=False)
webhook_url = validators.String()
allow_extra_fields = True
class DirectoryDeviceLinkCompletionValidator(Schema):
"""Directory User Device link completion validator"""
type = validators.OneOf(['DEVICE_LINK_COMPLETION'])
device_id = validators.String()
class DirectoryGetSessionsValidator(Schema):
"""Directory get Sessions validator"""
auth_request = validators.String()
date_created = ValidateISODate()
service_icon = validators.String()
service_id = validators.String()
service_name = validators.String()
allow_extra_fields = True
class DirectoryValidator(Schema):
"""Directory entity validator"""
id = validators.String()
service_ids = ForEach(validators.String())
sdk_keys = ForEach(validators.String())
premium = validators.Bool()
name = validators.String()
android_key = validators.String()
ios_certificate_fingerprint = validators.String()
active = validators.Bool()
denial_context_inquiry_enabled = validators.Bool(if_empty=False,
if_missing=False)
webhook_url = validators.String()
allow_extra_fields = True
class DirectoryDeviceLinkCompletionValidator(Schema):
"""Directory User Device link completion validator"""
type = validators.OneOf(['DEVICE_LINK_COMPLETION'])
device_id = validators.String()
device_public_key = validators.String()
service_pins = ForEach()
auth_request = validators.String() # UUID
type = validators.String()
reason = validators.String()
denial_reason = validators.String(if_missing=None, if_empty=None)
device_id = validators.String()
auth_policy = AuthPolicyValidator(if_missing=None)
auth_methods = ForEach(AuthMethodsValidator())
allow_extra_fields = True
class AuthorizeValidator(Schema):
"""Authorize entity validator"""
auth_request = validators.String(not_empty=True)
push_package = validators.String(if_missing=None, not_empty=True)
device_ids = ForEach(validators.String(), if_missing=None)
allow_extra_fields = True
class AuthorizeSSEValidator(Schema):
"""Authorize server-sent-event (webhook) validator"""
service_user_hash = validators.String()
api_time = validators.String()
allow_extra_fields = True
class ServiceValidator(Schema):
"""Service entity validation"""
id = validators.String()
icon = validators.String()
name = validators.String()
description = validators.String()
if "type" in value:
if value["type"] == "TERRITORY":
value.update(PolicyTerritoryValidator().to_python(
value, state))
elif value["type"] == "GEO_CIRCLE":
value.update(PolicyGeoCircleValidator().to_python(
value, state))
return value
class ConditionalGeoFenceValidator(Schema):
"""Validates conditional geofence policies"""
allow_extra_fields = True
inside = validators.NotEmpty(accept_iterator=True)
outside = validators.NotEmpty(accept_iterator=True)
fences = ForEach(not_empty=True)
@staticmethod
def _validate_python(value, state):
if 'inside' in value and 'outside' in value:
value['inside'] = PolicyBaseValidator().to_python(
value['inside'], state)
value['outside'] = PolicyBaseValidator().to_python(
value['outside'], state)
return value
class MethodAmountPolicyValidator(Schema):
"""Validates method amount policies"""
allow_extra_fields = True
amount = validators.Int(not_empty=True)