equal
deleted
inserted
replaced
|
1 """ |
|
2 Decorators for views based on HTTP headers. |
|
3 """ |
|
4 |
|
5 try: |
|
6 from functools import wraps |
|
7 except ImportError: |
|
8 from django.utils.functional import wraps # Python 2.3, 2.4 fallback. |
|
9 |
|
10 from django.utils.decorators import decorator_from_middleware |
|
11 from django.middleware.http import ConditionalGetMiddleware |
|
12 from django.http import HttpResponseNotAllowed |
|
13 |
|
14 conditional_page = decorator_from_middleware(ConditionalGetMiddleware) |
|
15 |
|
16 def require_http_methods(request_method_list): |
|
17 """ |
|
18 Decorator to make a view only accept particular request methods. Usage:: |
|
19 |
|
20 @require_http_methods(["GET", "POST"]) |
|
21 def my_view(request): |
|
22 # I can assume now that only GET or POST requests make it this far |
|
23 # ... |
|
24 |
|
25 Note that request methods should be in uppercase. |
|
26 """ |
|
27 def decorator(func): |
|
28 def inner(request, *args, **kwargs): |
|
29 if request.method not in request_method_list: |
|
30 return HttpResponseNotAllowed(request_method_list) |
|
31 return func(request, *args, **kwargs) |
|
32 return wraps(func)(inner) |
|
33 return decorator |
|
34 |
|
35 require_GET = require_http_methods(["GET"]) |
|
36 require_GET.__doc__ = "Decorator to require that a view only accept the GET method." |
|
37 |
|
38 require_POST = require_http_methods(["POST"]) |
|
39 require_POST.__doc__ = "Decorator to require that a view only accept the POST method." |