Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1287

Show
Ignore:
Timestamp:
08/26/06 21:12:23
Author:
fumanchu
Message:

Fix for #525 (Support Deleting Sessions).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/sessions.py

    r1280 r1287  
    118118            t.start() 
    119119     
     120    def delete(self): 
     121        """Delete stored session data.""" 
     122        self._delete() 
     123     
    120124    def __getitem__(self, key): 
    121125        if not self.loaded: self.load() 
     
    188192    def _save(self, expiration_time): 
    189193        self.cache[self.id] = (self._data, expiration_time) 
     194     
     195    def _delete(self): 
     196        del self.cache[self.id] 
    190197     
    191198    def acquire_lock(self): 
     
    225232        finally: 
    226233            f.close() 
     234     
     235    def _delete(self): 
     236        os.unlink(self._get_file_path()) 
    227237     
    228238    def acquire_lock(self, path=None): 
     
    304314     
    305315    def _save(self, expiration_time): 
     316        pickled_data = pickle.dumps(self._data) 
     317        self.cursor.execute('update session set data = %s, ' 
     318                            'expiration_time = %s where id = %s', 
     319                            (pickled_data, expiration_time, self.id)) 
     320     
     321    def _delete(self): 
    306322        self.cursor.execute('delete from session where id=%s', (self.id,)) 
    307         pickled_data = pickle.dumps(self._data) 
    308         self.cursor.execute( 
    309             'insert into session (id, data, expiration_time) values (%s, %s, %s)', 
    310             (self.id, pickled_data, expiration_time)) 
    311      
     323    
    312324    def acquire_lock(self): 
    313325        # We use the "for update" clause to lock the row 
     326        self.locked = True 
    314327        self.cursor.execute('select id from session where id=%s for update', 
    315328                            (self.id,)) 
     
    319332        #   introduced by the "for update" clause 
    320333        self.cursor.close() 
     334        self.locked = False 
    321335     
    322336    def clean_up(self): 
     
    403417        cookie[name]['secure'] = 1 
    404418 
     419def expire(): 
     420    """Expire the current session cookie.""" 
     421    name = cherrypy.request.config.get('tools.sessions.name', 'session_id') 
     422    one_year = 60 * 60 * 24 * 365 
     423    exp = time.gmtime(time.time() - one_year) 
     424    t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", exp) 
     425    cherrypy.response.simple_cookie[name]['expires'] = t 
     426 
  • trunk/cherrypy/test/test_session.py

    r1275 r1287  
    99 
    1010import cherrypy 
     11from cherrypy.lib import sessions 
    1112 
    1213 
     
    4445            return str(c) 
    4546        index.exposed = True 
     47         
     48        def delete(self): 
     49            cherrypy.session.delete() 
     50            sessions.expire() 
     51            return "done" 
     52        delete.exposed = True 
    4653     
    4754    cherrypy.tree.mount(Root()) 
     
    5966        self.getPage('/testStr', self.cookies) 
    6067        self.assertBody('3') 
     68         
    6169        self.getPage('/setsessiontype/file') 
    6270        self.getPage('/testStr') 
     
    7280        self.assertBody('1') 
    7381         
    74         # Wait for the cleanup thread to delete session files 
     82        # Test session delete 
     83        self.getPage('/delete', self.cookies) 
     84        self.assertBody("done") 
     85        f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')] 
     86        self.assertEqual(f(), []) 
     87         
     88        # Wait for the cleanup thread to delete remaining session files 
     89        self.getPage('/') 
    7590        f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')] 
    7691        self.assertNotEqual(f(), []) 

Hosted by WebFaction

Log in as guest/cpguest to create tickets