Dear all,

When you enable sessions in web.py, the default behavior of the framework is
to load/save the session into the store (e.g. a "sessions" table) at each
request.

Suppose you want a session management per request : i.e, some requests
should be associated with the session, some other should not (for
performance matters, for instance)

Here's a sample recipe : let me know what you think !
http://pastebin.com/Stn9uKvH

Thanks !
Franck


"""
Per-request session management
(web.py recipe)
"""

import web

def init_session():
    """ Initializes the session at a global level (web.config._session) """

    if web.config.get('_session') is None:
        db = web.database(dbn='sqlite', db='path_to_your_database')
        store = web.session.DBStore(db, 'sessions')
        # When building a Session, we must make sure that app is None,
otherwise an application processor will be set up.
        # Such a global processor would load / save the session for every
request, no matter what.
        web.config._session = web.session.Session(app = None, store = store,
initializer = {'count': 0})


def get_session():
    """ Returns the global session (web.config._session) """
    return web.config._session


def _load_session():
    """ Cleans up the session and loads it from the store, by the id from
cookie """
    session = get_session()
    session._cleanup()
    session._load()

def _save_session():
    """ Saves the session into the store """
    get_session()._save()

*def enable_sessions(func):
    """ Wraps a controller method (GET/POST) in order to handle session
management on a per-request basis """

    def wrapped_func(*args):
        _load_session()
        try :
            return func(*args)
        finally:
            _save_session()

    return wrapped_func*


class hello:
    """ Sample controller with a session-enabled GET method : see
http://localhost:8080/ """

*    @enable_sessions*
    def GET(self):

        # get_session() may be freely called since GET is properly decorated
        session = get_session()
        print 'session', session

        # Also, the session may be freely modified
        session.count += 1

        return 'Hello ! Sessions are enabled : count = %d.' % session.count

class anonymous:
    """ Sample controller with a session-disabled GET method : see
http://localhost:8080/anonymous """

    def GET(self):

        # get_session() may not be called here ; actually, if called, the
method will return an empty Storage
        return 'Hello ! Sessions are disabled.'

# Defines the URLs
urls = (
    '/',                'hello',
    '/anonymous',       'anonymous'
)

# Inits the web application
app = web.application(urls, globals())

# Initializes the session
init_session()

if __name__ == '__main__':

    # Runs the web application
    app.run()

-- 
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