mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-24 02:24:03 +03:00
Merge pull request #229 from max-arnold/228-runtime-head
add View.head() method at runtime (issue #228)
This commit is contained in:
commit
21b5601d3b
|
@ -65,15 +65,45 @@ except ImportError:
|
||||||
environ.update(request)
|
environ.update(request)
|
||||||
return WSGIRequest(environ)
|
return WSGIRequest(environ)
|
||||||
|
|
||||||
# django.views.generic.View (Django >= 1.3)
|
# django.views.generic.View (1.3 <= Django < 1.4)
|
||||||
try:
|
try:
|
||||||
from django.views.generic import View
|
from django.views.generic import View
|
||||||
if not hasattr(View, 'head'):
|
|
||||||
|
if django.VERSION < (1, 4):
|
||||||
|
from django.utils.decorators import classonlymethod
|
||||||
|
from django.utils.functional import update_wrapper
|
||||||
|
|
||||||
# First implementation of Django class-based views did not include head method
|
# First implementation of Django class-based views did not include head method
|
||||||
# in base View class - https://code.djangoproject.com/ticket/15668
|
# in base View class - https://code.djangoproject.com/ticket/15668
|
||||||
class ViewPlusHead(View):
|
class ViewPlusHead(View):
|
||||||
def head(self, request, *args, **kwargs):
|
@classonlymethod
|
||||||
return self.get(request, *args, **kwargs)
|
def as_view(cls, **initkwargs):
|
||||||
|
"""
|
||||||
|
Main entry point for a request-response process.
|
||||||
|
"""
|
||||||
|
# sanitize keyword arguments
|
||||||
|
for key in initkwargs:
|
||||||
|
if key in cls.http_method_names:
|
||||||
|
raise TypeError(u"You tried to pass in the %s method name as a "
|
||||||
|
u"keyword argument to %s(). Don't do that."
|
||||||
|
% (key, cls.__name__))
|
||||||
|
if not hasattr(cls, key):
|
||||||
|
raise TypeError(u"%s() received an invalid keyword %r" % (
|
||||||
|
cls.__name__, key))
|
||||||
|
|
||||||
|
def view(request, *args, **kwargs):
|
||||||
|
self = cls(**initkwargs)
|
||||||
|
if hasattr(self, 'get') and not hasattr(self, 'head'):
|
||||||
|
self.head = self.get
|
||||||
|
return self.dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
|
# take name and docstring from class
|
||||||
|
update_wrapper(view, cls, updated=())
|
||||||
|
|
||||||
|
# and possible attributes set by decorators
|
||||||
|
# like csrf_exempt from dispatch
|
||||||
|
update_wrapper(view, cls.dispatch, assigned=())
|
||||||
|
return view
|
||||||
View = ViewPlusHead
|
View = ViewPlusHead
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -121,6 +151,8 @@ except ImportError:
|
||||||
|
|
||||||
def view(request, *args, **kwargs):
|
def view(request, *args, **kwargs):
|
||||||
self = cls(**initkwargs)
|
self = cls(**initkwargs)
|
||||||
|
if hasattr(self, 'get') and not hasattr(self, 'head'):
|
||||||
|
self.head = self.get
|
||||||
return self.dispatch(request, *args, **kwargs)
|
return self.dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
# take name and docstring from class
|
# take name and docstring from class
|
||||||
|
@ -154,9 +186,6 @@ except ImportError:
|
||||||
#)
|
#)
|
||||||
return http.HttpResponseNotAllowed(allowed_methods)
|
return http.HttpResponseNotAllowed(allowed_methods)
|
||||||
|
|
||||||
def head(self, request, *args, **kwargs):
|
|
||||||
return self.get(request, *args, **kwargs)
|
|
||||||
|
|
||||||
# PUT, DELETE do not require CSRF until 1.4. They should. Make it better.
|
# PUT, DELETE do not require CSRF until 1.4. They should. Make it better.
|
||||||
if django.VERSION >= (1, 4):
|
if django.VERSION >= (1, 4):
|
||||||
from django.middleware.csrf import CsrfViewMiddleware
|
from django.middleware.csrf import CsrfViewMiddleware
|
||||||
|
|
Loading…
Reference in New Issue
Block a user