Skip to content

Commit

Permalink
Merge pull request #5 from aplmicrons/custom_err
Browse files Browse the repository at this point in the history
Custom error handler
  • Loading branch information
sandyhider authored Aug 4, 2017
2 parents 1ad7b10 + 867bcb2 commit fc2523f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions django/bosscore/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ErrorCodes(IntEnum):
MISSING_PERMISSION = 3001
UNRECOGNIZED_PERMISSION = 3002
INGEST_NOT_CREATOR = 3003
ACCESS_DENIED_UNKNOWN = 3004 # User may not even be logged in.

# Database errors
RESOURCE_NOT_FOUND = 4000
Expand Down
70 changes: 70 additions & 0 deletions django/bosscore/renderer_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2016 The Johns Hopkins University Applied Physics Laboratory
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from bosscore.error import ErrorCodes
from rest_framework.renderers import JSONRenderer

def check_for_403(fcn):
"""Decorator to check renderer_context for a 403 response.
Args:
fcn (function): A custom BaseRenderer.render() implementation..
Returns:
(function): Wraps given function with one that checks for a 403 status.
"""

def wrapper(*args, **kwargs):
"""Return a JSON response if a 403 found.
Executes the custom renderer if no 403 status code found.
Args:
Returns:
(BaseRenderer)
"""

# renderer_context is the 4th argument if not specified by keyword.
if len(args) < 4 and kwargs is None:
return fcn(*args, **kwargs)

renderer_context = None
if len(args) >= 4:
renderer_context = args[3]
elif kwargs is not None and 'renderer_context' in kwargs:
renderer_context = kwargs['renderer_context']

if renderer_context is None:
return fcn(*args, **kwargs)

# Check for presense of a Response object.
if 'response' not in renderer_context:
return fcn(*args, **kwargs)

# Have a response, check for 403.
if renderer_context['response'].status_code == 403:
renderer_context['response']['Content-Type'] = 'application/json'
obj = args[0]
obj.media_type = 'application/json'
obj.format = 'json'
err_msg = {"status": 403, "message": "Access denied, are you logged in?",
"code": ErrorCodes.ACCESS_DENIED_UNKNOWN}
jr = JSONRenderer()
return jr.render(err_msg, 'application/json', renderer_context)

return fcn(*args, **kwargs)

return wrapper

16 changes: 16 additions & 0 deletions django/bossspatialdb/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io
from PIL import Image

from bosscore.renderer_helper import check_for_403

class BloscPythonRenderer(renderers.BaseRenderer):
""" A DRF renderer for a blosc encoded cube of data using the numpy interface
Expand All @@ -31,6 +32,7 @@ class BloscPythonRenderer(renderers.BaseRenderer):
charset = None
render_style = 'binary'

@check_for_403
def render(self, data, media_type=None, renderer_context=None):

if not data["data"].data.flags['C_CONTIGUOUS']:
Expand All @@ -52,8 +54,18 @@ class BloscRenderer(renderers.BaseRenderer):
charset = None
render_style = 'binary'

@check_for_403
def render(self, data, media_type=None, renderer_context=None):

if renderer_context['response'].status_code == 403:
renderer_context["accepted_media_type"] = 'application/json'
self.media_type = 'application/json'
self.format = 'json'
err_msg = {"status": 403, "message": "Access denied, are you logged in?",
"code": 2005}
jr = JSONRenderer()
return jr.render(err_msg, 'application/json', renderer_context)

if not data["data"].data.flags['C_CONTIGUOUS']:
data["data"].data = np.ascontiguousarray(data["data"].data, dtype=data["data"].data.dtype)

Expand All @@ -75,6 +87,7 @@ class NpygzRenderer(renderers.BaseRenderer):
charset = None
render_style = 'binary'

@check_for_403
def render(self, data, media_type=None, renderer_context=None):

if not data["data"].data.flags['C_CONTIGUOUS']:
Expand Down Expand Up @@ -106,6 +119,7 @@ class JpegRenderer(renderers.BaseRenderer):
charset = None
render_style = 'binary'

@check_for_403
def render(self, data, media_type=None, renderer_context=None):

# Return data, squeezing time dimension as this only works with 3D data
Expand All @@ -115,6 +129,7 @@ def render(self, data, media_type=None, renderer_context=None):
else:
# This appears to contain time data. Error out
renderer_context["response"].status_code = 400
renderer_context['response']['Content-Type'] = 'application/json'
renderer_context["accepted_media_type"] = 'application/json'
self.media_type = 'application/json'
self.format = 'json'
Expand All @@ -126,6 +141,7 @@ def render(self, data, media_type=None, renderer_context=None):
if renderer_context['view'].bit_depth != 8:
# This renderer only works on uint8 data
renderer_context["response"].status_code = 400
renderer_context['response']['Content-Type'] = 'application/json'
renderer_context["accepted_media_type"] = 'application/json'
self.media_type = 'application/json'
self.format = 'json'
Expand Down
4 changes: 4 additions & 0 deletions django/bosstiles/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.
import io
from rest_framework import renderers
from rest_framework.renderers import JSONRenderer
from bosscore.renderer_helper import check_for_403


class PNGRenderer(renderers.BaseRenderer):
Expand All @@ -23,6 +25,7 @@ class PNGRenderer(renderers.BaseRenderer):
charset = None
render_style = 'binary'

@check_for_403
def render(self, data, media_type=None, renderer_context=None):
file_obj = io.BytesIO()
data.save(file_obj, "PNG")
Expand All @@ -38,6 +41,7 @@ class JPEGRenderer(renderers.BaseRenderer):
charset = None
render_style = 'binary'

@check_for_403
def render(self, data, media_type=None, renderer_context=None):
file_obj = io.BytesIO()
data.save(file_obj, "JPEG")
Expand Down

0 comments on commit fc2523f

Please sign in to comment.