Hi again guys,

For DecentURL.com I've implemented my own extremely simple session handling
that (lazily) loads a session from the DB into web.ctx.session at the start
of the request, and then saves it back out if it's changed at the end.

First I implemented this manually, just putting init() and end() at the
beginning and end of every request method, like so:

class home:
    def GET(self):
        init()
        web.render('home.html')
        end()

But having the init()/end() automatically called would be much cooler, so I
implemented them as WSGI middleware. Nice and simple ... and would have
worked fine, except that the middleware is called *before* web.ctx is set
up, so doing it that way meant I would have had to make my own version of
threadeddict() -- dumb. I'm presuming other people either haven't done this,
or used session middleware that do their own thread-safing?

Anyway, then I decided to implement init as the __init__ of the home class,
and just have all your request classes inherit from Req, or something.
That's cooler, anyway, because then you can inherit from different classes,
like Html or Text or Rss, and have __init__() also send out the right
content type for that request.

But I still needed an end(), and __del__ won't do, because you don't know
when/whether it's going to be called (see
http://decenturl.com/docs.python/custom-del). So I use __init__ but had to
call my end() manually. I just modified request.py's handle() to add a call
to inst.end() if it exists.

And it works nicely, so I thought I'd share it if other people need to
similar things. I guess you could have a .start() as well, instead of or as
well as using __init__, just to make it a bit more symmetrical. But here's
the code in handle():

--------------
def handle(...):
    ...
    inst = cls()  # __init__ called here
    tocall = getattr(inst, meth)
    ...
    r = tocall(...)
    if hasattr(inst, 'end'):
        inst.end()
    return r
--------------

Cheers,
Ben.

-- 
Ben Hoyt, http://benhoyt.com/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to