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 -*-
# 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)

View File

@ -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)