本文整理汇总了Python中rest_framework.status.HTTP_403_FORBIDDEN属性的典型用法代码示例。如果您正苦于以下问题:Python status.HTTP_403_FORBIDDEN属性的具体用法?Python status.HTTP_403_FORBIDDEN怎么用?Python status.HTTP_403_FORBIDDEN使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类rest_framework.status
的用法示例。
在下文中一共展示了status.HTTP_403_FORBIDDEN属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_user
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def add_user(self, request, pk=None):
group = self.get_object()
if not request.data.get("user_id"):
return Response(
{"user_id": "This field is required."}, status=status.HTTP_400_BAD_REQUEST
)
try:
user = User.objects.get(pk=request.data.get("user_id"))
except User.DoesNotExist:
return Response({"user_id": "Invalid user ID."}, status=status.HTTP_400_BAD_REQUEST)
if request.user == user:
return Response(status=status.HTTP_403_FORBIDDEN)
user.groups.add(group)
return Response(status=status.HTTP_204_NO_CONTENT)
示例2: approve
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def approve(self, request, pk=None):
approval_request = self.get_object()
if not request.data.get("comment"):
return Response(
{"comment": "This field is required."}, status=status.HTTP_400_BAD_REQUEST
)
try:
approval_request.approve(approver=request.user, comment=request.data.get("comment"))
except ApprovalRequest.NotActionable:
return Response(
{"error": "This approval request has already been approved or rejected."},
status=status.HTTP_400_BAD_REQUEST,
)
except ApprovalRequest.CannotActOnOwnRequest:
return Response(
{"error": "You cannot approve your own approval request."},
status=status.HTTP_403_FORBIDDEN,
)
return Response(ApprovalRequestSerializer(approval_request).data)
示例3: reject
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def reject(self, request, pk=None):
approval_request = self.get_object()
if not request.data.get("comment"):
return Response(
{"comment": "This field is required."}, status=status.HTTP_400_BAD_REQUEST
)
try:
approval_request.reject(approver=request.user, comment=request.data.get("comment"))
except ApprovalRequest.NotActionable:
return Response(
{"error": "This approval request has already been approved or rejected."},
status=status.HTTP_400_BAD_REQUEST,
)
except ApprovalRequest.CannotActOnOwnRequest:
return Response(
{"error": "You cannot reject your own approval request."},
status=status.HTTP_403_FORBIDDEN,
)
return Response(ApprovalRequestSerializer(approval_request).data)
示例4: test_public_user
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_public_user(self):
"""Public user cannot create/edit anything"""
assign_perm("view_collection", self.user1, self.collection)
assign_perm("share_collection", self.user1, self.collection)
data = {"public": {"add": ["view"]}}
resp = self._detail_permissions(self.collection.pk, data, self.user1)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
data = {"public": {"add": ["edit"]}}
resp = self._detail_permissions(self.collection.pk, data, self.user1)
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
data = {"public": {"remove": ["edit"]}}
resp = self._detail_permissions(self.collection.pk, data, self.user1)
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
示例5: test_token_register_api
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_token_register_api(self):
url = api_reverse('api-auth:login')
data = {
'username': 'cfe',
'password': 'yeahhhcfe',
}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK) # 400
token = response.data.get("token", None)
self.client.credentials(HTTP_AUTHORIZATION='JWT ' + token)
url2 = api_reverse('api-auth:register')
data2 = {
'username': 'cfe.doe',
'email': 'cfe.doe@gmail.com',
'password': 'learncode',
'password2': 'learncode'
}
response = self.client.post(url2, data2, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # 403
示例6: test_other_user_permissions_api
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_other_user_permissions_api(self):
data = self.create_item()
data_id = data.get("id")
user = User.objects.create(username='testjmitch')
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
self.client.credentials(HTTP_AUTHORIZATION='JWT ' + token)
rud_url = api_reverse('api-status:detail', kwargs={"id": data_id})
rud_data = {
'content': "smashing"
}
get_ = self.client.get(rud_url, format='json')
put_ = self.client.put(rud_url, rud_data, format='json')
delete_ = self.client.delete(rud_url, format='json')
self.assertEqual(get_.status_code, status.HTTP_200_OK)
self.assertEqual(put_.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(delete_.status_code, status.HTTP_403_FORBIDDEN)
示例7: test_regular_user_bulk_create
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_regular_user_bulk_create(self):
self.org_1.regular_users.add(self.user)
url = reverse('event-list')
location_id = reverse('place-detail', kwargs={'pk': self.place.id})
data_1 = self.make_complex_event_dict(self.system_data_source, self.org_1, location_id, self.languages)
data_1['name']['fi'] = 'event-data-1'
data_1['publication_status'] = 'public'
data_2 = deepcopy(data_1)
data_2['name']['fi'] = 'event-data-2'
data_2['publication_status'] = 'draft'
self.client.force_authenticate(self.user)
response = self.client.post(url, [data_1, data_2], format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
# test creating multiple draft events
data_1['publication_status'] = 'draft'
self.client.force_authenticate(self.user)
response = self.client.post(url, [data_1, data_2], format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
qs = Event.objects.filter(name_fi__in=['event-data-1', 'event-data-2'])
self.assertEqual(qs.count(), 2)
示例8: test_cannot_get_conversation_as_nonmember
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_cannot_get_conversation_as_nonmember(self):
self.client.force_login(user=self.user)
# cannot get via activity
response = self.client.get(self.conversation_url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
# cannot get via conversation info
conversation_id = self.activity.conversation.id
response = self.client.get('/api/conversations/{}/'.format(conversation_id))
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
# cannot write a message
conversation_id = self.activity.conversation.id
response = self.client.post('/api/messages/', {
'conversation': conversation_id,
'content': 'hey',
})
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data)
self.assertEqual(response.data['detail'], 'You are not in this conversation')
示例9: destroy
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
# Should not be able to delete self
if request.user == instance:
return Response(status=status.HTTP_403_FORBIDDEN)
return super().destroy(request, *args, **kwargs)
示例10: test_not_logged_in
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_not_logged_in(self):
request = self.create_post_request()
self.add_session_to_request(request)
response = self.view_func(request)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
示例11: test_forbidden
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_forbidden(self):
request = self.create_post_request({})
response = self.view_func(request)
self.assert_invalid_response(response, status.HTTP_403_FORBIDDEN)
示例12: test_patch_public_user
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_patch_public_user(self):
data = {"name": "New collection"}
resp = self._patch(3, data)
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
p = Collection.objects.get(pk=3)
self.assertEqual(p.name, "Test collection 3")
示例13: test_delete_public_user
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_delete_public_user(self):
resp = self._delete(3)
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
collection_exists = Collection.objects.filter(pk=3).exists()
self.assertTrue(collection_exists)
示例14: test_post_no_perms
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_post_no_perms(self):
collection = Collection.objects.get(pk=1)
remove_perm("edit_collection", self.user2, collection)
data_count = Data.objects.count()
resp = self._post(self.data, self.user2)
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(Data.objects.count(), data_count)
示例15: test_post_public_user
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_403_FORBIDDEN [as 别名]
def test_post_public_user(self):
data_count = Data.objects.count()
resp = self._post(self.data)
# User has no permission to add Data object to the collection.
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(Data.objects.count(), data_count)