2007-05-21 12:03:53 +08:00
|
|
|
r"""
|
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
|
|
|
|
>>> from django.contrib.sessions.backends.cache import SessionStore as CacheSession
|
|
|
|
>>> from django.contrib.sessions.backends.file import SessionStore as FileSession
|
2007-10-20 18:12:59 +08:00
|
|
|
>>> from django.contrib.sessions.backends.base import SessionBase
|
2007-05-21 12:03:53 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> db_session = DatabaseSession()
|
|
|
|
>>> db_session.modified
|
2007-07-03 23:02:40 +08:00
|
|
|
False
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> db_session['cat'] = "dog"
|
|
|
|
>>> db_session.modified
|
|
|
|
True
|
|
|
|
>>> db_session.pop('cat')
|
|
|
|
'dog'
|
|
|
|
>>> db_session.pop('some key', 'does not exist')
|
2007-07-03 23:02:40 +08:00
|
|
|
'does not exist'
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> db_session.save()
|
|
|
|
>>> db_session.exists(db_session.session_key)
|
2007-07-03 23:02:40 +08:00
|
|
|
True
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> db_session.delete(db_session.session_key)
|
|
|
|
>>> db_session.exists(db_session.session_key)
|
2007-07-03 23:02:40 +08:00
|
|
|
False
|
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> file_session = FileSession()
|
|
|
|
>>> file_session.modified
|
|
|
|
False
|
|
|
|
>>> file_session['cat'] = "dog"
|
|
|
|
>>> file_session.modified
|
2007-07-03 23:02:40 +08:00
|
|
|
True
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> file_session.pop('cat')
|
|
|
|
'dog'
|
|
|
|
>>> file_session.pop('some key', 'does not exist')
|
|
|
|
'does not exist'
|
|
|
|
>>> file_session.save()
|
|
|
|
>>> file_session.exists(file_session.session_key)
|
2007-07-03 23:02:40 +08:00
|
|
|
True
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> file_session.delete(file_session.session_key)
|
|
|
|
>>> file_session.exists(file_session.session_key)
|
|
|
|
False
|
2007-05-21 12:03:53 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> cache_session = CacheSession()
|
|
|
|
>>> cache_session.modified
|
|
|
|
False
|
|
|
|
>>> cache_session['cat'] = "dog"
|
|
|
|
>>> cache_session.modified
|
|
|
|
True
|
|
|
|
>>> cache_session.pop('cat')
|
|
|
|
'dog'
|
|
|
|
>>> cache_session.pop('some key', 'does not exist')
|
2007-05-21 12:03:53 +08:00
|
|
|
'does not exist'
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> cache_session.save()
|
|
|
|
>>> cache_session.delete(cache_session.session_key)
|
|
|
|
>>> cache_session.exists(cache_session.session_key)
|
|
|
|
False
|
2007-10-20 18:12:59 +08:00
|
|
|
|
|
|
|
>>> s = SessionBase()
|
|
|
|
>>> s._session['some key'] = 'exists' # Pre-populate the session with some data
|
|
|
|
>>> s.accessed = False # Reset to pretend this wasn't accessed previously
|
|
|
|
|
|
|
|
>>> s.accessed, s.modified
|
|
|
|
(False, False)
|
|
|
|
|
|
|
|
>>> s.pop('non existant key', 'does not exist')
|
|
|
|
'does not exist'
|
|
|
|
>>> s.accessed, s.modified
|
|
|
|
(True, False)
|
|
|
|
|
2007-12-02 23:27:29 +08:00
|
|
|
>>> s.setdefault('foo', 'bar')
|
|
|
|
'bar'
|
|
|
|
>>> s.setdefault('foo', 'baz')
|
|
|
|
'bar'
|
|
|
|
|
2007-10-20 18:12:59 +08:00
|
|
|
>>> s.accessed = False # Reset the accessed flag
|
|
|
|
|
|
|
|
>>> s.pop('some key')
|
|
|
|
'exists'
|
|
|
|
>>> s.accessed, s.modified
|
|
|
|
(True, True)
|
|
|
|
|
|
|
|
>>> s.pop('some key', 'does not exist')
|
|
|
|
'does not exist'
|
2007-05-21 12:03:53 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|