python-oxdjango/oxdjango/decorators.py

22 lines
626 B
Python
Raw Normal View History

2009-10-11 12:47:39 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
try:
from django.contrib.auth.decorators import wraps
except:
from django.utils.functional import wraps
2010-01-26 13:55:01 +00:00
from shortcuts import render_to_json_response
2009-10-11 12:47:39 +00:00
def login_required_json(function=None):
"""
Decorator for views that checks that the user is logged in
return json error if not logged in.
"""
2010-01-26 13:55:01 +00:00
def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated():
return function(request, *args, **kwargs)
2010-02-08 15:05:08 +00:00
return render_to_json_response({'status': {'code': 401, 'text': 'login required'}})
2010-01-26 13:55:01 +00:00
return wraps(function)(_wrapped_view)
2009-10-11 12:47:39 +00:00