Created
August 25, 2016 12:59
-
-
Save ustun/7e814f5a7340f033112fa2a2075bbacb to your computer and use it in GitHub Desktop.
If the response is a json object, outputs the matched func, file name and line no
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
class MatchingViewFuncMiddleware(object): | |
def process_request(self, request): | |
pass | |
def process_response(self, request, response): | |
if 'content-type' in response._headers and \ | |
response._headers['content-type'][1].startswith('application/json') and \ | |
response.status_code == 200: | |
data = json.loads(response.content) | |
if not isinstance(data, dict): | |
return response | |
if 'meta' not in data: | |
data['meta'] = {} | |
myfunc, myargs, mykwargs = resolve(request.path) | |
func_name = myfunc.func_name | |
func_file_name = myfunc.func_code.co_filename | |
func_line_no = myfunc.func_code.co_firstlineno | |
data['meta']['resolved_view_function'] = "{} at {}:{}".format(func_name, func_file_name, func_line_no) | |
data['meta']['resolved_view_function_path'] = "{}:{}".format(func_file_name, func_line_no) | |
response.content = json.dumps(data) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment