Django

Code

Changeset 8342

Show
Ignore:
Timestamp:
08/13/08 22:57:46 (4 months ago)
Author:
mtredinnick
Message:

Implemented a flush() method on sessions that cleans out the session and
regenerates the key. Used to ensure the caller gets a fresh session at logout,
for example.

Based on a patch from mrts. Refs #7515.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/sessions/backends/base.py

    r8341 r8342  
    224224        return self.get('_session_expiry') == 0 
    225225 
     226    def flush(self): 
     227        """ 
     228        Removes the current session data from the database and regenerates the 
     229        key. 
     230        """ 
     231        self.clear() 
     232        self.delete() 
     233        self.create() 
     234 
    226235    # Methods that child classes must implement. 
    227236 
     
    248257        raise NotImplementedError 
    249258 
    250     def delete(self, session_key): 
    251         """ 
    252         Clears out the session data under this key. 
     259    def delete(self, session_key=None): 
     260        """ 
     261        Deletes the session data under this key. If the key is None, the 
     262        current session key value is used. 
    253263        """ 
    254264        raise NotImplementedError 
  • django/trunk/django/contrib/sessions/backends/cache.py

    r8340 r8342  
    4040        return False 
    4141 
    42     def delete(self, session_key): 
     42    def delete(self, session_key=None): 
     43        if session_key is None: 
     44            session_key = self._session_key 
    4345        self._cache.delete(session_key) 
    4446 
  • django/trunk/django/contrib/sessions/backends/db.py

    r8340 r8342  
    6262            raise 
    6363 
    64     def delete(self, session_key): 
     64    def delete(self, session_key=None): 
     65        if session_key is None: 
     66            session_key = self._session_key 
    6567        try: 
    6668            Session.objects.get(session_key=session_key).delete() 
  • django/trunk/django/contrib/sessions/backends/file.py

    r8340 r8342  
    9090        return False 
    9191 
    92     def delete(self, session_key): 
     92    def delete(self, session_key=None): 
     93        if session_key is None: 
     94            session_key = self._session_key 
    9395        try: 
    9496            os.unlink(self._key_to_file(session_key)) 
  • django/trunk/django/contrib/sessions/tests.py

    r8341 r8342  
    2424False 
    2525 
     26>>> db_session['foo'] = 'bar' 
     27>>> db_session.save() 
     28>>> db_session.exists(db_session.session_key) 
     29True 
     30>>> prev_key = db_session.session_key 
     31>>> db_session.flush() 
     32>>> db_session.exists(prev_key) 
     33False 
     34>>> db_session.session_key == prev_key 
     35False 
     36>>> db_session.modified, db_session.accessed 
     37(True, True) 
     38 
    2639>>> file_session = FileSession() 
    2740>>> file_session.modified 
     
    4053>>> file_session.exists(file_session.session_key) 
    4154False 
     55 
     56>>> file_session['foo'] = 'bar' 
     57>>> file_session.save() 
     58>>> file_session.exists(file_session.session_key) 
     59True 
     60>>> prev_key = file_session.session_key 
     61>>> file_session.flush() 
     62>>> file_session.exists(prev_key) 
     63False 
     64>>> file_session.session_key == prev_key 
     65False 
     66>>> file_session.modified, file_session.accessed 
     67(True, True) 
    4268 
    4369# Make sure the file backend checks for a good storage dir 
     
    6288>>> cache_session.exists(cache_session.session_key) 
    6389False 
     90>>> cache_session['foo'] = 'bar' 
     91>>> cache_session.save() 
     92>>> cache_session.exists(cache_session.session_key) 
     93True 
     94>>> prev_key = cache_session.session_key 
     95>>> cache_session.flush() 
     96>>> cache_session.exists(prev_key) 
     97False 
     98>>> cache_session.session_key == prev_key 
     99False 
     100>>> cache_session.modified, cache_session.accessed 
     101(True, True) 
    64102 
    65103>>> s = SessionBase() 
  • django/trunk/docs/sessions.txt

    r8341 r8342  
    110110 
    111111It also has these methods: 
     112 
     113    * ``flush()`` 
     114 
     115      **New in Django development version** 
     116 
     117      Delete the current session data from the database and regenerate the 
     118      session key value that is sent back to the user in the cookie. This is 
     119      used if you want to ensure that the previous session data can't be 
     120      accessed again from the user's browser (for example, the standard 
     121      ``logout()`` method calls it). 
    112122 
    113123    * ``set_test_cookie()``