Ticket #2548: session_middleware.11.diff
File session_middleware.11.diff, 5.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 return self.get('_session_age', settings.SESSION_COOKIE_AGE) 53 def set_session_age(self, seconds): 54 " Sets the life of the session, irrespective of global settings. " 55 if not seconds: 56 del self['_session_age'] 57 else: 58 self['_session_age'] = seconds 59 60 def get_expire_at_browser_close(self): 61 return settings.SESSION_EXPIRE_AT_BROWSER_CLOSE and ('_session_age' not in self) 62 51 63 def _get_session(self): 52 64 # Lazily loads session from storage. 53 65 self.accessed = True … … 92 104 obj = Session.objects.get_new_session_object() 93 105 session_key = obj.session_key 94 106 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, 107 session_age = request.session.get_session_age() 108 delta_age = datetime.timedelta(seconds=session_age) 109 if request.session.get_expire_at_browser_close(): 110 session_age = None 111 expires = None 112 else: 113 expires = datetime.datetime.strftime(datetime.datetime.utcnow() + delta_age, "%a, %d-%b-%Y %H:%M:%S GMT") 114 Session.objects.save(session_key, request.session._session, datetime.datetime.now() + delta_age) 115 response.set_cookie( 116 settings.SESSION_COOKIE_NAME, session_key, 117 max_age=session_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, 105 118 secure=settings.SESSION_COOKIE_SECURE or None) 106 119 return response -
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_expiry_age()`` as described above in `Using sessions in views`_. 251 220 252 Clearing the session table 221 253 ========================== 222 254