"""auth.py"""
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.http import HttpResponse, HttpResponseRedirect

class HttpResponseNotAuthorized(HttpResponse):
    status_code = 401

    def __init__(self, *args, **kwargs):
        HttpResponse.__init__(self, *args, **kwargs)
        self['WWW-Authenticate'] = 'NegotiateNTLM'

def negotiate_ntlm(request,
                   content='You are not authorized to access this website.',
                   redirect_field_name=REDIRECT_FIELD_NAME):

    redirect_to = request.REQUEST.get(redirect_field_name, '/')

    if request.user.is_authenticated():
        return HttpResponseRedirect(redirect_to)
    else:
        return HttpResponseNotAuthorized(content)
