本文整理汇总了Python中rest_framework.permissions.IsAdminUser方法的典型用法代码示例。如果您正苦于以下问题:Python permissions.IsAdminUser方法的具体用法?Python permissions.IsAdminUser怎么用?Python permissions.IsAdminUser使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.permissions
的用法示例。
在下文中一共展示了permissions.IsAdminUser方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAdminUser [as 别名]
def get_permissions(self):
"""Define custom permissions for different methods"""
# at minimum require users to be authenticated
self.permission_classes = [IsAuthenticated]
# for PUT requests require users to be admins
if self.request.method == 'PUT':
self.permission_classes.append(IsAdminUser)
return super(viewsets.ViewSet, self).get_permissions()
示例2: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAdminUser [as 别名]
def get_permissions(self):
if self.action == 'list' or self.action == 'retrieve':
permission_classes = [permissions.IsAuthenticated]
else:
permission_classes = [permissions.IsAdminUser]
return [permission() for permission in permission_classes]
示例3: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAdminUser [as 别名]
def get_permissions(self):
if self.action == 'partial_update' or self.action == 'update':
permission_classes = [permissions.IsAdminUser]
elif self.action == 'list' or self.action == 'retrieve':
permission_classes = [permissions.IsAuthenticated]
else:
permission_classes = [DenyAny]
return [permission() for permission in permission_classes]
示例4: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAdminUser [as 别名]
def get_permissions(self):
if self.request.method == 'GET':
permission_classes = [IsAuthenticated, ]
else:
permission_classes = [IsAdminUser, ]
return [permission() for permission in permission_classes]
示例5: create
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAdminUser [as 别名]
def create(self, request: Request) -> Response:
"""Creates a recipe."""
self.logger.debug("Creating recipe")
# Initialize permission classes
permission_classes = [IsAuthenticated, IsAdminUser]
# Get recipe json
try:
request_dict = request.data.dict()
recipe_json = request_dict["json"]
except KeyError as e:
message = "Unable to create recipe, {} is required".format(e)
return Response({"message": message}, 400)
# Get recipe manager
app_config = apps.get_app_config(APP_NAME)
recipe_manager = app_config.coordinator.recipe
# Create recipe
message, status = recipe_manager.create_recipe(recipe_json)
# Build response
response = {"message": message}
# Return response
self.logger.debug("Returning response: {}".format(response))
return Response(response, status)
示例6: has_permission
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAdminUser [as 别名]
def has_permission(self, request, view):
if request.method in SAFE_METHODS:
return True
return IsAdminUser().has_permission(request, view)
示例7: test_viewset_get_queryset_with_IsAdminUser_permission
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import IsAdminUser [as 别名]
def test_viewset_get_queryset_with_IsAdminUser_permission(self):
from rest_framework.permissions import IsAdminUser
setattr(self.view, "permission_classes", (IsAdminUser,))
request = factory.get(path="/", data="", content_type="application/json")
force_authenticate(request, user=self.user)
response = self.view.as_view(actions={"get": "list"})(request)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
force_authenticate(request, user=self.admin_user)
response = self.view.as_view(actions={"get": "list"})(request)
self.assertEqual(response.status_code, status.HTTP_200_OK)