[albatross-users] Finding Saved Values in Sessions

Dave Cole djc at object-craft.com.au
Sun Aug 18 19:49:28 EST 2002


>     Howdy.  I'm trying to use albatross for a web application that
> I'm writing.  I'm just starting to get into it however, and I
> already have a question:
> 
>     How can I check to see if a value is stored in a session?  For
> example when someone logs in to my application I have it store the
> loginName in the session.
> 
>     What I want to do is have the application check to see if that
> key/value is stored in the session.  I tried doing it this way:
> 
>     ctx.locals.isAuthenticated = ctx.decode_session(ctx.app.get_session(ctx.sesid())).kas_key('loginName')

In a session based application the loading of sessions happens
automatically.  By the time your page_enter(), page_leave(),
page_process(), or page_display() is executed the session has been
loaded into ctx.locals.  This means that all you really need to do is
something like the following:

    ctx.locals.isAuthenticated = hasattr(ctx.locals, 'loginName')

>     But it doesn't seem to work.

The problem is that decode_session() does not return a decoded
session, it decodes the session into ctx.locals and then returns None.

>     Some other things:
> 
>     # This seems to work, it returns the session id.
>     ctx.locals.sesid = ctx.sesid()
> 
>     # This doesn't seem to work.  It returns 'None'
>     ctx.locals.blah = ctx.app.get_session(ctx.sesid())

None should only be returned when the session does not exist.  This
will happen either the first time that the browser accesses the
application, or when an exception is raised in the application.  To be
defensive, the Application run() method deletes a session if it
catches an exception.  This prevents nasty situations where a browser
gets trapped in an exception hole.

>     Any idea how I can make this work?  I'm using the
>     ModularSessionFileApp and SessionFileAppContext modules.

Try the suggestion above.

- Dave

-- 
http://www.object-craft.com.au




More information about the Albatross-users mailing list