Example #1
0
 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)
Example #2
0
def exception_handler(exc, context):
    """
    Returns the response that should be used for any given exception.

    By default we handle the REST framework `APIException`, and also
    Django's built-in `Http404` and `PermissionDenied` exceptions.

    Any unhandled exceptions may return `None`, which will cause a 500 error
    to be raised.
    """
    if isinstance(exc, Http404):
        exc = exceptions.NotFound()
    elif isinstance(exc, PermissionDenied):
        exc = exceptions.PermissionDenied()

    if isinstance(exc, exceptions.APIException):
        headers = {}
        if getattr(exc, 'auth_header', None):
            headers['WWW-Authenticate'] = exc.auth_header
        if getattr(exc, 'wait', None):
            headers['Retry-After'] = '%d' % exc.wait

        if isinstance(exc.detail, (list, dict)):
            data = exc.detail
        else:
            data = {'detail': exc.detail}

        set_rollback()
        return Response(data, status=exc.status_code, headers=headers)

    return None
 def get_object(self):
     if self.request.method == 'PUT':
         raise exceptions.PermissionDenied()
 def check_permissions(self, request):
     if request.method == 'POST':
         raise exceptions.PermissionDenied()
Example #5
0
 def get(self, request, *args, **kwargs):
     schema = self.schema_generator.get_schema(request, self.public)
     if schema is None:
         raise exceptions.PermissionDenied()
     return Response(schema)