Python rest_framework.exceptions 模块,NotAuthenticated() 实例源码

我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用rest_framework.exceptions.NotAuthenticated()

项目:sdining    作者:Lurance    | 项目源码 | 文件源码
def handle_exception(self, exc):
        """
        Handle any exception that occurs, by returning an appropriate response,
        or re-raising the error.
        """
        if isinstance(exc, (exceptions.NotAuthenticated,
                            exceptions.AuthenticationFailed)):
            # WWW-Authenticate header for 401 responses, else coerce to 403
            auth_header = self.get_authenticate_header(self.request)

            if auth_header:
                exc.auth_header = auth_header
            else:
                exc.status_code = status.HTTP_403_FORBIDDEN

        exception_handler = self.get_exception_handler()

        context = self.get_exception_handler_context()
        response = exception_handler(exc, context)

        if response is None:
            self.raise_uncaught_exception(exc)

        response.exception = True
        return response
项目:sdining    作者:Lurance    | 项目源码 | 文件源码
def permission_denied(self, request, message=None):
        """
        If request is not permitted, determine what kind of exception to raise.
        """
        if request.authenticators and not request.successful_authenticator:
            raise exceptions.NotAuthenticated()
        raise exceptions.PermissionDenied(detail=message)
项目:django-open-volunteering-platform    作者:OpenVolunteeringPlatform    | 项目源码 | 文件源码
def get_skills_and_causes(self, request):
    if not request.user.is_authenticated():
      raise NotAuthenticated()

    user = request.user
    output = {"skills": [], "causes": []}

    if user.profile:
      output["skills"] = user.profile.skills.values_list('id', flat=True)
      output["causes"] = user.profile.causes.values_list('id', flat=True)

    return output
项目:jianshu-api    作者:strugglingyouth    | 项目源码 | 文件源码
def permission_denied(self, request, message=None):
        """
        If request is not permitted, determine what kind of exception to raise.
        """
        if not request.successful_authenticator:
            raise exceptions.NotAuthenticated()
        raise exceptions.PermissionDenied(detail=message)
项目:jianshu-api    作者:strugglingyouth    | 项目源码 | 文件源码
def handle_exception(self, exc):
        """
        Handle any exception that occurs, by returning an appropriate response,
        or re-raising the error.
        """
        if isinstance(exc, (exceptions.NotAuthenticated,
                            exceptions.AuthenticationFailed)):
            # WWW-Authenticate header for 401 responses, else coerce to 403
            auth_header = self.get_authenticate_header(self.request)

            if auth_header:
                exc.auth_header = auth_header
            else:
                exc.status_code = status.HTTP_403_FORBIDDEN

        exception_handler = self.settings.EXCEPTION_HANDLER

        context = self.get_exception_handler_context()
        response = exception_handler(exc, context)

        if response is None:
            raise

        response.exception = True
        return response

    # Note: Views are made CSRF exempt from within `as_view` as to prevent
    # accidental removal of this exemption in cases where `dispatch` needs to
    # be overridden.
项目:BrewCenterAPI    作者:BrewCenter    | 项目源码 | 文件源码
def custom_exception_handler(exc, context):
    # if its a view with a list and request attr
    if 'view' in context and hasattr(context['view'], 'list') and hasattr(context['view'], 'request'):
        view = context['view']
        request = view.request

        if request.method == 'GET' and settings.ENABLE_UNAUTHENTICATED_RESULTS and isinstance(exc, NotAuthenticated):
            return view.list(context['request'])

    return exception_handler(exc, context)
项目:FormShare    作者:qlands    | 项目源码 | 文件源码
def has_permission(self, request, view):

        if request.user.is_anonymous() and view.action == 'list':
            if request.GET.get('search'):
                raise exceptions.NotAuthenticated()

        return \
            super(UserViewSetPermissions, self).has_permission(request, view)
项目:tunga-api    作者:tunga-io    | 项目源码 | 文件源码
def download_profile(self, request, user_id=None):
        """
        Download User Profile Endpoint
        ---
        omit_serializer: True
        omit_parameters:
            - query
        """
        current_url = '%s?%s' % (
            reverse(request.resolver_match.url_name, kwargs={'user_id': user_id}),
            urlencode(request.query_params)
        )
        login_url = '/signin?next=%s' % quote_plus(current_url)
        if not request.user.is_authenticated():
            return redirect(login_url)

        user = get_object_or_404(self.get_queryset(), pk=user_id)

        try:
            self.check_object_permissions(request, user)
        except NotAuthenticated:
            return redirect(login_url)
        except PermissionDenied:
            return HttpResponse("You do not have permission to access this estimate")

        ctx = {
            'user': user,
            'profile': user.profile,
            'work': user.work_set.all(),
            'education': user.education_set.all()
        }

        rendered_html = render_to_string("tunga/pdf/profile.html", context=ctx).encode(encoding="UTF-8")

        if request.accepted_renderer.format == 'html':
            return HttpResponse(rendered_html)

        pdf_file = HTML(string=rendered_html, encoding='utf-8').write_pdf()
        http_response = HttpResponse(pdf_file, content_type='application/pdf')
        http_response['Content-Disposition'] = 'filename="developer_profile.pdf"'
        return http_response