本文整理汇总了Python中rest_framework.status.HTTP_400_BAD_REQUEST属性的典型用法代码示例。如果您正苦于以下问题:Python status.HTTP_400_BAD_REQUEST属性的具体用法?Python status.HTTP_400_BAD_REQUEST怎么用?Python status.HTTP_400_BAD_REQUEST使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类rest_framework.status
的用法示例。
在下文中一共展示了status.HTTP_400_BAD_REQUEST属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_delete_update_puppy
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def get_delete_update_puppy(request, pk):
try:
puppy = Puppy.objects.get(pk=pk)
except Puppy.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
# get details of a single puppy
if request.method == 'GET':
serializer = PuppySerializer(puppy)
return Response(serializer.data)
# update details of a single puppy
if request.method == 'PUT':
serializer = PuppySerializer(puppy, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# delete a single puppy
if request.method == 'DELETE':
puppy.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
示例2: get_post_puppies
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def get_post_puppies(request):
# get all puppies
if request.method == 'GET':
puppies = Puppy.objects.all()
serializer = PuppySerializer(puppies, many=True)
return Response(serializer.data)
# insert a new record for a puppy
if request.method == 'POST':
data = {
'name': request.data.get('name'),
'age': int(request.data.get('age')),
'breed': request.data.get('breed'),
'color': request.data.get('color')
}
serializer = PuppySerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例3: exception_handler
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def exception_handler(exc, context):
"""
Returns the response that should be used for any given exception.
Adds support the DRF default to also handle django.core.exceptions.ValidationError
Any unhandled exceptions may return `None`, which will cause a 500 error
to be raised.
"""
response = original_exception_handler(exc, context)
if response:
return response
elif isinstance(exc, DjangoValidationError):
data = {"messages": exc.messages}
set_rollback()
return Response(data, status=status.HTTP_400_BAD_REQUEST)
return None
示例4: add_user
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [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)
示例5: remove_user
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def remove_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 user.groups.filter(pk=group.pk).count() == 0:
return Response(
{"user_id": "User is not in group."}, status=status.HTTP_400_BAD_REQUEST
)
user.groups.remove(group)
return Response(status=status.HTTP_204_NO_CONTENT)
示例6: approve
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [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)
示例7: test_reset_expired
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def test_reset_expired(self):
timestamp = int(time.time())
old_password = 'password1'
new_password = 'eaWrivtig5'
user = self.create_test_user(password=old_password)
with patch('time.time',
side_effect=lambda: timestamp):
signer = ResetPasswordSigner({'user_id': user.pk})
data = signer.get_signed_data()
data['password'] = new_password
request = self.create_post_request(data)
with patch('time.time',
side_effect=lambda: timestamp + 3600 * 24 * 8):
response = self.view_func(request)
self.assert_invalid_response(response, status.HTTP_400_BAD_REQUEST)
user.refresh_from_db()
self.assertTrue(user.check_password(old_password))
示例8: test_expired
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def test_expired(self):
self.setup_user()
timestamp = time.time()
with patch('time.time',
side_effect=lambda: timestamp):
signer = RegisterEmailSigner({
'user_id': self.user.pk,
'email': self.new_email,
})
data = signer.get_signed_data()
request = self.create_post_request(data)
with patch('time.time',
side_effect=lambda: timestamp + 3600 * 24 * 8):
response = self.view_func(request)
self.assert_invalid_response(response, status.HTTP_400_BAD_REQUEST)
self.user.refresh_from_db()
self.assertEqual(self.user.email, self.email)
示例9: stockcode_detail
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def stockcode_detail(request, pk, format=None):
"""
Retrieve, update or delete a snippet instance.
"""
try:
values = SC.objects.get(pk=pk)
except SC.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
serializer = ListingSerializer(values)
return Response(serializer.data)
elif request.method == 'PUT':
serializer = ListingSerializer(values, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
values.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
示例10: ZXG_detail
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def ZXG_detail(request, pk, format=None):
"""
Retrieve, update or delete a snippet instance.
"""
try:
values = BlockDetail.objects.get(pk=pk)
except BlockDetail.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
serializer = BKDetailSerializer(values)
return Response(serializer.data)
elif request.method == 'PUT':
serializer = BKDetailSerializer(values, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
values.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
示例11: BK_detail
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def BK_detail(request, pk, format=None):
"""
Retrieve, update or delete a snippet instance.
"""
try:
values = BlockDetail.objects.get(pk=pk)
except BlockDetail.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
serializer = BKDetailSerializer(values)
return Response(serializer.data)
elif request.method == 'PUT':
serializer = BKDetailSerializer(values, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
values.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
# from .hsgtcgholdview import HSGTCG
示例12: test_post_invalid_fields
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def test_post_invalid_fields(self):
data_n = Data.objects.count()
self.data["collection"] = {"id": 42}
resp = self._post(self.data, self.user1)
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
resp.data["collection"][0],
"Invalid collection value: {'id': 42} - object does not exist.",
)
self.data["collection"] = {"id": 1}
self.data["process"] = {"id": 42}
resp = self._post(self.data, self.user1)
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
str(resp.data["process"][0]),
"Invalid process value: {'id': 42} - object does not exist.",
)
self.assertEqual(Data.objects.count(), data_n)
示例13: test_share_by_email
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def test_share_by_email(self):
assign_perm("view_collection", self.user1, self.collection)
assign_perm("share_collection", self.user1, self.collection)
data = {"users": {"add": {self.user2.email: ["view"]}}}
resp = self._detail_permissions(self.collection.pk, data, self.user1)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertEqual(
UserObjectPermission.objects.filter(user=self.user2).count(), 1
)
# Check if error is raise when trying to share with duplicated email.
data = {"users": {"add": {self.user3.email: ["view"]}}}
resp = self._detail_permissions(self.collection.pk, data, self.user1)
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
resp.data["detail"], "Cannot uniquely determine user: user1@test.com"
)
示例14: slug_exists
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def slug_exists(self, request):
"""Check if given url slug exists.
Check if slug given in query parameter ``name`` exists. Return
``True`` if slug already exists and ``False`` otherwise.
"""
if not request.user.is_authenticated:
return Response(status=status.HTTP_401_UNAUTHORIZED)
if "name" not in request.query_params:
return Response(
{"error": "Query parameter `name` must be given."},
status=status.HTTP_400_BAD_REQUEST,
)
queryset = self.get_queryset()
slug_name = request.query_params["name"]
return Response(queryset.filter(slug__iexact=slug_name).exists())
示例15: create
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_400_BAD_REQUEST [as 别名]
def create(self, request):
"""Create a new product request
"""
variant_id = request.data.get("variant_id", None)
if variant_id is not None:
variant = ProductVariant.objects.get(id=variant_id)
product_request = ProductRequest(variant=variant)
product_request.save()
serializer = self.serializer_class(product_request)
response = Response(data=serializer.data, status=status.HTTP_201_CREATED)
else:
response = Response(
{"message": "Missing 'variant_id'"},
status=status.HTTP_400_BAD_REQUEST)
return response