2 |
2 |
3 from django.conf import settings |
3 from django.conf import settings |
4 from django.utils.cache import patch_vary_headers |
4 from django.utils.cache import patch_vary_headers |
5 from django.utils.http import cookie_date |
5 from django.utils.http import cookie_date |
6 |
6 |
7 TEST_COOKIE_NAME = 'testcookie' |
|
8 TEST_COOKIE_VALUE = 'worked' |
|
9 |
|
10 class SessionMiddleware(object): |
7 class SessionMiddleware(object): |
11 |
|
12 def process_request(self, request): |
8 def process_request(self, request): |
13 engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) |
9 engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) |
14 session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) |
10 session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) |
15 request.session = engine.SessionStore(session_key) |
11 request.session = engine.SessionStore(session_key) |
16 |
12 |
17 def process_response(self, request, response): |
13 def process_response(self, request, response): |
18 # If request.session was modified, or if response.session was set, save |
14 """ |
19 # those changes and set a session cookie. |
15 If request.session was modified, or if the configuration is to save the |
|
16 session every time, save the changes and set a session cookie. |
|
17 """ |
20 try: |
18 try: |
21 accessed = request.session.accessed |
19 accessed = request.session.accessed |
22 modified = request.session.modified |
20 modified = request.session.modified |
23 except AttributeError: |
21 except AttributeError: |
24 pass |
22 pass |
25 else: |
23 else: |
26 if accessed: |
24 if accessed: |
27 patch_vary_headers(response, ('Cookie',)) |
25 patch_vary_headers(response, ('Cookie',)) |
28 if modified or settings.SESSION_SAVE_EVERY_REQUEST: |
26 if modified or settings.SESSION_SAVE_EVERY_REQUEST: |
29 if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE: |
27 if request.session.get_expire_at_browser_close(): |
30 max_age = None |
28 max_age = None |
31 expires = None |
29 expires = None |
32 else: |
30 else: |
33 max_age = settings.SESSION_COOKIE_AGE |
31 max_age = request.session.get_expiry_age() |
34 expires_time = time.time() + settings.SESSION_COOKIE_AGE |
32 expires_time = time.time() + max_age |
35 expires = cookie_date(expires_time) |
33 expires = cookie_date(expires_time) |
36 # Save the seesion data and refresh the client cookie. |
34 # Save the session data and refresh the client cookie. |
37 request.session.save() |
35 request.session.save() |
38 response.set_cookie(settings.SESSION_COOKIE_NAME, |
36 response.set_cookie(settings.SESSION_COOKIE_NAME, |
39 request.session.session_key, max_age=max_age, |
37 request.session.session_key, max_age=max_age, |
40 expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, |
38 expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, |
41 path=settings.SESSION_COOKIE_PATH, |
39 path=settings.SESSION_COOKIE_PATH, |
42 secure=settings.SESSION_COOKIE_SECURE or None) |
40 secure=settings.SESSION_COOKIE_SECURE or None) |
43 |
|
44 return response |
41 return response |