Adding a status-based redirect handler
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
							
								
								
									
										12
									
								
								pecan/templates/project/+package+/templates/error.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								pecan/templates/project/+package+/templates/error.html
									
									
									
									
									
										Normal 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> | ||||
| @@ -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:: | ||||
|   | ||||
| @@ -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() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Yoann Roman
					Yoann Roman