add fields

This commit is contained in:
j 2010-01-26 19:25:01 +05:30
parent fe6de58583
commit 1b5ec862af
2 changed files with 15 additions and 10 deletions

View file

@ -1,18 +1,20 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # 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): def login_required_json(function=None):
""" """
Decorator for views that checks that the user is logged in Decorator for views that checks that the user is logged in
return json error if not logged in. return json error if not logged in.
""" """
actual_decorator = user_passes_test(
lambda u: u.is_authenticated(), def _wrapped_view(request, *args, **kwargs):
login_url='/json/login', if request.user.is_authenticated():
) return function(request, *args, **kwargs)
if function: response = render_to_json_response({'status': {'code': 403, 'text': 'login required'}})
return actual_decorator(function) response.status_code = 403
return actual_decorator return response
return wraps(function)(_wrapped_view)

View file

@ -8,10 +8,13 @@ except ImportError:
from django.utils import simplejson from django.utils import simplejson
from django.conf import settings 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 indent=None
if settings.DEBUG: if settings.DEBUG:
content_type = "text/javascript" content_type = "text/javascript"
indent = 2 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)