Code

Ticket #2548: session_middleware.9.diff

File session_middleware.9.diff, 4.3 KB (added by upadhyay@…, 7 years ago)

[Looks like I am not going to stop embaressing myself today.] Updated the docs too.

Line 
1Index: AUTHORS
2===================================================================
3--- AUTHORS     (revision 5468)
4+++ AUTHORS     (working copy)
5@@ -229,7 +229,7 @@
6     Karen Tracey <graybark@bellsouth.net>
7     Makoto Tsuyuki <mtsuyuki@gmail.com>
8     tt@gurgle.no
9-    Amit Upadhyay
10+    Amit Upadhyay <http://www.amitu.com/blog/>
11     Geert Vanderkelen
12     viestards.lists@gmail.com
13     Milton Waddams
14Index: django/contrib/sessions/middleware.py
15===================================================================
16--- django/contrib/sessions/middleware.py       (revision 5468)
17+++ django/contrib/sessions/middleware.py       (working copy)
18@@ -48,6 +48,13 @@
19     def delete_test_cookie(self):
20         del self[TEST_COOKIE_NAME]
21 
22+    def set_life(self, val):
23+        """
24+            sets the life of the session, irrespective of global settings.
25+            val is in seconds.
26+        """
27+        self["_expires_on"] = TILL_BROWSER_CLOSE
28+
29     def _get_session(self):
30         # Lazily loads session from storage.
31         self.accessed = True
32@@ -92,15 +99,20 @@
33                     obj = Session.objects.get_new_session_object()
34                     session_key = obj.session_key
35 
36-                if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE:
37-                    max_age = None
38-                    expires = None
39-                else:
40-                    max_age = settings.SESSION_COOKIE_AGE
41-                    expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE), "%a, %d-%b-%Y %H:%M:%S GMT")
42-                new_session = Session.objects.save(session_key, request.session._session,
43-                    datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE))
44-                response.set_cookie(settings.SESSION_COOKIE_NAME, session_key,
45+                now = datetime.datetime.now()
46+                delta_yield = now + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE)
47+                if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE and not request.session.get("_expires_on"):
48+                    max_age = None
49+                    expires = None
50+                else:
51+                    delta_yield = request.session.get("_expires_on", delta_yield)
52+                    max_age_timedelta = delta_yield - now
53+                    max_age = max_age_timedelta.days * 24 * 60 * 60 + max_age_timedelta.seconds
54+                    expires = datetime.datetime.strftime(delta_yield, "%a, %d-%b-%Y %H:%M:%S GMT")
55+                Session.objects.save(session_key, request.session._session, delta_yield)
56+                response.set_cookie(
57+                    settings.SESSION_COOKIE_NAME, session_key,
58                     max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
59-                    secure=settings.SESSION_COOKIE_SECURE or None)
60+                    secure=settings.SESSION_COOKIE_SECURE or None
61+                )
62         return response
63Index: docs/sessions.txt
64===================================================================
65--- docs/sessions.txt   (revision 5468)
66+++ docs/sessions.txt   (working copy)
67@@ -58,7 +58,7 @@
68 
69     * ``items()``
70 
71-It also has these three methods:
72+It also has these four methods:
73 
74     * ``set_test_cookie()``
75       Sets a test cookie to determine whether the user's browser supports
76@@ -75,6 +75,14 @@
77     * ``delete_test_cookie()``
78       Deletes the test cookie. Use this to clean up after yourself.
79 
80+    * ``set_life(val)``
81+      It takes an integer, number of seconds for which the session should be
82+      valid. It can also take django.contrib.session.models.TILL_BROWSER_CLOSE,
83+      that will quell the session at the close of browser. Calling set_life
84+      will overwrite the global/default session expiry policy. See
85+      `browser-length sessions vs. persistent sessions` for default session expiry
86+      policy.
87+
88 You can edit ``request.session`` at any point in your view. You can edit it
89 multiple times.
90 
91@@ -217,6 +225,9 @@
92 her browser. Use this if you want people to have to log in every time they open
93 a browser.
94 
95+These settings are global defaults, and can be overwritten by explicitly calling
96+``request.session.set_life()`` as described above.
97+
98 Clearing the session table
99 ==========================
100