RequestFetchingMixin

 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

  1. DateTimeField with microseconds by tobias 5 years, 1 month ago
  2. Effective content caching for mass-load site using redirect feature by nnseva 2 years, 9 months ago
  3. @url decorator - getting rid of urlpatterns by southern_sun 6 years, 8 months ago
  4. GeoDjango maps in admin TabularInlines by alanB 3 years, 6 months ago
  5. Complex Form Preview by smagala 5 years ago

Comments

(Forgotten your password?)