Adding a status-based redirect handler

This commit is contained in:
Yoann Roman
2011-01-14 16:57:24 -05:00
parent 8c981ef26f
commit ed5c644779
6 changed files with 49 additions and 8 deletions

View File

@@ -1,8 +1,9 @@
from paste.urlparser import StaticURLParser
from paste.cascade import Cascade
from paste.errordocument import make_errordocument
from paste.recursive import RecursiveMiddleware
from paste.urlparser import StaticURLParser
from weberror.errormiddleware import ErrorMiddleware
from weberror.evalexception import EvalException
from paste.recursive import RecursiveMiddleware
from core import Pecan, request, response, override_template, abort, redirect, error_for
from decorators import expose
@@ -23,6 +24,7 @@ def make_app(root, static_root=None, debug=False, errorcfg={}, wrap_app=None, **
app = EvalException(app, **errorcfg)
else:
app = ErrorMiddleware(app, **errorcfg)
app = make_errordocument(app, conf, **conf.app.errors)
if static_root:
app = Cascade([StaticURLParser(static_root), app])
return app

View File

@@ -10,7 +10,10 @@ app = {
'modules' : [],
'static_root' : 'public',
'template_path' : '',
'debug' : False
'debug' : False,
'errors' : {
'__force_dict__' : True
}
}
# Custom Configurations must be in Python dictionary format in user config

View File

@@ -1,5 +1,6 @@
from pecan import expose, request
from formencode import Schema, validators as v
from webob.exc import status_map
class SampleForm(Schema):
@@ -15,3 +16,12 @@ class RootController(object):
@expose('success.html', schema=SampleForm(), error_handler='index')
def handle_form(self, name, age):
return dict(name=name, age=age)
@expose('error.html')
def error(self, status):
try:
status = int(status)
except ValueError:
status = 0
message = getattr(status_map.get(status), 'explanation', '')
return dict(status=status, message=message)

View File

@@ -0,0 +1,12 @@
<%inherit file="layout.html" />
## provide definitions for blocks we want to redefine
<%def name="title()">
Server Error ${status}
</%def>
## now define the body of the template
<header>
<h1>Server Error ${status}</h1>
</header>
<p>${message}</p>

View File

@@ -15,7 +15,11 @@ app = {
'static_root' : 'public',
'template_path' : '${package}/templates',
'reload': True,
'debug' : True
'debug' : True,
'errors' : {
'404' : '/error/404',
'__force_dict__' : True
}
}
# Custom Configurations must be in Python dictionary format::

View File

@@ -1,11 +1,15 @@
import os
from formencode import Schema, validators
from paste.recursive import ForwardRequestException
from unittest import TestCase
from webtest import TestApp
from pecan import Pecan, expose, request, response, redirect, abort
from pecan.templating import _builtin_renderers as builtin_renderers
from webtest import TestApp
from formencode import Schema, validators
import os
class TestBase(object):
class TestBase(TestCase):
def test_simple_app(self):
class RootController(object):
@@ -366,6 +370,10 @@ class TestBase(object):
def index(self):
redirect('/testing')
@expose()
def internal(self):
redirect('/testing', internal=True)
@expose()
def permanent(self):
redirect('/testing', code=301)
@@ -381,6 +389,8 @@ class TestBase(object):
assert r.status_int == 200
assert r.body == 'it worked!'
self.assertRaises(ForwardRequestException, app.get, '/internal')
r = app.get('/permanent')
assert r.status_int == 301
r = r.follow()