From dc7f21daf73d2aae66c3bb8a043a59cbace6e602 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Wed, 18 Jun 2008 12:07:46 +0000 Subject: [PATCH] Fixed #7429 -- Modified the Session base class a little more dictionary-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 --- django/contrib/sessions/backends/base.py | 19 ++++++++ django/contrib/sessions/tests.py | 60 ++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py index 1063760915..1f9c20871b 100644 --- a/django/contrib/sessions/backends/base.py +++ b/django/contrib/sessions/backends/base.py @@ -87,6 +87,25 @@ class SessionBase(object): 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. diff --git a/django/contrib/sessions/tests.py b/django/contrib/sessions/tests.py index 0f162b211f..a1bc6799fa 100644 --- a/django/contrib/sessions/tests.py +++ b/django/contrib/sessions/tests.py @@ -89,6 +89,66 @@ False >>> 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 # #########################