Discussion:
[albatross-users] SimpleSessionApp, <a href> and session time out
Hans Kramer
2008-10-09 08:53:26 UTC
Permalink
Dear all,

I am new to Albatross and Python Webframe works. Moreover, I am not a
seasoned web developer and in the past I really hated working with this
kind of technology. However, I have to say that I really like what you
guys have done with Albatross. I am excited.

I have a few questions and half baked solutions I want to have some feed
back on.

First of all I would like to use the SimpleSessionApp class
instead of the RandomModularSessionApp class. (I tend to like to work
with classes) Perhaps this is a mistake, just be frank.

A)
Then I want to have a link in my template files for people to log out.
Something like this: (I also need these kind of links for other
purposes, I might come on in a later e-mail)

<al-a href="logout">Log Out</al-a>

Clicking on it, the frame work should execute:
class LogoutPage:

name = 'logout'

def page_process(self, context):
context.remove_session()
context.set_page('login')

Currently that didn't work. I found a solution by making some small
changes. First I edited in app.py the run method of class Application.
Just before self.load_page(ctx) I put the following command:
if os.environ.has_key('PATH_INFO'):
ctx.locals.__page__ = os.environ['PATH_INFO'][1:]

This works fine, except that in the next page the URI is rendered as
/cgi-bin/application.py/logout and the user cannot log in. I fixed this
by editing cgiapp.py and changing
def get_uri(self):
return self.get_param('REQUEST_URI')
to
def get_uri(self):
return self.get_param('SCRIPT_NAME')

Is this going to break anything? Is this a good solution? Or am I doing
something fundamentally wrong (by for instance using SimpleSessionApp).
If not, can I supply a patch?

B)
Session time out that was something that was biting me too. I work for a
Pharmaceutical company (clinical trail) and they really hate to see
stack traces. So when a session times out, instead of the stack trace I
want to refer it to a TimeOut page. (in the future I might want to
implement something that browser keeps refreshing the session as long as
it is alive... anybody?).
I solved that in similar fashion as above. However, due to lack of the
understanding of the internals I hard coded the page name timeout.
Again I edited run method in app.py. If the load_session fails, I create
a new session and send if off to the timeout page.

try:
self.load_session(ctx)
if os.environ.has_key('PATH_INFO'):
ctx.locals.__page__ = os.environ['PATH_INFO'][1:]
except:
ctx.new_session()
ctx.locals.__page__ = "timeout"

Please let me know what anybody thinks of the edits I made.
Thanks again for creating such a new framework.

Kind regards,

Hans Kramer
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://www.object-craft.com.au/pipermail/albatross-users/attachments/20081009/33356b9b/attachment.pgp>
Damian McGuckin
2008-10-09 10:41:35 UTC
Permalink
First of all I would like to use the SimpleSessionApp class instead of
the RandomModularSessionApp class. (I tend to like to work with classes)
The RandomModularSessionApp is just as class-friendly as the other. I have
found the Random... far easier with which to work that the other.
Perhaps this is a mistake, just be frank.
Probably more a misconception. Then again, my experience is biased to
those things with which I have worked. Just my $0.02.

For the other things, I will let the more experienced hands/brains make
suggestions.

- Damian

Pacific Engineering Systems International, 277-279 Broadway, Broadway NSW 2007
Ph:+61-2-8571-0847 .. Fx:+61-2-9692-9623 | unsolicited email not wanted here !
Views and opinions here are mine and not those of any past or present employer
Andrew McNamara
2011-02-01 07:16:00 UTC
Permalink
Post by Hans Kramer
I have a few questions and half baked solutions I want to have some feed
back on.
I have to apologise profusely for not seeing your message earlier. I hope
you haven't already given up in disgust.
Post by Hans Kramer
First of all I would like to use the SimpleSessionApp class
instead of the RandomModularSessionApp class. (I tend to like to work
with classes) Perhaps this is a mistake, just be frank.
There are several different styles of application you might choose from -
Simple, Modular or RandomModular. The "Simple" app classes use classes
for the pages, the "Modular" and "RandomModular" classes use python
modules for the pages. SimpleSessionApp should be fine for your purposes.
The "Random" application classes are used when you want users to be able to
choose pages via the URL (for the other application schemes, the page name
doesn't appear in the URL).
Post by Hans Kramer
A)
Then I want to have a link in my template files for people to log out.
Something like this: (I also need these kind of links for other
purposes, I might come on in a later e-mail)
<al-a href="logout">Log Out</al-a>
If you really want to use links to drive actions, you might want to use
one of the "Random" application classes (which are driven by URLs). The
non Random application classes are better driven via form submission.
Post by Hans Kramer
name = 'logout'
context.remove_session()
context.set_page('login')
Currently that didn't work. I found a solution by making some small
changes. First I edited in app.py the run method of class Application.
As mentioned above, only the Random apps will invoke this page when they
see a /logout URL, and the page_process() method is only called on form
submission (too late, in this case - you probably would use page_enter()
instead).

If you use one of the other application models, and had a page containing
a logout button:

<al-form method="post">
[...]
<al-input type="submit" name="logout" value="Logout" />
[...]
</al-form>

Then the page_process() method for that page might look like:

def page_process(self, ctx):
if ctx.req_equals('logout'):
ctx.remove_session()
ctx.set_page('login')

Note, no need for a logout page in this case.
Post by Hans Kramer
ctx.locals.__page__ = os.environ['PATH_INFO'][1:]
This works fine, except that in the next page the URI is rendered as
/cgi-bin/application.py/logout and the user cannot log in. I fixed this
by editing cgiapp.py and changing
return self.get_param('REQUEST_URI')
to
return self.get_param('SCRIPT_NAME')
Is this going to break anything? Is this a good solution? Or am I doing
something fundamentally wrong (by for instance using SimpleSessionApp).
If not, can I supply a patch?
It shouldn't be necessary to do this sort of low-level stuff.
Post by Hans Kramer
B)
Session time out that was something that was biting me too. I work for a
Pharmaceutical company (clinical trail) and they really hate to see
stack traces. So when a session times out, instead of the stack trace I
want to refer it to a TimeOut page. (in the future I might want to
implement something that browser keeps refreshing the session as long as
it is alive... anybody?).
Currently the framework doesn't provide much help with this, and I'm
looking at addressing this, but one approach to do what you want might
be to subclass application class and overload the load_session method
along these lines:

class Application(SimpleSessionApp):

def load_session(self, ctx):
try:
SimpleSessionApp.load_session(self, ctx)
except SessionExpired:
ctx.set_page('timeout')
--
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/
Loading...