Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Upgrade session fixing to be compatible with 1.4

Thanks to @brosner for the utility function that
fixes this to remain backwards compatible with
Django 1.3
  • Loading branch information...
commit 12cde2784317f811225fe738af37e477241449eb 1 parent 8194536
@paltman paltman authored
Showing with 21 additions and 2 deletions.
  1. +18 −0 anafero/utils.py
  2. +3 −2 anafero/views.py
View
18 anafero/utils.py
@@ -10,3 +10,21 @@ def _generate_code():
while referral_class.objects.filter(code=code).exists():
code = _generate_code()
return code
+
+
+def ensure_session_key(request):
+ """
+ Given a request return a session key that will be used. There may already
+ be a session key associated, but if there is not, we force the session to
+ create itself and persist between requests for the client behind the given
+ request.
+ """
+ key = request.session.session_key
+ if key is None:
+ # @@@ Django forces us to handle session key collision amongst
+ # multiple processes (not handled)
+ request.session.save()
+ # force session to persist for client
+ request.session.modified = True
+ key = request.session.session_key
+ return key
View
5 anafero/views.py
@@ -9,6 +9,7 @@
from django.contrib.contenttypes.models import ContentType
from anafero.models import Referral
+from anafero.utils import ensure_session_key
@login_required
@@ -47,12 +48,12 @@ def create_referral(request):
def process_referral(request, code):
referral = get_object_or_404(Referral, code=code)
referral.respond(request, "RESPONDED")
- request.session["anafero-tracking"] = True
+ session_key = ensure_session_key(request)
response = redirect(referral.redirect_to)
if request.user.is_anonymous():
response.set_cookie(
"anafero-referral",
- "%s:%s" % (code, request.session.session_key)
+ "%s:%s" % (code, session_key)
)
else:
response.delete_cookie("anafero-referral")
Please sign in to comment.
Something went wrong with that request. Please try again.