Login

Other approach of making middleware (by decorators)

Author:
diverman
Posted:
March 6, 2011
Language:
Python
Version:
1.2
Score:
0 (after 0 ratings)

Other approach of making middleware. Advantage of is to specify, which middleware is used for which view function and in what order. Middleware function gets all arguments, that are passed to view function.

Example usage

@RequestMiddleware
def print_params_middleware(request, *args, **kwargs):
    print 'GET params:', request.GET

@ResponseMiddleware
def modify_headers_middleware(request, response, *args, **kwargs):
    response['Content-Type'] = 'text/html'

@ExceptionMiddleware
def catch_error_middleware(request, e, *args, **kwargs):
    return HttpResponse('catched error %s' % e )

@modify_headers_middleware
@catch_error_middleware
@print_params_middleware
def some_view(request, *args, **kwargs):
    print 'someview'
    return HttpResponse()
 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from functools import wraps

def RequestMiddleware(middleware_func):
    """ RequestMiddleware """

    @wraps(middleware_func)
    def middleware(view_func):
        """ middleware """

        @wraps(view_func)
        def new_view_func(request, *args, **kwargs ):
            """ new_view_func """

            middleware_func(request, *args, **kwargs)

            return view_func(request, *args, **kwargs)
        return new_view_func
    return middleware


def ResponseMiddleware(middleware_func):
    """ ResponseMiddleware """

    @wraps(middleware_func)
    def middleware(view_func):
        """ middleware """

        @wraps(view_func)
        def new_view_func(request, *args, **kwargs ):
            """ new_view_func """

            response = view_func(request, *args, **kwargs)

            middleware_func(request, response, *args, **kwargs)

            return response
        return new_view_func
    return middleware


def ExceptionMiddleware(middleware_func):
    """ ExceptionMiddleware """

    @wraps(middleware_func)
    def middleware(view_func):
        """ middleware """

        @wraps(view_func)
        def new_view_func(request, *args, **kwargs ):
            """ new_view_func """

            try:
                response = view_func(request, *args, **kwargs)
            except Exception, e:
                response = middleware_func(request, e, *args, **kwargs)

            if response is None:
                raise

            return response
        return new_view_func
    return middleware

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 4 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 4 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 11 months, 3 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year ago
  5. Help text hyperlinks by sa2812 1 year, 1 month ago

Comments

Please login first before commenting.