mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-04 20:33:16 +03:00
tidy up
This commit is contained in:
parent
4692374e0d
commit
84a4fd3ea1
|
@ -341,6 +341,19 @@ class ModelResource(Resource):
|
||||||
return _any(data, self.fields)
|
return _any(data, self.fields)
|
||||||
|
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
try:
|
||||||
|
if args:
|
||||||
|
# If we have any none kwargs then assume the last represents the primrary key
|
||||||
|
instance = self.model.objects.get(pk=args[-1], **kwargs)
|
||||||
|
else:
|
||||||
|
# Otherwise assume the kwargs uniquely identify the model
|
||||||
|
instance = self.model.objects.get(**kwargs)
|
||||||
|
except self.model.DoesNotExist:
|
||||||
|
raise ErrorResponse(status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
return instance
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
# TODO: test creation on a non-existing resource url
|
# TODO: test creation on a non-existing resource url
|
||||||
|
|
||||||
|
@ -361,19 +374,6 @@ class ModelResource(Resource):
|
||||||
headers['Location'] = instance.get_absolute_url()
|
headers['Location'] = instance.get_absolute_url()
|
||||||
return Response(status.HTTP_201_CREATED, instance, headers)
|
return Response(status.HTTP_201_CREATED, instance, headers)
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
|
||||||
try:
|
|
||||||
if args:
|
|
||||||
# If we have any none kwargs then assume the last represents the primrary key
|
|
||||||
instance = self.model.objects.get(pk=args[-1], **kwargs)
|
|
||||||
else:
|
|
||||||
# Otherwise assume the kwargs uniquely identify the model
|
|
||||||
instance = self.model.objects.get(**kwargs)
|
|
||||||
except self.model.DoesNotExist:
|
|
||||||
raise ErrorResponse(status.HTTP_404_NOT_FOUND)
|
|
||||||
|
|
||||||
return instance
|
|
||||||
|
|
||||||
def put(self, request, *args, **kwargs):
|
def put(self, request, *args, **kwargs):
|
||||||
# TODO: update on the url of a non-existing resource url doesn't work correctly at the moment - will end up with a new url
|
# TODO: update on the url of a non-existing resource url doesn't work correctly at the moment - will end up with a new url
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -16,8 +16,8 @@ __all__ = ['Resource']
|
||||||
|
|
||||||
|
|
||||||
class Resource(RequestMixin, ResponseMixin, AuthMixin, View):
|
class Resource(RequestMixin, ResponseMixin, AuthMixin, View):
|
||||||
"""Handles incoming requests and maps them to REST operations,
|
"""Handles incoming requests and maps them to REST operations.
|
||||||
performing authentication, input deserialization, input validation, output serialization."""
|
Performs request deserialization, response serialization, authentication and input validation."""
|
||||||
|
|
||||||
http_method_names = ['get', 'post', 'put', 'delete', 'head', 'options', 'trace', 'patch']
|
http_method_names = ['get', 'post', 'put', 'delete', 'head', 'options', 'trace', 'patch']
|
||||||
|
|
||||||
|
@ -73,21 +73,10 @@ class Resource(RequestMixin, ResponseMixin, AuthMixin, View):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
# Session based authentication is explicitly CSRF validated, all other authentication is CSRF exempt.
|
# Note: session based authentication is explicitly CSRF validated,
|
||||||
|
# all other authentication is CSRF exempt.
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
"""This method is the core of Resource, through which all requests are passed.
|
|
||||||
|
|
||||||
Broadly this consists of the following procedure:
|
|
||||||
|
|
||||||
0. ensure the operation is permitted
|
|
||||||
1. deserialize request content into request data, using standard HTTP content types (PUT/POST only)
|
|
||||||
2. cleanup and validate request data (PUT/POST only)
|
|
||||||
3. call the core method to get the response data
|
|
||||||
4. cleanup the response data
|
|
||||||
5. serialize response data into response content, using standard HTTP content negotiation
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
self.request = request
|
self.request = request
|
||||||
self.args = args
|
self.args = args
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
|
@ -134,7 +123,5 @@ class Resource(RequestMixin, ResponseMixin, AuthMixin, View):
|
||||||
response.headers['Vary'] = 'Authenticate, Accept'
|
response.headers['Vary'] = 'Authenticate, Accept'
|
||||||
|
|
||||||
return self.emit(response)
|
return self.emit(response)
|
||||||
except:
|
|
||||||
import traceback
|
|
||||||
traceback.print_exc()
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user