django-extauth / extauth / session.py

'''
Copyright 2009 H. Lee Moffitt Cancer Center and Research Institute, Inc. 
All rights reserved.

@author: Michael Hall <mhall119@gmail.com>
'''
from django.utils._threading_local import local

_thread_locals = local()

def get_current_user():
    "Return the owner of the current thread"
    if _thread_locals is None:
        raise Exception("Session requires thread local variables")

    from django.contrib.auth.models import AnonymousUser
    user = getattr(_thread_locals, 'user', None)
    if user is None:
        raise Exception("Cannot use None as Session user")
    return user

def get_current_user_id():
    user = get_current_user()
    if (user != None):
        return getattr(user, 'id', 0)
    else:
        return 0
    
def set_current_user(user):
    "Set the owner of the current thread"
    if _thread_locals is None:
        raise Exception("Session requires thread local variables")
    
    if user is None:
        raise Exception("Cannot set None as Session user")
    _thread_locals.user = user

class CaptureRequestUser(object):
    "Middleware that captures the current HTTP request user for ExtAuth permissions"
    
    def process_request(self, request):
        #if hasattr(request, 'user'):
        #    set_current_user(getattr(request, 'user'))
        #else:
        from django.contrib.auth import get_user
        set_current_user(get_user(request))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.