Fixed #6087 -- Added setdefault() support to the Session object. Thanks, Ben

Slavin.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6831 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-12-02 15:27:29 +00:00
parent f610269006
commit 49da9ab57e
3 changed files with 15 additions and 0 deletions

View File

@ -51,6 +51,14 @@ class SessionBase(object):
self.modified = self.modified or key in self._session
return self._session.pop(key, *args)
def setdefault(self, key, value):
if key in self._session:
return self._session[key]
else:
self.modified = True
self._session[key] = value
return value
def set_test_cookie(self):
self[self.TEST_COOKIE_NAME] = self.TEST_COOKIE_VALUE

View File

@ -66,6 +66,11 @@ False
>>> s.accessed, s.modified
(True, False)
>>> s.setdefault('foo', 'bar')
'bar'
>>> s.setdefault('foo', 'baz')
'bar'
>>> s.accessed = False # Reset the accessed flag
>>> s.pop('some key')

View File

@ -99,6 +99,8 @@ It implements the following standard dictionary methods:
* ``items()``
* ``setdefault()``
It also has these three methods:
* ``set_test_cookie()``