Esempio n. 1
0
def auth_processor(request):
    """
    Returns context variables required by apps that use Django's authentication
    system.

    If there is no 'user' attribute in the request, uses AnonymousUser (from
    django.contrib.auth).
    """
    if hasattr(request, 'user'):
        user = request.user
        if user.is_authenticated():
            messages = user.message_set.all()
        else:
            messages = None
    else:
        from django.contrib.auth.models import AnonymousUser
        user = AnonymousUser()
        messages = None

    from django.core.context_processors import PermWrapper
    return {
        'user': user,
        'messages': messages,
        'perms': PermWrapper(user),
    }
def auth(request):
    """
    Returns context variables required by apps that use Django's authentication
    system.

    If there is no 'user' attribute in the request, uses AnonymousUser (from
    django.contrib.auth).
    """

    # 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': SimpleLazyObject(get_user),
        'messages': messages.get_messages(request),
        'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(),
    }
Esempio n. 3
0
def auth_without_messages(request):
    """
    A modified version of the bundled authentication context processor
    which does not retrieve per-user messages from the database.

    Be aware that the admin currently does not work when the default
    auth context processor is disabled when running in DEBUG mode.
    You will also loose feedback given with messages in the admin.
    """

    if settings.DEBUG and 'django.core.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS:
        raise ImproperlyConfigured(
            "'django.core.context_processors.auth' can not be used together with 'notices.context_processors.auth_without_messages'"
        )

    if hasattr(request, 'user'):
        user = request.user
    else:
        from django.contrib.auth.models import AnonymousUser
        user = AnonymousUser()
    return {
        'user': user,
        'messages': [],
        'perms': PermWrapper(user),
    }
Esempio n. 4
0
def auth_processor(request):
    if hasattr(request, 'user'):
        user = request.user
    else:
        from django.contrib.auth.models import AnonymousUser
        user = AnonymousUser()

    from django.core.context_processors import PermWrapper
    return {
        'user': user,
        'perms': PermWrapper(user),
    }
Esempio n. 5
0
def auth(request):
    """
    Returns context variables required by apps that use Django's authentication
    system.
    
    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()
    return {'user': user, 'perms': PermWrapper(user)}
Esempio n. 6
0
def auth_processor(request):
    if hasattr(request, 'user'):
        user = request.user
        if user.is_authenticated():
            messages = user.message_set.all()
        else:
            messages = None
    else:
        from django.contrib.auth.models import AnonymousUser
        user = AnonymousUser()
        messages = None

    from django.core.context_processors import PermWrapper
    return {
        'user': user,
        'messages': messages,
        'perms': PermWrapper(user),
    }
Esempio n. 7
0
def get_user(request):
    data = {}
    data['perms'] = PermWrapper(request.user)
    data['user'] = request.user
    #    data.update(csrf(request))
    return data