Code

Ticket #6087: ticket_6087__rev_6812.diff

File ticket_6087__rev_6812.diff, 1.1 KB (added by __hawkeye__, 6 years ago)

Add setdefult, includes tests.

Line 
1Index: django/contrib/sessions/tests.py
2===================================================================
3--- django/contrib/sessions/tests.py    (revision 6812)
4+++ django/contrib/sessions/tests.py    (working copy)
5@@ -66,6 +66,11 @@
6 >>> s.accessed, s.modified
7 (True, False)
8 
9+>>> s.setdefault('foo', 'bar')
10+'bar'
11+>>> s.setdefault('foo', 'baz')
12+'bar'
13+
14 >>> s.accessed = False  # Reset the accessed flag
15 
16 >>> s.pop('some key')
17Index: django/contrib/sessions/backends/base.py
18===================================================================
19--- django/contrib/sessions/backends/base.py    (revision 6812)
20+++ django/contrib/sessions/backends/base.py    (working copy)
21@@ -51,6 +51,14 @@
22         self.modified = self.modified or key in self._session
23         return self._session.pop(key, *args)
24 
25+    def setdefault(self, key, value):
26+        if key in self._session:
27+            return self._session[key]
28+        else:
29+            self.modified = True
30+            self._session[key] = value
31+            return value
32+
33     def set_test_cookie(self):
34         self[self.TEST_COOKIE_NAME] = self.TEST_COOKIE_VALUE
35