Skip to content

Instantly share code, notes, and snippets.

@kucerarichard
Forked from amol-/request_validation.py
Created September 3, 2019 21:24
Show Gist options
  • Save kucerarichard/b1fdc1b6eb56717c6496e61a7f1dda00 to your computer and use it in GitHub Desktop.
Save kucerarichard/b1fdc1b6eb56717c6496e61a7f1dda00 to your computer and use it in GitHub Desktop.
Test TurboGears request.validation in minimal mode
from wsgiref.simple_server import make_server
import tg
from tg import RestController, expose, validate, MinimalApplicationConfigurator
from formencode import validators
class RootController(RestController):
@expose('json')
@validate({"man":validators.String(not_empty=True), "pin":validators.String(not_empty=True), "sou":validators.String(not_empty=True), "win":validators.String(not_empty=True)})
def calc(self, **kw):
validation_status = tg.request.validation
errors = [{key: str(value)} for key, value in validation_status.errors.items()]
return dict(errors=errors)
# Configure a new minimal application with our root controller.
config = MinimalApplicationConfigurator()
config.update_blueprint({
'root_controller': RootController()
})
# Serve the newly configured web application.
print("Serving on port 8080...")
httpd = make_server('', 8080, config.make_wsgi_app())
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment