Created
September 17, 2013 13:37
-
-
Save techiev2/6594379 to your computer and use it in GitHub Desktop.
BaseView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VIEW_ERRORS = { | |
'405': { | |
'status': 405, | |
'message': 'End point does not accept the method' | |
}, | |
'404': { | |
'status': 404, | |
'message': '{0} not found' | |
}, | |
'403': { | |
'status': 403, | |
'message': 'Not authorized to access end point' | |
}, | |
'401': { | |
'status': 401, | |
'message': 'No authorization found' | |
}, | |
'400': { | |
'status': 400, | |
'message': 'Missing keys/Invalid data' | |
}, | |
'412': { | |
'status': 412, | |
'message': 'Missing keys/Invalid data' | |
}, | |
'500': { | |
'status': 500, | |
'message': 'Server error' | |
} | |
} | |
VIEW_MESSAGES = { | |
'delete_succeed': { | |
'status': 204, | |
}, | |
'delete_fail': { | |
'status': 202, | |
'message': '{0} object deletion failed' | |
} | |
} | |
class BaseView(object): | |
""" | |
Base view class | |
""" | |
def __init__(self, controller): | |
""" | |
Base view init | |
:param controller: Instance of Request handler from controller | |
""" | |
if not isinstance(controller, RequestHandler): | |
raise Exception("Invalid construct") | |
self.data, self.status_code, self.response, self.controller = \ | |
None, None, None, controller | |
def as_json(self): | |
""" | |
Respond with a JSON response from View. | |
""" | |
self.response = self.data or self.controller.response | |
if not self.response and self.status_code not in (204,): | |
raise Exception("No controller response") | |
headers_written = getattr(self.controller, '_headers_written') | |
finished = getattr(self.controller, '_finished') | |
if not (headers_written or finished): | |
self.controller.set_header("Content-Type", "application/json") | |
if self.response.get('status_code') and \ | |
self.response.get('status_code') != 200: | |
self.raise_error(**self.response) | |
return | |
self.controller.write(self.response) | |
self.controller.finish() | |
def raise_standard(self, key): | |
""" | |
Raise standard message. | |
""" | |
key = VIEW_MESSAGES.get(key) or '' | |
if not key: | |
return VIEW_ERRORS['500'] | |
def raise_error(self, status_code, custom_msg=None): | |
""" | |
Custom raise error method for BaseView | |
:param status_code: HTTP error status code to raise. | |
:param custom_msg: Custom error message | |
""" | |
self.status_code = status_code or 500 | |
self.status_message = VIEW_ERRORS.get(str(self.status_code)) | |
if self.status_code == 404: | |
model = self.controller.model if \ | |
hasattr(self.controller, 'model') else 'Object' | |
self.status_message['message'] = \ | |
self.status_message['message'].format(model) | |
if custom_msg: | |
self.status_message['message'] = custom_msg | |
self.controller.set_status(self.status_code) | |
if self.status_code not in (204,): | |
self.controller.write(self.status_message) | |
self.controller.finish() | |
def render(self): | |
"""Template render method for BaseView""" | |
self.controller._transforms = [] if not \ | |
self.controller._transforms \ | |
else self.controller._transforms | |
self.controller._headers_written = False if \ | |
self.controller._headers_written else \ | |
self.controller._headers_written | |
self.controller._finished = False if \ | |
self.controller._finished else\ | |
self.controller._finished | |
self.response = self.controller.response or {} | |
if getattr(self.controller, 'as_json', False): | |
self.as_json() | |
return | |
self.controller.render(self.controller.template_file, | |
data=self.response) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment