OnlineJudge/account/decorators.py
2015-08-04 13:22:37 +08:00

25 lines
736 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
from django.http import HttpResponse
from django.shortcuts import render
from utils.shortcuts import error_response
from .models import User
def login_required(func):
def check(*args, **kwargs):
# 在class based views 里面args 有两个元素一个是self, 第二个才是request
# 在function based views 里面args 只有request 一个参数
request = args[-1]
if request.user.is_authenticated():
return func(*args, **kwargs)
if request.is_ajax():
return error_response(u"请先登录")
else:
return render(request, "utils/error.html", {"error": u"请先登录"})
return check
def admin_required():
pass