Code

Ticket #10899: easy_session_manipulation.diff

File easy_session_manipulation.diff, 3.9 KB (added by tallfred, 5 years ago)
Line 
1Index: test/client.py
2===================================================================
3--- test/client.py      (revision 10620)
4+++ test/client.py      (working copy)
5@@ -166,6 +166,7 @@ class Client(object):
6         self.cookies = SimpleCookie()
7         self.exc_info = None
8         self.errors = StringIO()
9+        self._session_cache = None
10 
11     def store_exc_info(self, **kwargs):
12         """
13@@ -177,12 +178,24 @@ class Client(object):
14         """
15         Obtains the current session variables.
16         """
17-        if 'django.contrib.sessions' in settings.INSTALLED_APPS:
18-            engine = import_module(settings.SESSION_ENGINE)
19-            cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
20-            if cookie:
21-                return engine.SessionStore(cookie.value)
22-        return {}
23+        if self._session_cache == None:
24+            if 'django.contrib.sessions' in settings.INSTALLED_APPS:
25+                engine = import_module(settings.SESSION_ENGINE)
26+                cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
27+                if cookie:
28+                    self._session_cache = engine.SessionStore(cookie.value)
29+                else:
30+                    self._session_cache = engine.SessionStore()
31+                    self._session_cache.save()  # make load() work so the cookie isn't worthless
32+                    self.cookies[settings.SESSION_COOKIE_NAME] = self._session_cache.session_key
33+            else:
34+                self._session_cache = {}
35+        return self._session_cache
36+    def _session_save(self):
37+        if self._session_cache:
38+            self._session_cache.save()
39+            self._session_cache = None
40+
41     session = property(_session)
42 
43     def request(self, **request):
44@@ -268,6 +281,7 @@ class Client(object):
45         """
46         Requests a response from the server using GET.
47         """
48+        self._session_save()
49         parsed = urlparse(path)
50         r = {
51             'CONTENT_TYPE':    'text/html; charset=utf-8',
52@@ -288,6 +302,7 @@ class Client(object):
53         """
54         Requests a response from the server using POST.
55         """
56+        self._session_save()
57         if content_type is MULTIPART_CONTENT:
58             post_data = encode_multipart(BOUNDARY, data)
59         else:
60@@ -319,6 +334,7 @@ class Client(object):
61         """
62         Request a response from the server using HEAD.
63         """
64+        self._session_save()
65         parsed = urlparse(path)
66         r = {
67             'CONTENT_TYPE':    'text/html; charset=utf-8',
68@@ -338,6 +354,7 @@ class Client(object):
69         """
70         Request a response from the server using OPTIONS.
71         """
72+        self._session_save()
73         parsed = urlparse(path)
74         r = {
75             'PATH_INFO':       urllib.unquote(parsed[2]),
76@@ -357,6 +374,7 @@ class Client(object):
77         """
78         Send a resource to the server using PUT.
79         """
80+        self._session_save()
81         if content_type is MULTIPART_CONTENT:
82             post_data = encode_multipart(BOUNDARY, data)
83         else:
84@@ -382,6 +400,7 @@ class Client(object):
85         """
86         Send a DELETE request to the server.
87         """
88+        self._session_save()
89         parsed = urlparse(path)
90         r = {
91             'PATH_INFO':       urllib.unquote(parsed[2]),
92@@ -404,6 +423,7 @@ class Client(object):
93         are incorrect, or the user is inactive, or if the sessions framework is
94         not available.
95         """
96+        self._session_save()
97         user = authenticate(**credentials)
98         if user and user.is_active \
99                 and 'django.contrib.sessions' in settings.INSTALLED_APPS:
100@@ -442,6 +462,7 @@ class Client(object):
101 
102         Causes the authenticated user to be logged out.
103         """
104+        self._session_cache = None
105         session = import_module(settings.SESSION_ENGINE).SessionStore()
106         session_cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
107         if session_cookie: