diff --git a/oxdjango/decorators.py b/oxdjango/decorators.py index 1311e99..e616536 100644 --- a/oxdjango/decorators.py +++ b/oxdjango/decorators.py @@ -1,18 +1,20 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 -from django.contrib.auth.decorators import user_passes_test +from django.contrib.auth.decorators import wraps +from shortcuts import render_to_json_response def login_required_json(function=None): """ Decorator for views that checks that the user is logged in return json error if not logged in. """ - actual_decorator = user_passes_test( - lambda u: u.is_authenticated(), - login_url='/json/login', - ) - if function: - return actual_decorator(function) - return actual_decorator + + def _wrapped_view(request, *args, **kwargs): + if request.user.is_authenticated(): + return function(request, *args, **kwargs) + response = render_to_json_response({'status': {'code': 403, 'text': 'login required'}}) + response.status_code = 403 + return response + return wraps(function)(_wrapped_view) diff --git a/oxdjango/shortcuts.py b/oxdjango/shortcuts.py index 1c6913f..7a07819 100644 --- a/oxdjango/shortcuts.py +++ b/oxdjango/shortcuts.py @@ -8,10 +8,13 @@ except ImportError: from django.utils import simplejson from django.conf import settings -def render_to_json_response(dictionary, content_type="text/json"): +def render_to_json_response(dictionary, content_type="text/json", status=200): indent=None if settings.DEBUG: content_type = "text/javascript" indent = 2 - return HttpResponse(simplejson.dumps(dictionary, indent=indent), content_type=content_type) + print simplejson.dumps(dictionary, indent=indent) + if 'status' in dictionary and 'code' in dictionary['status']: + status = dictionary['status']['code'] + return HttpResponse(simplejson.dumps(dictionary, indent=indent), content_type=content_type, status=status)