Skip to content

Instantly share code, notes, and snippets.

@bitemyapp
Created December 10, 2013 08:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitemyapp/7887449 to your computer and use it in GitHub Desktop.
Save bitemyapp/7887449 to your computer and use it in GitHub Desktop.
def get_request():
"""
blindly walks up the stack looking for
request.user
"""
for i in itertools.count():
try:
frame = sys._getframe(i)
except ValueError:
frame = None
if not frame: return None
if "request" in frame.f_locals:
request = frame.f_locals['request']
if not isinstance(request, HttpRequest) or not hasattr(request, "user"):
# wrong signature... keep looking
continue
return request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment