本文整理汇总了Python中rest_framework.status.HTTP_401_UNAUTHORIZED属性的典型用法代码示例。如果您正苦于以下问题:Python status.HTTP_401_UNAUTHORIZED属性的具体用法?Python status.HTTP_401_UNAUTHORIZED怎么用?Python status.HTTP_401_UNAUTHORIZED使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类rest_framework.status
的用法示例。
在下文中一共展示了status.HTTP_401_UNAUTHORIZED属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: slug_exists
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [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())
示例2: test_delegate_jwti_inactive_user
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def test_delegate_jwti_inactive_user(self):
data = {
'client_id': 'gandolf',
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'refresh_token': self.token1.key,
'api_type': 'app',
}
self.user1.is_active = False
self.user1.save()
response = self.client.post(self.delegate_url,
data=data,
format='json')
self.assertEqual(
response.status_code,
status.HTTP_401_UNAUTHORIZED,
(response.status_code, response.content)
)
开发者ID:lock8,项目名称:django-rest-framework-jwt-refresh-token,代码行数:19,代码来源:test_long_refresh_token_views.py
示例3: test_api_token_auth
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def test_api_token_auth(self):
username, password = 'admin', 'mypass'
# No user created
response = self.client.post(reverse('v1:api-token-auth'), format='json', data={'username': username,
'password': password})
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
# Create user
User.objects.create_superuser(username, 'admin@admin.com', password)
response = self.client.post(reverse('v1:api-token-auth'), format='json', data={'username': username,
'password': password})
self.assertEqual(response.status_code, status.HTTP_200_OK)
token = response.json()['token']
# Test protected endpoint
response = self.client.get(reverse('v1:private-safes'))
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
response = self.client.get(reverse('v1:private-safes'), HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.status_code, status.HTTP_200_OK)
示例4: get_feedback
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def get_feedback(request, feedback_id):
"""
Returns the feedback pertaining to a certain feedback id
:param request:
:return: 401 if authorization failed
:return: 404 if not found
:return: 200 successful
"""
try:
user_feedback = Feedback.objects.get(pk=feedback_id)
if request.user is not user_feedback.user:
return Response(status=status.HTTP_401_UNAUTHORIZED)
except Feedback.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = FeedbackCondensedSerializer(user_feedback)
return Response(serializer.data)
示例5: get_trip
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def get_trip(request, trip_id):
"""
Returns a trip using 'trip_id'
:param request:
:param trip_id:
:return: 401 if user is not a member of this specific trip and trip is private
:return: 404 if invalid trip id is sent
:return: 200 successful
"""
try:
trip = Trip.objects.get(pk=trip_id)
if request.user not in trip.users.all() and not trip.is_public:
return Response(status=status.HTTP_401_UNAUTHORIZED)
except Trip.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = TripSerializer(trip)
return Response(serializer.data)
示例6: update_trip_name
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def update_trip_name(request, trip_id, trip_name):
"""
:param request:
:param trip_id:
:param trip_name:
:return: 400 if user not present in the trip
:return: 404 if trip or user does not exist
:return: 200 successful
"""
try:
trip = Trip.objects.get(id=trip_id)
if request.user not in trip.users.all():
return Response(status=status.HTTP_401_UNAUTHORIZED)
trip.trip_name = trip_name
trip.save(update_fields=['trip_name'])
except Trip.DoesNotExist:
error_message = "Trip does not exist"
return Response(error_message, status=status.HTTP_404_NOT_FOUND)
except Exception as e:
return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_200_OK)
示例7: update_trip_public
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def update_trip_public(request, trip_id):
"""
Makes given trip public
:param request:
:param trip_id:
:return: 400 if user not present in the trip
:return: 404 if trip or user does not exist
:return: 200 successful
"""
try:
trip = Trip.objects.get(pk=trip_id)
# if signed-in user not associated with requested trip
if request.user not in trip.users.all():
error_message = "User not a part of trip"
return Response(error_message, status=status.HTTP_401_UNAUTHORIZED)
trip.is_public = True
trip.save()
except Trip.DoesNotExist:
error_message = "Trip does not exist"
return Response(error_message, status=status.HTTP_404_NOT_FOUND)
return Response(status=status.HTTP_200_OK)
示例8: update_trip_private
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def update_trip_private(request, trip_id):
"""
Makes given trip private
:param request:
:param trip_id:
:return: 400 if user not present in the trip
:return: 404 if trip or user does not exist
:return: 200 successful
"""
try:
trip = Trip.objects.get(pk=trip_id)
# if signed-in user not associated with requested trip
if request.user not in trip.users.all():
error_message = "User not a part of trip"
return Response(error_message, status=status.HTTP_401_UNAUTHORIZED)
trip.is_public = False
trip.save()
except Trip.DoesNotExist:
error_message = "Trip does not exist"
return Response(error_message, status=status.HTTP_404_NOT_FOUND)
return Response(status=status.HTTP_200_OK)
示例9: mark_notification_as_read
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def mark_notification_as_read(request, notification_id):
"""
Mark notification as read
:param request:
:param notification_id:
:return 200 successful
"""
try:
notification = Notification.objects.get(id=notification_id)
if request.user != notification.destined_user:
return Response(status=status.HTTP_401_UNAUTHORIZED)
notification.is_read = True
notification.save()
except Notification.DoesNotExist:
error_message = "Notification does not exist"
return Response(error_message, status=status.HTTP_404_NOT_FOUND)
success_message = "Successfully marked notification as read."
return Response(success_message, status=status.HTTP_200_OK)
示例10: with_requests_mock
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def with_requests_mock(allowed):
def inner(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
tmp_file = kwargs['tmp_file']
filename = kwargs['filename']
requests_response = requests.Response()
if allowed:
requests_response.raw = tmp_file
requests_response.headers['Content-Disposition'] = f'attachment; filename="{filename}"'
requests_response.status_code = status.HTTP_200_OK
else:
requests_response._content = b'{"message": "nope"}'
requests_response.status_code = status.HTTP_401_UNAUTHORIZED
kwargs['requests_response'] = requests_response
with mock.patch('substrapp.views.utils.authenticate_outgoing_request',
return_value=HTTPBasicAuth('foo', 'bar')), \
mock.patch('substrapp.utils.requests.get', return_value=requests_response):
f(*args, **kwargs)
return wrapper
return inner
示例11: password
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def password(self, request, **kwargs):
user = request.user
old = request.data.get('old', None)
new1 = request.data.get('new1', None)
new2 = request.data.get('new2', None)
try:
user = request.user.change_password(old=old, new=(new1, new2))
except user.IncorrectPassword:
return Response('Incorrect password',
status=status.HTTP_401_UNAUTHORIZED)
except user.PasswordsDontMatch:
return Response('Passwords do not match',
status=status.HTTP_400_BAD_REQUEST)
update_session_auth_hash(request, user)
return Response('password updated')
示例12: is_authenticated
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def is_authenticated(request):
"""
Determine whether the user is authenticated.
Return user_name and status HTTP_200_OK if authenticated, else return
status HTTP_401_UNAUTHORIZED.
"""
if request.user and request.user.is_authenticated():
data = [request.user.username, request.user.is_staff]
return Response(data=data, status=status.HTTP_200_OK)
return Response(status=status.HTTP_401_UNAUTHORIZED)
示例13: update
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def update(self, request, *args, **kwargs):
"""Update the ``Relation`` object.
Reject the update if user doesn't have ``EDIT`` permission on
the collection referenced in the ``Relation``.
"""
instance = self.get_object()
if (
not request.user.has_perm("edit_collection", instance.collection)
and not request.user.is_superuser
):
return Response(status=status.HTTP_401_UNAUTHORIZED)
return super().update(request, *args, **kwargs)
示例14: destroy
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def destroy(self, request, *args, **kwargs):
"""Delete the ``Relation`` object.
Reject the delete if user doesn't have ``EDIT`` permission on
the collection referenced in the ``Relation``.
"""
instance = self.get_object()
if (
not request.user.has_perm("edit_collection", instance.collection)
and not request.user.is_superuser
):
return Response(status=status.HTTP_401_UNAUTHORIZED)
return super().destroy(request, *args, **kwargs)
示例15: test_requires_auth
# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_401_UNAUTHORIZED [as 别名]
def test_requires_auth(self):
response = self.client.get(self.list_url)
self.assertEqual(
response.status_code,
status.HTTP_401_UNAUTHORIZED,
(response.status_code, response.content)
)
response = self.client.get(self.detail_url)
self.assertEqual(
response.status_code,
status.HTTP_401_UNAUTHORIZED,
(response.status_code, response.content)
)
response = self.client.delete(self.detail_url)
self.assertEqual(
response.status_code,
status.HTTP_401_UNAUTHORIZED,
(response.status_code, response.content)
)
response = self.client.post(self.list_url)
self.assertEqual(
response.status_code,
status.HTTP_401_UNAUTHORIZED,
(response.status_code, response.content)
)
开发者ID:lock8,项目名称:django-rest-framework-jwt-refresh-token,代码行数:30,代码来源:test_long_refresh_token_views.py