You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
from rest_framework import permissions
|
|
|
|
from rest_framework.permissions import BasePermission, SAFE_METHODS
|
|
|
|
|
|
|
|
|
|
|
|
class IsAdminUserOrReadOnly(permissions.BasePermission):
|
|
|
|
"""
|
|
|
|
仅管理员用户可以进行修改
|
|
|
|
其他用户仅可进行查看
|
|
|
|
"""
|
|
|
|
# 每次请求到来时被唤醒执行
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
# 对其他仅允许GET, HEAD, OPTIONS请求
|
|
|
|
if request.method in permissions.SAFE_METHODS:
|
|
|
|
return True
|
|
|
|
# 仅管理员可进行其他操作
|
|
|
|
return request.user.is_superuser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|