Skip to content

Instantly share code, notes, and snippets.

@chriskief
Last active December 26, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriskief/7131240 to your computer and use it in GitHub Desktop.
Save chriskief/7131240 to your computer and use it in GitHub Desktop.
import json
from django.http import HttpResponse
from django.utils import timezone
from django.utils.dateformat import format
from django.conf import settings
def login_required_ajax(function=None):
# ensure the user is authenticated to access a certain ajax view
# otherwise return a json object notifying the user access is denied
def _decorator(view_func):
def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated():
return view_func(request, *args, **kwargs)
else:
now = format(timezone.now(), u'U')
# delete all existing kwargs
# as they cause HttpResponse to throw TypeError: __init__() got an unexpected keyword argument
kwargs = {'content_type': 'application/json'}
response = {
'status': '401',
'message': 'Unknown authentication scheme',
'timestamp': now,
'errorcode': settings.API_ERROR_AUTHENTICATION
}
return HttpResponse(status=401, content=json.dumps(response), **kwargs)
return _wrapped_view
if function is None:
return _decorator
else:
return _decorator(function)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment