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. codigo alto nivel by MrRocklion 1 week, 1 day ago
  2. Load template from specific app by Krzysiek555 4 weeks ago
  3. PostgreSQL JSON subqueries by dolamroth 1 month ago
  4. "Magic Link" Management Command by webology 6 months ago
  5. Closest ORM models to a latitude/longitude point by simonw 6 months ago

Comments

Please login first before commenting.