mirror of
https://github.com/django/django.git
synced 2025-10-26 07:06:08 +00:00
[soc2009/multidb] Merged up to trunk r11756.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11758 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -8,6 +8,8 @@ RequestContext.
|
||||
"""
|
||||
|
||||
from django.conf import settings
|
||||
from django.middleware.csrf import get_token
|
||||
from django.utils.functional import lazy, memoize, SimpleLazyObject
|
||||
|
||||
def auth(request):
|
||||
"""
|
||||
@@ -17,17 +19,46 @@ def auth(request):
|
||||
If there is no 'user' attribute in the request, uses AnonymousUser (from
|
||||
django.contrib.auth).
|
||||
"""
|
||||
if hasattr(request, 'user'):
|
||||
user = request.user
|
||||
else:
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
user = AnonymousUser()
|
||||
# If we access request.user, request.session is accessed, which results in
|
||||
# 'Vary: Cookie' being sent in every request that uses this context
|
||||
# processor, which can easily be every request on a site if
|
||||
# TEMPLATE_CONTEXT_PROCESSORS has this context processor added. This kills
|
||||
# the ability to cache. So, we carefully ensure these attributes are lazy.
|
||||
# We don't use django.utils.functional.lazy() for User, because that
|
||||
# requires knowing the class of the object we want to proxy, which could
|
||||
# break with custom auth backends. LazyObject is a less complete but more
|
||||
# flexible solution that is a good enough wrapper for 'User'.
|
||||
def get_user():
|
||||
if hasattr(request, 'user'):
|
||||
return request.user
|
||||
else:
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
return AnonymousUser()
|
||||
|
||||
return {
|
||||
'user': user,
|
||||
'messages': user.get_and_delete_messages(),
|
||||
'perms': PermWrapper(user),
|
||||
'user': SimpleLazyObject(get_user),
|
||||
'messages': lazy(memoize(lambda: get_user().get_and_delete_messages(), {}, 0), list)(),
|
||||
'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(),
|
||||
}
|
||||
|
||||
def csrf(request):
|
||||
"""
|
||||
Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
|
||||
it has not been provided by either a view decorator or the middleware
|
||||
"""
|
||||
def _get_val():
|
||||
token = get_token(request)
|
||||
if token is None:
|
||||
# In order to be able to provide debugging info in the
|
||||
# case of misconfiguration, we use a sentinel value
|
||||
# instead of returning an empty dict.
|
||||
return 'NOTPROVIDED'
|
||||
else:
|
||||
return token
|
||||
_get_val = lazy(_get_val, str)
|
||||
|
||||
return {'csrf_token': _get_val() }
|
||||
|
||||
def debug(request):
|
||||
"Returns context variables helpful for debugging."
|
||||
context_extras = {}
|
||||
@@ -79,7 +110,7 @@ class PermWrapper(object):
|
||||
|
||||
def __getitem__(self, module_name):
|
||||
return PermLookupDict(self.user, module_name)
|
||||
|
||||
|
||||
def __iter__(self):
|
||||
# I am large, I contain multitudes.
|
||||
raise TypeError("PermWrapper is not iterable.")
|
||||
|
||||
Reference in New Issue
Block a user