本文整理汇总了Python中rest_framework.permissions.AllowAny方法的典型用法代码示例。如果您正苦于以下问题:Python permissions.AllowAny方法的具体用法?Python permissions.AllowAny怎么用?Python permissions.AllowAny使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.permissions
的用法示例。
在下文中一共展示了permissions.AllowAny方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_should_not_cache_for_non_json_responses
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def test_should_not_cache_for_non_json_responses(self):
""" Verify that the decorator does not cache if the response is not json """
def key_func(**kwargs): # pylint: disable=unused-argument
return 'non_json_cache_key'
class TestView(views.APIView):
permission_classes = [permissions.AllowAny]
renderer_classes = [BrowsableAPIRenderer] # Non-json responses
@compressed_cache_response(key_func=key_func)
def get(self, request, *args, **kwargs):
return Response('test response')
view_instance = TestView()
view_instance.headers = {} # pylint: disable=attribute-defined-outside-init
view_instance.dispatch(request=self.request)
# Verify nothing was cached
self.assertEqual(cache.get('non_json_cache_key'), None)
示例2: test_should_not_cache_if_waffled
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def test_should_not_cache_if_waffled(self, waffle_active):
""" Verify that the decorator does not cache the waffle flag is turned off """
def key_func(**kwargs): # pylint: disable=unused-argument
return self.cache_response_key
class TestView(views.APIView):
permission_classes = [permissions.AllowAny]
renderer_classes = [JSONRenderer]
@compressed_cache_response(key_func=key_func)
def get(self, request, *args, **kwargs):
return Response('test response')
with override_flag('compressed_cache.TestView.get', active=waffle_active):
view_instance = TestView()
view_instance.headers = {} # pylint: disable=attribute-defined-outside-init
view_instance.dispatch(request=self.request)
# Verify nothing was cached
if waffle_active:
self.assertIsNot(cache.get(self.cache_response_key), None)
else:
self.assertIs(cache.get(self.cache_response_key), None)
示例3: post
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def post(self, request, format=None):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
email = serializer.data['email']
try:
user = get_user_model().objects.get(email=email)
if user.is_verified and user.is_active:
password_reset_code = \
PasswordResetCode.objects.create_reset_code(user)
password_reset_code.send_password_reset_email()
content = {'email': email}
return Response(content, status=status.HTTP_201_CREATED)
except get_user_model().DoesNotExist:
pass
# Since this is AllowAny, don't give away error.
content = {'detail': _('Password reset not allowed.')}
return Response(content, status=status.HTTP_400_BAD_REQUEST)
else:
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
示例4: get_schema_view
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def get_schema_view(self):
view = get_schema_view(
openapi.Info(
title="Django Bananas Admin API Schema",
default_version=BananasVersioning.default_version,
description="API for django-bananas.js",
# terms_of_service="https://www.google.com/policies/terms/",
# license=openapi.License(name="BSD License"),
),
# validators=["flex", "ssv"],
public=False,
generator_class=BananasOpenAPISchemaGenerator,
authentication_classes=(SessionAuthentication,),
permission_classes=(permissions.AllowAny,),
patterns=self.urls,
)
view.versioning_class = BananasVersioning
return view
示例5: get_serializer_context
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def get_serializer_context(self, *args, **kwargs):
return {"request": self.request}
# class RegisterAPIView(APIView):
# permission_classes = [permissions.AllowAny]
# def post(self, request, *args, **kwargs):
# if request.user.is_authenticated():
# return Response({'detail': 'You are already registered and are authenticated.'}, status=400)
# data = request.data
# username = data.get('username') # username or email address
# email = data.get('username')
# password = data.get('password')
# password2 = data.get('password2')
# qs = User.objects.filter(
# Q(username__iexact=username)|
# Q(email__iexact=username)
# )
# if password != password2:
# return Response({"password": "Password must match."}, status=401)
# if qs.exists():
# return Response({"detail": "This user already exists"}, status=401)
# else:
# user = User.objects.create(username=username, email=email)
# user.set_password(password)
# user.save()
# # payload = jwt_payload_handler(user)
# # token = jwt_encode_handler(payload)
# # response = jwt_response_payload_handler(token, user, request=request)
# # return Response(response, status=201)
# return Response({'detail': "Thank you for registering. Please verify your email."}, status=201)
# return Response({"detail": "Invalid Request"}, status=400)
示例6: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def get_permissions(self):
if self.action == 'list' or self.action == 'retrieve':
permission_classes = [permissions.AllowAny]
else:
permission_classes = [DenyAny]
return [permission() for permission in permission_classes]
示例7: test_excluding_fields
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def test_excluding_fields(self):
@autofilter(exclude_fields=("indexed_int", "indexed_char", ))
class SampleApiV7(ListAPIView):
permission_classes = (AllowAny,)
serializer_class = SampleModelForAutofilterSerializerVer1
queryset = SampleModelForAutofilter.objects.all()
self.assertEqual(set(SampleApiV7.filter_fields.keys()), {"id", "fk", "indexed_text", "indexed_url",
"indexed_email", "nullable_field", "unique_text"})
示例8: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return (permissions.IsAuthenticated(),)
if self.request.method == 'POST':
return (permissions.AllowAny(),)
return (permissions.IsAuthenticated(), IsAccountOwner(),)
示例9: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def get_permissions(self):
"""
Instantiates and returns the list of permissions that this view requires.
"""
if self.action == "create":
# Allow any user to create an account, but limit other actions to logged-in users.
permission_classes = [AllowAny]
else:
permission_classes = [IsAuthenticated]
return [permission() for permission in permission_classes]
示例10: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def get_permissions(self):
"""
The create action are used to register user, we do not require authentication on that
endpoint.
"""
if self.action == "create":
return [AllowAny()]
return super().get_permissions()
示例11: __init__
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def __init__(self):
if system_layout.current_value is not None:
raise MiddlewareNotUsed()
class FarmNotConfiguredView(APIView):
permission_classes = (AllowAny, )
def get(self, request):
raise FarmNotConfiguredError()
post = get
put = get
patch = get
delete = get
self.view = FarmNotConfiguredView.as_view()
示例12: test_should_handle_getting_uncompressed_response_from_cache
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def test_should_handle_getting_uncompressed_response_from_cache(self):
""" Verify that the decorator correctly returns uncompressed responses """
def key_func(**kwargs): # pylint: disable=unused-argument
return self.cache_response_key
class TestView(views.APIView):
permission_classes = [permissions.AllowAny]
renderer_classes = [JSONRenderer]
@compressed_cache_response(key_func=key_func)
def get(self, request, *args, **kwargs):
return Response('test response')
view_instance = TestView()
view_instance.headers = {} # pylint: disable=attribute-defined-outside-init
uncompressed_cached_response = Response('cached test response')
view_instance.finalize_response(request=self.request, response=uncompressed_cached_response)
uncompressed_cached_response.render()
response_triple = (
uncompressed_cached_response.rendered_content,
uncompressed_cached_response.status_code,
uncompressed_cached_response._headers.copy(), # pylint: disable=protected-access
)
cache.set(self.cache_response_key, response_triple)
response = view_instance.dispatch(request=self.request)
self.assertEqual(response.content.decode('utf-8'), '"cached test response"')
示例13: test_should_handle_getting_compressed_response_from_cache
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def test_should_handle_getting_compressed_response_from_cache(self):
""" Verify that the decorator correctly returns compressed responses """
def key_func(**kwargs): # pylint: disable=unused-argument
return self.cache_response_key
class TestView(views.APIView):
permission_classes = [permissions.AllowAny]
renderer_classes = [JSONRenderer]
@compressed_cache_response(key_func=key_func)
def get(self, request, *args, **kwargs):
return Response('test response')
view_instance = TestView()
view_instance.headers = {} # pylint: disable=attribute-defined-outside-init
compressed_cached_response = Response('compressed cached test response')
view_instance.finalize_response(request=self.request, response=compressed_cached_response)
compressed_cached_response.render()
# Rendered content is compressed before response goes into the cache
response_triple = (
zlib.compress(compressed_cached_response.rendered_content),
compressed_cached_response.status_code,
compressed_cached_response._headers.copy(), # pylint: disable=protected-access
)
cache.set(self.cache_response_key, response_triple)
response = view_instance.dispatch(request=self.request)
self.assertEqual(response.content.decode('utf-8'), '"compressed cached test response"')
示例14: register_permission_classes
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def register_permission_classes():
permission_classes = [AllowAny, ]
for klass in getattr(settings, 'REST_AUTH_REGISTER_PERMISSION_CLASSES', tuple()):
permission_classes.append(import_callable(klass))
return tuple(permission_classes)
示例15: get_permissions
# 需要导入模块: from rest_framework import permissions [as 别名]
# 或者: from rest_framework.permissions import AllowAny [as 别名]
def get_permissions(self):
if self.request.query_params.get('user-only', None):
self.permission_classes = (AllowAny,)
return super().get_permissions()