Login

Cookieless Session Decorator

Author:
achimnol
Posted:
August 5, 2009
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

Although many people have already posted cookieless session middlewares and related stuffs, but this one is just for only required views.

You can use this as a view decorator like:

@session_from_http_params
@login_required
def your_view(request):
     ...

This is very useful for those who use SWFUpload. Flash has a bug with sending cookies properly, so SWFUpload offers an workaround -- session key as a POST parameter.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# -*- coding: utf-8 -*-

from functools import wraps
from django.conf import settings
from django.utils.importlib import import_module

def session_from_http_params(view_func):
    @wraps(view_func)
    def decorated(request, *args, **kwargs):
        engine = import_module(settings.SESSION_ENGINE)
        session_key = request.GET.get(settings.SESSION_COOKIE_NAME, None)
        if session_key is None:
            session_key = request.POST.get(settings.SESSION_COOKIE_NAME, None)
        request.session = engine.SessionStore(session_key)
        return view_func(request, *args, **kwargs)
    return decorated

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.