1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | from django.http import HttpRequest
import inspect
class RequestFetchingMixin(object):
def __new__(cls, *args, **kwargs):
obj = object.__new__(cls, *args, **kwargs)
f = inspect.currentframe()
requests = []
request = None
backup_count = 0
while not requests and f.f_back is not None and backup_count < 5:
f = f.f_back
for var, val in f.f_locals.iteritems():
if isinstance(val, HttpRequest):
requests.append((var, val))
backup_count += 1
if not requests:
request = None
elif len(requests) == 1:
request = requests[0][1]
else:
for var, val in requests:
if var == 'request':
request = val
if request is None:
request = requests[0][1]
obj.request = request
return obj
|
More like this
- Complex Form Preview by smagala 4 years, 2 months ago
- DropDownMultiple widget by marinho 5 years, 1 month ago
- Complex Formsets by smagala 4 years, 5 months ago
- Semi-Portable recaptcha integration with django forms by pinkeen 2 years, 6 months ago
- DateTimeField with microseconds by tobias 4 years, 3 months ago
Comments