python-oxdjango/oxdjango/decorators.py
2010-01-26 19:25:01 +05:30

20 lines
614 B
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
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.
"""
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)