Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Forked from pindia/gist:9b7787a607a5a676c6d8
Last active November 12, 2015 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-martin/d94380055a76489a277c to your computer and use it in GitHub Desktop.
Save chris-martin/d94380055a76489a277c to your computer and use it in GitHub Desktop.
# 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):
if settings.SESSION_COOKIE_NAME not in request.COOKIES:
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, request, response):
d = settings.OLD_COOKIE_DOMAIN
if d is not None:
response.delete_cookie('sessionid', domain=d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment