changeset 5:2693b81f5960

fix redirection behaviour (though ultimately the whole class should be refactored not to store request on it)
author k0s <k0scist@gmail.com>
date Sat, 26 Dec 2009 22:56:56 -0500
parents 4db4c02ed1ea
children 40fa556252a7
files bitsyauth/__init__.py
diffstat 1 files changed, 11 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/bitsyauth/__init__.py
+++ b/bitsyauth/__init__.py
@@ -41,17 +41,16 @@ class BitsyAuthInnerWare(object):
         * realm: realm for HTTP digest authentication
         """
 
         self.app = app
         self.passwords = passwords
         self.site = site or ''
         self.realm = realm or self.site
         self.captcha = True
-        self.redirect_to = '/' # redirect to site root        
         self.urls = { 'login': '/login', 'join': '/join', }
         self.keys = {} # keys, words for CAPTCHA request
 
         self.content_type = { 'image_captcha': 'image/png',
                               'wav_captcha': 'audio/wav' }
 
         if newuser:
             self.newuser = newuser
@@ -65,16 +64,17 @@ class BitsyAuthInnerWare(object):
             self.captcha = False
         
     ### WSGI/HTTP layer
 
     def __call__(self, environ, start_response):
 
         self.request = Request(environ)
         self.request.path_info = self.request.path_info.rstrip('/')
+        self.redirect_to = self.request.script_name 
 
         # URLs intrinsic to BitsyAuth
         if self.request.path_info == '/logout':
             response = self.redirect()
             return response(self.request.environ, start_response)
 
         if self.request.path_info in self.url_lookup():
             response = self.make_response()
@@ -163,21 +163,22 @@ class BitsyAuthInnerWare(object):
             return self.get_response(function(wrap=True),
                                      content_type=self.content_type.get(handler,'text/html'))
         if self.request.method == 'POST':
             post_func = getattr(self, handler + "_post")
             errors = post_func()
             if errors:
                 return self.get_response(function(errors=errors, wrap=True))
             else:
-                return self.redirect("Welcome!")
+                location = self.request.POST.get('referer')
+                return self.redirect("Welcome!", location=location)
 
-    def redirect(self, message=''):
+    def redirect(self, message='', location=None):
         """redirect from instrinsic urls"""
-        return exc.HTTPSeeOther(message, location=self.redirect_to)
+        return exc.HTTPSeeOther(message, location=location or self.redirect_to)
 
     def image_captcha(self, wrap=True):
         """return data for the image"""
         key = self.request.path_info.split('/join/')[-1]
         key = int(key.split('.png')[0])
         return skimpyAPI.Png(self.keys[key], scale=3.0).data()
                 
     ### forms and their display methods
@@ -185,23 +186,27 @@ class BitsyAuthInnerWare(object):
     ### login
 
     def login_form(self, referer=None, action=None):
         if action is None:
             action = self.urls['login']
         form = Form(action='', submit='Login')
         form.add_element('textfield', 'Name', input_name='username')
         form.add_element('password', 'Password', input_name='password')
-        if referer is not None:
+        if referer:
             form.add_element('hidden', 'referer', value=referer)
         return form
 
     def login(self, errors=None, wrap=False, action=None):
         """login div"""
-        form = self.login_form(action=action)
+        referer = None
+        if hasattr(self, 'request'):
+            referer = self.request.referer
+
+        form = self.login_form(action=action, referer=referer)
         join = self.urls.get('join')
         retval = form(errors)
         if join:        
             retval += '<br/>\n' + markup.a('join', href="%s" % join)
         retval = markup.div(retval)
         if wrap:
             title = 'login'
             if self.site: