Decorator for views that checks that the user is logged in, redirecting
    to the log-in page if necessary.

def login_required(function=None, message=None, login_url=None):
    """
    Decorator for views that checks that the user is logged in, redirecting
    to the log-in page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_authenticated(),
        message=message,
        login_url=login_url
    )

    if function:
        return actual_decorator(function)
    return actual_decorator