Django

Code

Ticket #703: request_method_decorators.diff

File request_method_decorators.diff, 1.3 kB (added by cygnus@cprogrammer.org, 3 years ago)

Patch to implement decorators to restrict request method for views.

  • django/views/decorators/http.py

    old new  
    77from django.middleware.http import ConditionalGetMiddleware 
    88 
    99conditional_page = decorator_from_middleware(ConditionalGetMiddleware) 
     10 
     11# Decorators for disallowing access based on the value of 
     12# request.META['REQUEST_METHOD'].  These decorators will prevent the 
     13# calling of views whose operation depends on particular request 
     14# methods.  Note that if request.META['REQUEST_METHOD'] is not 
     15# present, the views will not be called. 
     16 
     17from django.utils.httpwrappers import HttpResponseForbidden 
     18 
     19def requires_POST(view_func): 
     20    def _requires_POST_wrapper(request, *args, **kwargs): 
     21        if request.META.get('REQUEST_METHOD', None) != 'POST': 
     22            return HttpResponseForbidden() 
     23        return view_func(request, *args, **kwargs) 
     24    return _requires_POST_wrapper 
     25 
     26def requires_GET(view_func): 
     27    def _requires_GET_wrapper(request, *args, **kwargs): 
     28        if request.META.get('REQUEST_METHOD', None) != 'GET': 
     29            return HttpResponseForbidden() 
     30        return view_func(request, *args, **kwargs) 
     31    return _requires_GET_wrapper