Ticket #2548: session_middleware.13.diff
File session_middleware.13.diff, 7.9 KB (added by , 17 years ago) |
---|
-
AUTHORS
229 229 Karen Tracey <graybark@bellsouth.net> 230 230 Makoto Tsuyuki <mtsuyuki@gmail.com> 231 231 tt@gurgle.no 232 Amit Upadhyay 232 Amit Upadhyay <http://www.amitu.com/blog/> 233 233 Geert Vanderkelen 234 234 viestards.lists@gmail.com 235 235 Milton Waddams -
django/contrib/sessions/middleware.py
48 48 def delete_test_cookie(self): 49 49 del self[TEST_COOKIE_NAME] 50 50 51 def get_session_age(self): 52 expires_on = self.get('_session_expires_on') 53 if not expires_on: return settings.SESSION_COOKIE_AGE 54 delta = expires_on - datetime.datetime.now() 55 return delta.days * 24 * 60 * 60 + delta.seconds 56 57 def set_session_age(self, seconds): 58 " Sets the life of the session, irrespective of global settings. " 59 if not seconds: 60 try: 61 del self['_session_expires_on'] 62 except KeyError: 63 pass 64 else: 65 self['_session_expires_on'] = datetime.datetime.now() + datetime.timedelta(seconds=seconds) 66 67 def get_expire_at_browser_close(self): 68 return settings.SESSION_EXPIRE_AT_BROWSER_CLOSE and ('_session_expires_on' not in self) 69 51 70 def _get_session(self): 52 71 # Lazily loads session from storage. 53 72 self.accessed = True … … 92 111 obj = Session.objects.get_new_session_object() 93 112 session_key = obj.session_key 94 113 95 if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE: 96 max_age = None 97 expires = None 98 else: 99 max_age = settings.SESSION_COOKIE_AGE 100 expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE), "%a, %d-%b-%Y %H:%M:%S GMT") 101 new_session = Session.objects.save(session_key, request.session._session, 102 datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE)) 103 response.set_cookie(settings.SESSION_COOKIE_NAME, session_key, 104 max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, 114 session_age = request.session.get_session_age() 115 delta_age = datetime.timedelta(seconds=session_age) 116 if request.session.get_expire_at_browser_close(): 117 session_age = None 118 expires = None 119 else: 120 expires = datetime.datetime.strftime(datetime.datetime.utcnow() + delta_age, "%a, %d-%b-%Y %H:%M:%S GMT") 121 Session.objects.save(session_key, request.session._session, datetime.datetime.now() + delta_age) 122 response.set_cookie( 123 settings.SESSION_COOKIE_NAME, session_key, 124 max_age=session_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, 105 125 secure=settings.SESSION_COOKIE_SECURE or None) 106 126 return response -
django/contrib/sessions/tests.py
10 10 11 11 >>> s.pop('some key', 'does not exist') 12 12 'does not exist' 13 14 ######################## 15 # session_age settings # 16 ######################## 17 18 >>> from django.conf import settings 19 >>> s.get_session_age() == settings.SESSION_COOKIE_AGE 20 True 21 22 # Custom session age 23 >>> s.set_session_age(10) 24 >>> s.get_session_age() in (9, 10) 25 True 26 27 # Set back to default session age 28 >>> s.set_session_age(0) 29 >>> s.get_session_age() == settings.SESSION_COOKIE_AGE 30 True 31 32 # Allow to set back to default session age even if no alternate has been set 33 >>> s.set_session_age(0) 34 35 ############################### 36 # get_expire_at_browser_close # 37 ############################### 38 39 # A bit hackish: we're changing the setting then reverting back to 40 # the original setting just in case at the end of these tests. 41 >>> original_setting = settings.SESSION_EXPIRE_AT_BROWSER_CLOSE 42 43 >>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = False 44 45 # Custom session age 46 >>> s.set_session_age(10) 47 >>> s.get_expire_at_browser_close() 48 False 49 50 # Default session age 51 >>> s.set_session_age(0) 52 >>> s.get_expire_at_browser_close() 53 False 54 55 >>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True 56 57 # Custom session age 58 >>> s.set_session_age(10) 59 >>> s.get_expire_at_browser_close() 60 False 61 62 # Default session age 63 >>> s.set_session_age(0) 64 >>> s.get_expire_at_browser_close() 65 True 66 67 >>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = original_setting 13 68 """ 14 69 15 70 from django.contrib.sessions.middleware import SessionWrapper -
docs/sessions.txt
39 39 It implements the following standard dictionary methods: 40 40 41 41 * ``__getitem__(key)`` 42 42 43 Example: ``fav_color = request.session['fav_color']`` 43 44 44 45 * ``__setitem__(key, value)`` 46 45 47 Example: ``request.session['fav_color'] = 'blue'`` 46 48 47 49 * ``__delitem__(key)`` 50 48 51 Example: ``del request.session['fav_color']``. This raises ``KeyError`` 49 52 if the given ``key`` isn't already in the session. 50 53 51 54 * ``__contains__(key)`` 55 52 56 Example: ``'fav_color' in request.session`` 53 57 54 58 * ``get(key, default=None)`` 59 55 60 Example: ``fav_color = request.session.get('fav_color', 'red')`` 56 61 57 62 * ``keys()`` 58 63 59 64 * ``items()`` 60 65 61 It also has these threemethods:66 It also has these methods: 62 67 63 68 * ``set_test_cookie()`` 69 64 70 Sets a test cookie to determine whether the user's browser supports 65 71 cookies. Due to the way cookies work, you won't be able to test this 66 72 until the user's next page request. See "Setting test cookies" below for 67 73 more information. 68 74 69 75 * ``test_cookie_worked()`` 76 70 77 Returns either ``True`` or ``False``, depending on whether the user's 71 78 browser accepted the test cookie. Due to the way cookies work, you'll 72 79 have to call ``set_test_cookie()`` on a previous, separate page request. 73 80 See "Setting test cookies" below for more information. 74 81 75 82 * ``delete_test_cookie()`` 83 76 84 Deletes the test cookie. Use this to clean up after yourself. 77 85 86 * ``set_session_age(seconds)`` 87 88 It takes an integer, number of seconds for which the session should be 89 valid. This will override the default/global session expiry policy. 90 See `browser-length sessions vs. persistent sessions`_ for default 91 session expiry policy. To fall back to the global session expiry policy 92 again, call ``set_session_age(0)``. 93 94 * ``get_session_age()`` 95 96 Returns the age of the session (which will equal 97 ``settings.SESSION_COOKIE_AGE`` unless it has been overridden by 98 ``set_session_age``). 99 100 * ``get_expire_at_browser_close()`` 101 102 Returns either ``True`` or ``False``, depending on whether this session 103 expires at browser close (which will equal 104 ``settings.SESSION_EXPIRE_AT_BROWSER_CLOSE`` unless the session age 105 hase been overridden by ``set_session_age``). 106 78 107 You can edit ``request.session`` at any point in your view. You can edit it 79 108 multiple times. 80 109 … … 217 246 her browser. Use this if you want people to have to log in every time they open 218 247 a browser. 219 248 249 These settings are global defaults, and can be overwritten by explicitly calling 250 ``request.session.set_session_age()`` as described above in `Using sessions in views`_. 251 220 252 Clearing the session table 221 253 ========================== 222 254