Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #7429 -- Modified the Session base class a little more dictiona…

…ry-like by adding update(), has_key(), values(), and iterator access methods. Thanks to Jeremy Dunck for the suggestion and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7687 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit dc7f21daf73d2aae66c3bb8a043a59cbace6e602 1 parent 0694c75
Russell Keith-Magee freakboy3742 authored
19 django/contrib/sessions/backends/base.py
View
@@ -87,6 +87,25 @@ def decode(self, session_data):
except:
return {}
+ def update(self, dict_):
+ self._session.update(dict_)
+ self.modified = True
+
+ def has_key(self, key):
+ return self._session.has_key(key)
+
+ def values(self):
+ return self._session.values()
+
+ def iterkeys(self):
+ return self._session.iterkeys()
+
+ def itervalues(self):
+ return self._session.itervalues()
+
+ def iteritems(self):
+ return self._session.iteritems()
+
def _get_new_session_key(self):
"Returns session key that isn't being used."
# The random module is seeded when this Apache child is created.
60 django/contrib/sessions/tests.py
View
@@ -89,6 +89,66 @@
>>> s.pop('some key', 'does not exist')
'does not exist'
+
+>>> s.get('update key', None)
+
+# test .update()
+>>> s.modified = s.accessed = False # Reset to pretend this wasn't accessed previously
+>>> s.update({'update key':1})
+>>> s.accessed, s.modified
+(True, True)
+>>> s.get('update key', None)
+1
+
+# test .has_key()
+>>> s.modified = s.accessed = False # Reset to pretend this wasn't accessed previously
+>>> s.has_key('update key')
+True
+>>> s.accessed, s.modified
+(True, False)
+
+# test .values()
+>>> s = SessionBase()
+>>> s.values()
+[]
+>>> s.accessed
+True
+>>> s['x'] = 1
+>>> s.values()
+[1]
+
+# test .iterkeys()
+>>> s.accessed = False
+>>> i = s.iterkeys()
+>>> hasattr(i,'__iter__')
+True
+>>> s.accessed
+True
+>>> list(i)
+['x']
+
+# test .itervalues()
+>>> s.accessed = False
+>>> i = s.itervalues()
+>>> hasattr(i,'__iter__')
+True
+>>> s.accessed
+True
+>>> list(i)
+[1]
+
+# test .iteritems()
+>>> s.accessed = False
+>>> i = s.iteritems()
+>>> hasattr(i,'__iter__')
+True
+>>> s.accessed
+True
+>>> list(i)
+[('x', 1)]
+
+
+
#########################
# Custom session expiry #
#########################
Please sign in to comment.
Something went wrong with that request. Please try again.