2010/3/12 Branko Vukelic <[email protected]>

> I think you can't really pinpoint the session timeout by any means
> other than checking it in a constantly-running background process per
> session (I think that'd be expensive), or per request.
>

I analyzed session.py and it is already checking if session alive (or
timeout) so it is not necessary to check in my app.

In session.py Session class checks in *_clean()* function whether session
alive, if not it calls Store Object's *cleanup()* function and detects all
sessions which are not alive then delete them.

In my app i am saving sessions on disk (not db) and i have a table which
holds users login status, so i need update my table when users session
timeout (actually not "timeout", i am doing this at first request after
session timeout, it is ok for me, i have always fresh login status ), i
could do this in my app in different ways but i wanted to do this inside
web.py and did some hacking to achive this.

Here is step by step what i did:

*In my application

*I added a parameter to web.config session_parameters called *
before_session_delete*, this parameter takes a function name which will be
run from session.py Store object

*web.config.session_parameters['before_session_delete'] =
my_before_session_delete_function

*Then i defined that function in my app

*def my_before_session_delete_function( session_data )*:
    *#do whatever you want


**In session.py* (changings are bold)

web.config.session_parameters = utils.storage({
    'cookie_name': 'webpy_session_id',
    'cookie_domain': None,
    'timeout': 86400, #24 * 60 * 60, # 24 hours in seconds
    'ignore_expiry': True,
    'ignore_change_ip': True,
    'secret_key': 'fLjUfxqXtfNoIldA0A0J',
    'expired_message': 'Session expired',
*    'before_session_delete': None*
})

#############################################################

class Session(utils.ThreadedDict):
    """Session management for web.py
    """

    def __init__(self, app, store, initializer=None):
        self.__dict__['store'] = store
        self.__dict__['_initializer'] = initializer
        self.__dict__['_last_cleanup_time'] = 0
        self.__dict__['_config'] =
utils.storage(web.config.session_parameters)

*        if self._config.before_session_delete:

self.store.set_before_session_delete(self._config.before_session_delete)*

        if app:
            app.add_processor(self._processor)


#######################################################################


class DiskStore(Store):

    def __init__(self, root):
        # if the storage root doesn't exists, create it.
        if not os.path.exists(root):
            os.mkdir(root)
        self.root = root
*        self.before_session_delete = None

    def set_before_session_delete(self, func):
        self.before_session_delete = func*

    def cleanup(self, timeout):
        now = time.time()
        for f in os.listdir(self.root):
            path = self._get_path(f)
            atime = os.stat(path).st_atime
            print "path = ", path
            if now - atime > timeout :
*                if self.before_session_delete:
                    try:

self.before_session_delete(self.__getitem__(os.path.basename(path)))
                    except ValueError, e:
                        pass*
                os.remove(path)


I had what i want, on before each session delete my function runs and i can
do whatever i want with session data before deleted.

I finished just now, as i tested it works, feel free to debug&modify it.


-- 
Aydın ŞEN

         Ege Üniversitesi
Uluslararası Bilgisayar Enstitüsü

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