Merge branch 'edumed' of github.com:fnp/redakcja into edumed
[redakcja.git] / apps / filebrowser / decorators.py
1 # -*- coding: utf-8
2
3 from django.contrib.sessions.models import Session
4 from django.shortcuts import get_object_or_404, render_to_response
5 from django.contrib.auth.models import User
6 from django.template import RequestContext
7 from django.conf import settings
8
9
10 def flash_login_required(function):
11     """
12     Decorator to recognize a user  by its session.
13     Used for Flash-Uploading.
14     """
15
16     def decorator(request, *args, **kwargs):
17         try:
18             engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
19         except:
20             import django.contrib.sessions.backends.db
21             engine = django.contrib.sessions.backends.db
22         session_data = engine.SessionStore(request.POST.get('session_key'))
23         user_id = session_data['_auth_user_id']
24         #will return 404 if the session ID does not resolve to a valid user
25         request.user = get_object_or_404(User, pk=user_id)
26         return function(request, *args, **kwargs)
27     return decorator