OnlineJudge/account/decorators.py
2015-08-14 10:21:22 +08:00

36 lines
1.1 KiB
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 functools import wraps
from django.http import HttpResponse
from django.shortcuts import render
from utils.shortcuts import error_response, error_page
from .models import User
def login_required(func):
@wraps(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 error_page(request, u"请先登录")
return check
def admin_required(func):
@wraps(func)
def check(*args, **kwargs):
request = args[-1]
if request.user.is_authenticated() and request.user.admin_type:
return func(*args, **kwargs)
if request.is_ajax():
return error_response(u"需要管理员权限")
else:
return error_page(request, u"需要管理员权限")
return check