Skip to content

Instantly share code, notes, and snippets.

@ustun
Created August 25, 2016 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ustun/7e814f5a7340f033112fa2a2075bbacb to your computer and use it in GitHub Desktop.
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
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