durusmail: quixote-users: request.redirects in _q_lookup
request.redirects in _q_lookup
2003-06-07
request.redirects in _q_lookup
Jason Sibre
2003-06-07
Mark,

I'm kinda new to this, so I may not have done it in the most Quixotic way,
but it feels right, and it works well.

The idea is this:  When doing _q_lookup, I make sure they are logged in, and
if not, raise my sub-class of AccessError.  As a sub-class of PublishError,
this is caught by the quixote machinery, which in turn checks to see if I
have an exception handler in place which will handle it.  I do, and it
handles it by redirecting to the login screen.

One thing to note along these lines...  the _q_exception_handler thus called
must be in the root namespace of your app.  It cannot be in a sub-module, or
in an previously _q_lookup-ed object, as those are not yet part of the
namespace stack at the time a Traversal-time exception is being handled.
I've submitted a patch to remedy that, but I haven't gotten a reply on it,
yet.  Either the Quixote's maintainers are busy, or missed it, or they
didn't like it ;)

I've attached snippets of my code below to demontrate.  (I've hacked this up
a bit to eliminate some code that I didn't think pertained, so it may not
look completely sensible, and may not even run as you see it, but it should
make my intent clear.)

Note also that these snippets are actually in different files, but that
shouldn't matter, as long as they're imported correctly.

Hope this helps,

Jason Sibre
---------------------------------------------------------------------
class NotLoggedInError (AccessError):
    pass


def _q_lookup(request, component):
    app = getProjectTasksApp()
    requireLoggedIn(request)
    #print "_q_lookup for component \"%s\"" % component

    if component.find("user_") == 0:
        #Misc code to generate user object and store it in
        #newWebObj
    else:
        raise TraversalError, "Undefined object id (%s)" % component
    return newWebObj



def requireLoggedIn(request):
    if request.session.user == None:
        raise NotLoggedInError, "You must be logged in"


def _q_exception_handler (request, exc):
    app = getProjectTasksApp()
    print "exception handler called: %s" % exc


    if isinstance(exc, NotLoggedInError):
        print "NotLoggedInError: %s" % request.get_path()
        #make_goto() inspects the current request to figure out
        #what the user was trying to access, and makes a url
        #url param like "goto=" + url_quote("someurl?var=v1&var=v2")
        #login in turn redirects there after a successful login.
        goto = make_goto()
        request.redirect(getRootedURL("login?" + goto))
    else:
        print "Unknown Error: %s" % request.get_path()
        return htmltext("""
        
          My Unhandled Exception Handler
          
            

%s

%s

%s

%s """) % (exc.title, exc.description, exc.format(request), \ htmltext(request.dump_html())) --------------------------------------------------------------------- -----Original Message----- From: quixote-users-bounces@mems-exchange.org [mailto:quixote-users-bounces@mems-exchange.org]On Behalf Of Mark Bucciarelli Sent: Saturday, June 07, 2003 9:51 AM To: quixote-users@mems-exchange.org Subject: [Quixote-users] request.redirects in _q_lookup I want my top-level _q_lookup to redirect all invalid requests to the login form. This redirects : http://localhost/a but not this : http://localhost/a/ My _q_lookup looks like this: if not request.session.user or request.session.user != name: url = 'http://%s/login' % request.get_server() print ' redirect to login form at', url return request.redirect(url) else: print ' create user interface' return user.Interface(request, name) It looks to me (seee TraversalError below) like quixote is treating the login form HTML as an object and attempting to traverse it. Is there a way to short-circuit the traverse and force quixote to honor the redirect? I want the url to read "/login", as it is part of the ui. More details ... The debug output is this: case 1: http://pooh:8000/a ----------------------------------------------------------------- __init__._q_lookup, name= a user= None redirect to login form at http://pooh:8000/login LoginForm.__init__ case 2: http://pooh:8000/a/ ----------------------------------------------------------------- __init__._q_lookup, name= a user= None redirect to login form at http://pooh:8000/login __init__._q_exception_handler url = http://pooh:8000/a/ path = /a/ server = pooh:8000 Here's the publishing error text: ('\n Redirect to http://pooh:8000/login\n \n

Redirect

\n

Your browser should have redirected you to\n

http://pooh:8000/login
\n

\n \n\n' has no _q_exports list). -- Mark Bucciarelli, www.hubcapconsulting.com He who receives an idea from me, receives instruction himself without lessening mine; as he who lights his taper at mine receives light without darkening me. -- Thomas Jefferson _______________________________________________ Quixote-users mailing list Quixote-users@mems-exchange.org http://mail.mems-exchange.org/mailman/listinfo/quixote-users
reply