mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-25 00:34:21 +03:00
Some minor tidy up
This commit is contained in:
parent
b522cc8e51
commit
4c438f9f4d
|
@ -59,7 +59,6 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
|
||||||
"""
|
"""
|
||||||
permissions = ( permissions.FullAnonAccess, )
|
permissions = ( permissions.FullAnonAccess, )
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def as_view(cls, **initkwargs):
|
def as_view(cls, **initkwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -71,7 +70,6 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
|
||||||
view.cls_instance = cls(**initkwargs)
|
view.cls_instance = cls(**initkwargs)
|
||||||
return view
|
return view
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def allowed_methods(self):
|
def allowed_methods(self):
|
||||||
"""
|
"""
|
||||||
|
@ -79,7 +77,6 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
|
||||||
"""
|
"""
|
||||||
return [method.upper() for method in self.http_method_names if hasattr(self, method)]
|
return [method.upper() for method in self.http_method_names if hasattr(self, method)]
|
||||||
|
|
||||||
|
|
||||||
def http_method_not_allowed(self, request, *args, **kwargs):
|
def http_method_not_allowed(self, request, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Return an HTTP 405 error if an operation is called which does not have a handler method.
|
Return an HTTP 405 error if an operation is called which does not have a handler method.
|
||||||
|
@ -87,15 +84,34 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
|
||||||
raise ErrorResponse(status.HTTP_405_METHOD_NOT_ALLOWED,
|
raise ErrorResponse(status.HTTP_405_METHOD_NOT_ALLOWED,
|
||||||
{'detail': 'Method \'%s\' not allowed on this resource.' % self.method})
|
{'detail': 'Method \'%s\' not allowed on this resource.' % self.method})
|
||||||
|
|
||||||
|
|
||||||
def initial(self, request, *args, **kargs):
|
def initial(self, request, *args, **kargs):
|
||||||
"""
|
"""
|
||||||
Hook for any code that needs to run prior to anything else.
|
Hook for any code that needs to run prior to anything else.
|
||||||
Required if you want to do things like set `request.upload_handlers` before
|
Required if you want to do things like set `request.upload_handlers` before
|
||||||
the authentication and dispatch handling is run.
|
the authentication and dispatch handling is run.
|
||||||
"""
|
"""
|
||||||
pass
|
# Calls to 'reverse' will not be fully qualified unless we set the
|
||||||
|
# scheme/host/port here.
|
||||||
|
self.orig_prefix = get_script_prefix()
|
||||||
|
if not (self.orig_prefix.startswith('http:') or self.orig_prefix.startswith('https:')):
|
||||||
|
prefix = '%s://%s' % (request.is_secure() and 'https' or 'http', request.get_host())
|
||||||
|
set_script_prefix(prefix + self.orig_prefix)
|
||||||
|
|
||||||
|
def final(self, request, response, *args, **kargs):
|
||||||
|
"""
|
||||||
|
Hook for any code that needs to run after everything else in the view.
|
||||||
|
"""
|
||||||
|
# Restore script_prefix.
|
||||||
|
set_script_prefix(self.orig_prefix)
|
||||||
|
|
||||||
|
# Always add these headers.
|
||||||
|
response.headers['Allow'] = ', '.join(self.allowed_methods)
|
||||||
|
# sample to allow caching using Vary http header
|
||||||
|
response.headers['Vary'] = 'Authenticate, Accept'
|
||||||
|
|
||||||
|
# merge with headers possibly set at some point in the view
|
||||||
|
response.headers.update(self.headers)
|
||||||
|
return self.render(response)
|
||||||
|
|
||||||
def add_header(self, field, value):
|
def add_header(self, field, value):
|
||||||
"""
|
"""
|
||||||
|
@ -113,12 +129,6 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
self.headers = {}
|
self.headers = {}
|
||||||
|
|
||||||
# Calls to 'reverse' will not be fully qualified unless we set the scheme/host/port here.
|
|
||||||
orig_prefix = get_script_prefix()
|
|
||||||
if not (orig_prefix.startswith('http:') or orig_prefix.startswith('https:')):
|
|
||||||
prefix = '%s://%s' % (request.is_secure() and 'https' or 'http', request.get_host())
|
|
||||||
set_script_prefix(prefix + orig_prefix)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.initial(request, *args, **kwargs)
|
self.initial(request, *args, **kwargs)
|
||||||
|
|
||||||
|
@ -154,7 +164,6 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
|
||||||
except ErrorResponse, exc:
|
except ErrorResponse, exc:
|
||||||
response = exc.response
|
response = exc.response
|
||||||
|
|
||||||
set_script_prefix(orig_prefix)
|
|
||||||
return self.final(request, response, *args, **kwargs)
|
return self.final(request, response, *args, **kwargs)
|
||||||
|
|
||||||
def options(self, request, *args, **kwargs):
|
def options(self, request, *args, **kwargs):
|
||||||
|
@ -172,19 +181,6 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
|
||||||
response_obj['fields'] = field_name_types
|
response_obj['fields'] = field_name_types
|
||||||
return response_obj
|
return response_obj
|
||||||
|
|
||||||
def final(self, request, response, *args, **kargs):
|
|
||||||
"""
|
|
||||||
Hook for any code that needs to run after everything else in the view.
|
|
||||||
"""
|
|
||||||
# Always add these headers.
|
|
||||||
response.headers['Allow'] = ', '.join(self.allowed_methods)
|
|
||||||
# sample to allow caching using Vary http header
|
|
||||||
response.headers['Vary'] = 'Authenticate, Accept'
|
|
||||||
|
|
||||||
# merge with headers possibly set at some point in the view
|
|
||||||
response.headers.update(self.headers)
|
|
||||||
return self.render(response)
|
|
||||||
|
|
||||||
|
|
||||||
class ModelView(View):
|
class ModelView(View):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user