-
-
Save hitme/82f547e704bbd552fdbba35757f7423d to your computer and use it in GitHub Desktop.
Django session cookie migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read more at http://www.pindi.us/blog/migrating-cross-domain-cookies-django | |
from django.conf import settings | |
from importlib import import_module | |
class UpdateSessionCookieMiddleware(object): | |
""" | |
Migrates session data from an old (hardcoded) session cookie name and domain to the name and | |
domain currently defined in the Django settings. | |
Should be placed after SessionMiddleware but before any middleware that uses request.session. | |
""" | |
def process_request(self, request): | |
session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) | |
if session_key is None: | |
old_session_key = request.COOKIES.get('sessionid', None) | |
if old_session_key: | |
engine = import_module(settings.SESSION_ENGINE) | |
old_session = engine.SessionStore(old_session_key) | |
for key, value in old_session.items(): | |
request.session[key] = value | |
request.session.save() | |
def process_response(self, response): | |
response.delete_cookie('sessionid', domain='www.getstudyroom.com') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
process_response
method must return the response.