Okay, so the last one there had a hickup, users could not longer
login, once they have logged out. It kept the AnonymousUser cached...
Here is an update which works with logging back in:
class UserPickleMiddleware(object):
def process_request(self, request):
if 'cached_user' not in request.session:
from django.contrib.auth import get_user
user = get_user(request)
if isinstance(user, User):
request.session['cached_user'] = user
if 'cached_user' in request.session:
request._cached_user = request.session['cached_user']
return None
This one makes sure that the user instance is indeed a regular User
and not some Anonymous person before setting the cached data. The
code could be more optimized. I didn't use an Else, because it's
needs to set the request variables. I'd rather write an extra "if"
instead of copying the same line to set the request variable twice.