Permalink
Browse files

Add Redirect support. Redirect is an exception, and also add Redirect…

… support

   in middle_session. Because Redirect is an exception, so it'll implement process_exception
   and save the session also.

git-svn-id: https://uliweb.googlecode.com/svn/trunk@950 14287918-b64d-0410-abb6-a92efa0c2026
  • Loading branch information...
1 parent 584ac13 commit 746a2967aeaaefb8d0bb67c2cf535c3004c89115 limodou committed Jul 11, 2012
Showing with 26 additions and 2 deletions.
  1. +3 −0 CHANGELOG.txt
  2. +1 −1 uliweb/__init__.py
  3. +8 −1 uliweb/contrib/session/middle_session.py
  4. +14 −0 uliweb/core/SimpleFrame.py
View
@@ -40,6 +40,9 @@ Uliweb Change Log
Above code will override the default template filename from ``index.html``
to ``index.ahtml``.
+#. Add Redirect support. Redirect is an exception, and also add Redirect support
+ in middle_session. Because Redirect is an exception, so it'll implement process_exception
+ and save the session also.
0.1.2 Version
-----------------
View
@@ -15,7 +15,7 @@
class UliwebError(Exception): pass
-from uliweb.core.SimpleFrame import (Request, Response, redirect, error, json,
+from uliweb.core.SimpleFrame import (Request, Response, redirect, Redirect, error, json,
POST, GET, url_for, expose, get_app_dir, get_apps, function, decorators,
functions, response, request, settings, application, NotFound, HTTPException,
)
@@ -1,4 +1,4 @@
-from uliweb import Middleware
+from uliweb import Middleware, Redirect
from weto.session import Session, SessionCookie
class SessionMiddle(Middleware):
@@ -52,3 +52,10 @@ def process_response(self, request, response):
path=c.path, secure=c.secure)
return response
+ def process_exception(self, request, e):
+ """
+ Still process session data when specially Exception
+ """
+ if isinstance(e, Redirect):
+ response = e.get_response()
+ self.process_response(request, response)
@@ -104,6 +104,16 @@ def redirect(location, code=302):
response.headers['Location'] = location
return response
+class Redirect(Exception):
+ """
+ This is an exception, which can be raised in view function
+ """
+ def __init__(self, location, code=302):
+ self.response = redirect(location, code)
+
+ def get_response(self):
+ return self.response
+
def error(message='', errorpage=None, request=None, appname=None, **kwargs):
kwargs.setdefault('message', message)
if request:
@@ -392,6 +402,7 @@ def _prepare_env(self):
env = Storage({})
env['url_for'] = url_for
env['redirect'] = redirect
+ env['Redirect'] = Redirect
env['error'] = error
env['application'] = self
env['settings'] = settings
@@ -711,6 +722,7 @@ def get_view_env(self):
local_env['response'] = local.response
local_env['url_for'] = url_for
local_env['redirect'] = redirect
+ local_env['Redirect'] = Redirect
local_env['error'] = error
local_env['settings'] = __global__.settings
local_env['json'] = json
@@ -986,6 +998,8 @@ def __call__(self, environ, start_response):
except HTTPError, e:
response = self.render(e.errorpage, Storage(e.errors))
+ except Redirect, e:
+ response = e.get_response()
except NotFound, e:
response = self.not_found(e)
except HTTPException, e:

0 comments on commit 746a296

Please sign in to comment.