Commit 3f1ac960 by benjaoming

JSON view can return HttpResponseRedirect

parent 6a9539de
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseNotFound, \ from django.http import HttpResponse, HttpResponseNotFound, \
HttpResponseForbidden HttpResponseForbidden
from django.http import HttpResponseRedirectBase
from django.shortcuts import redirect, get_object_or_404 from django.shortcuts import redirect, get_object_or_404
from django.template.context import RequestContext from django.template.context import RequestContext
from django.template.loader import render_to_string from django.template.loader import render_to_string
...@@ -15,6 +16,11 @@ from django.utils.http import urlquote ...@@ -15,6 +16,11 @@ from django.utils.http import urlquote
def json_view(func): def json_view(func):
def wrap(request, *args, **kwargs): def wrap(request, *args, **kwargs):
obj = func(request, *args, **kwargs) obj = func(request, *args, **kwargs)
if isinstance(obj, HttpResponseRedirectBase):
# Special behaviour: If it's a redirect, for instance
# because of login protection etc. just return
# the redirect
return obj
data = json.dumps(obj, ensure_ascii=False) data = json.dumps(obj, ensure_ascii=False)
status = kwargs.get('status', 200) status = kwargs.get('status', 200)
response = HttpResponse(mimetype='application/json', status=status) response = HttpResponse(mimetype='application/json', status=status)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment