Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #703: added decorators to require that view be called with a gi…
…ven HTTP REQUEST_METHOD git-svn-id: http://code.djangoproject.com/svn/django/trunk@1016 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
Showing
1 changed file
with
28 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,9 +1,35 @@ | |||
""" | """ | ||
Decorator for views that supports conditional get on ETag and Last-Modified | Decorators for views based on HTTP headers. | ||
headers. | |||
""" | """ | ||
|
|
||
from django.utils.decorators import decorator_from_middleware | from django.utils.decorators import decorator_from_middleware | ||
from django.middleware.http import ConditionalGetMiddleware | from django.middleware.http import ConditionalGetMiddleware | ||
from django.utils.httpwrappers import HttpResponseForbidden | |||
|
|
||
conditional_page = decorator_from_middleware(ConditionalGetMiddleware) | conditional_page = decorator_from_middleware(ConditionalGetMiddleware) | ||
|
|||
def require_http_methods(request_method_list): | |||
""" | |||
Decorator to make a view only accept particular request methods. Usage:: | |||
@require_http_methods(["GET", "POST"]) | |||
def my_view(request): | |||
# I can assume now that only GET or POST requests make it this far | |||
# ... | |||
Note that request methods ARE case sensitive. | |||
""" | |||
def decorator(func): | |||
def inner(request, *args, **kwargs): | |||
method = request.META.get("REQUEST_METHOD", None) | |||
if method not in request_method_list: | |||
raise HttpResponseForbidden("REQUEST_METHOD '%s' not allowed" % method) | |||
return func(request, *args, **kwargs) | |||
return inner | |||
return decorator | |||
|
|||
require_GET = require_http_methods(["GET"]) | |||
require_GET.__doc__ = "Decorator to require that a view only accept the GET method." | |||
|
|||
require_POST = require_http_methods(["POST"]) | |||
require_POST.__doc__ = "Decorator to require that a view only accept the POST method." |