2011-02-19 13:26:27 +03:00
|
|
|
from django.core.urlresolvers import set_script_prefix
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
from djangorestframework.compat import View
|
|
|
|
from djangorestframework.emitters import EmitterMixin
|
2011-02-19 13:47:26 +03:00
|
|
|
from djangorestframework.response import Response, ResponseException
|
2011-04-11 16:45:38 +04:00
|
|
|
from djangorestframework.request import RequestMixin, AuthMixin
|
2011-04-11 18:03:49 +04:00
|
|
|
from djangorestframework import emitters, parsers, authenticators, validators, status
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
# TODO: Figure how out references and named urls need to work nicely
|
|
|
|
# TODO: POST on existing 404 URL, PUT on existing 404 URL
|
|
|
|
#
|
|
|
|
# NEXT: Exceptions on func() -> 500, tracebacks emitted if settings.DEBUG
|
|
|
|
|
|
|
|
__all__ = ['Resource']
|
|
|
|
|
|
|
|
|
2011-04-11 18:03:49 +04:00
|
|
|
class Resource(EmitterMixin, AuthMixin, RequestMixin, View):
|
2011-01-24 02:08:16 +03:00
|
|
|
"""Handles incoming requests and maps them to REST operations,
|
|
|
|
performing authentication, input deserialization, input validation, output serialization."""
|
|
|
|
|
|
|
|
# List of RESTful operations which may be performed on this resource.
|
2011-04-02 19:32:37 +04:00
|
|
|
# These are going to get dropped at some point, the allowable methods will be defined simply by
|
|
|
|
# which methods are present on the request (in the same way as Django's generic View)
|
2011-01-24 02:08:16 +03:00
|
|
|
allowed_methods = ('GET',)
|
|
|
|
anon_allowed_methods = ()
|
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
# List of emitters the resource can serialize the response with, ordered by preference.
|
2011-01-24 02:08:16 +03:00
|
|
|
emitters = ( emitters.JSONEmitter,
|
|
|
|
emitters.DocumentingHTMLEmitter,
|
|
|
|
emitters.DocumentingXHTMLEmitter,
|
|
|
|
emitters.DocumentingPlainTextEmitter,
|
|
|
|
emitters.XMLEmitter )
|
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
# List of parsers the resource can parse the request with.
|
2011-01-24 02:08:16 +03:00
|
|
|
parsers = ( parsers.JSONParser,
|
2011-02-15 11:19:57 +03:00
|
|
|
parsers.FormParser,
|
|
|
|
parsers.MultipartParser )
|
2011-04-11 18:03:49 +04:00
|
|
|
|
|
|
|
# List of validators to validate, cleanup and type-ify the request content
|
|
|
|
validators = (validators.FormValidator,)
|
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
# List of all authenticating methods to attempt.
|
2011-01-24 21:59:23 +03:00
|
|
|
authenticators = ( authenticators.UserLoggedInAuthenticator,
|
|
|
|
authenticators.BasicAuthenticator )
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
# Optional form for input validation and presentation of HTML formatted responses.
|
|
|
|
form = None
|
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
# Allow name and description for the Resource to be set explicitly,
|
|
|
|
# overiding the default classname/docstring behaviour.
|
|
|
|
# These are used for documentation in the standard html and text emitters.
|
|
|
|
name = None
|
|
|
|
description = None
|
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
# Map standard HTTP methods to function calls
|
|
|
|
callmap = { 'GET': 'get', 'POST': 'post',
|
|
|
|
'PUT': 'put', 'DELETE': 'delete' }
|
|
|
|
|
2011-01-24 21:59:23 +03:00
|
|
|
def get(self, request, auth, *args, **kwargs):
|
2011-01-24 02:08:16 +03:00
|
|
|
"""Must be subclassed to be implemented."""
|
|
|
|
self.not_implemented('GET')
|
|
|
|
|
|
|
|
|
2011-01-24 21:59:23 +03:00
|
|
|
def post(self, request, auth, content, *args, **kwargs):
|
2011-01-24 02:08:16 +03:00
|
|
|
"""Must be subclassed to be implemented."""
|
|
|
|
self.not_implemented('POST')
|
|
|
|
|
|
|
|
|
2011-01-24 21:59:23 +03:00
|
|
|
def put(self, request, auth, content, *args, **kwargs):
|
2011-01-24 02:08:16 +03:00
|
|
|
"""Must be subclassed to be implemented."""
|
|
|
|
self.not_implemented('PUT')
|
|
|
|
|
|
|
|
|
2011-01-24 21:59:23 +03:00
|
|
|
def delete(self, request, auth, *args, **kwargs):
|
2011-01-24 02:08:16 +03:00
|
|
|
"""Must be subclassed to be implemented."""
|
|
|
|
self.not_implemented('DELETE')
|
|
|
|
|
|
|
|
|
|
|
|
def not_implemented(self, operation):
|
|
|
|
"""Return an HTTP 500 server error if an operation is called which has been allowed by
|
|
|
|
allowed_methods, but which has not been implemented."""
|
|
|
|
raise ResponseException(status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
{'detail': '%s operation on this resource has not been implemented' % (operation, )})
|
|
|
|
|
|
|
|
|
2011-01-24 21:59:23 +03:00
|
|
|
def check_method_allowed(self, method, auth):
|
2011-01-26 11:58:09 +03:00
|
|
|
"""Ensure the request method is permitted for this resource, raising a ResourceException if it is not."""
|
2011-01-24 21:59:23 +03:00
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
if not method in self.callmap.keys():
|
|
|
|
raise ResponseException(status.HTTP_501_NOT_IMPLEMENTED,
|
|
|
|
{'detail': 'Unknown or unsupported method \'%s\'' % method})
|
2011-01-24 21:59:23 +03:00
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
if not method in self.allowed_methods:
|
|
|
|
raise ResponseException(status.HTTP_405_METHOD_NOT_ALLOWED,
|
|
|
|
{'detail': 'Method \'%s\' not allowed on this resource.' % method})
|
|
|
|
|
2011-01-26 11:58:09 +03:00
|
|
|
if auth is None and not method in self.anon_allowed_methods:
|
|
|
|
raise ResponseException(status.HTTP_403_FORBIDDEN,
|
|
|
|
{'detail': 'You do not have permission to access this resource. ' +
|
|
|
|
'You may need to login or otherwise authenticate the request.'})
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
def cleanup_response(self, data):
|
|
|
|
"""Perform any resource-specific data filtering prior to the standard HTTP
|
|
|
|
content-type serialization.
|
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
Eg filter complex objects that cannot be serialized by json/xml/etc into basic objects that can.
|
|
|
|
|
|
|
|
TODO: This is going to be removed. I think that the 'fields' behaviour is going to move into
|
|
|
|
the EmitterMixin and Emitter classes."""
|
2011-01-24 02:08:16 +03:00
|
|
|
return data
|
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
# Session based authentication is explicitly CSRF validated, all other authentication is CSRF exempt.
|
|
|
|
@csrf_exempt
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2011-01-26 11:58:09 +03:00
|
|
|
"""This method is the core of Resource, through which all requests are passed.
|
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
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
|
|
|
|
"""
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
self.request = request
|
|
|
|
|
|
|
|
# Calls to 'reverse' will not be fully qualified unless we set the scheme/host/port here.
|
|
|
|
prefix = '%s://%s' % (request.is_secure() and 'https' or 'http', request.get_host())
|
|
|
|
set_script_prefix(prefix)
|
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
try:
|
|
|
|
# Authenticate the request, and store any context so that the resource operations can
|
|
|
|
# do more fine grained authentication if required.
|
|
|
|
#
|
|
|
|
# Typically the context will be a user, or None if this is an anonymous request,
|
|
|
|
# but it could potentially be more complex (eg the context of a request key which
|
|
|
|
# has been signed against a particular set of permissions)
|
2011-04-11 16:45:38 +04:00
|
|
|
auth_context = self.auth
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
# If using a form POST with '_method'/'_content'/'_content_type' overrides, then alter
|
|
|
|
# self.method, self.content_type, self.CONTENT appropriately.
|
|
|
|
self.perform_form_overloading()
|
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
# Ensure the requested operation is permitted on this resource
|
2011-04-02 19:32:37 +04:00
|
|
|
self.check_method_allowed(self.method, auth_context)
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
# Get the appropriate create/read/update/delete function
|
2011-04-02 19:32:37 +04:00
|
|
|
func = getattr(self, self.callmap.get(self.method, None))
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
# Either generate the response data, deserializing and validating any request data
|
2011-04-02 19:32:37 +04:00
|
|
|
# TODO: This is going to change to: func(request, *args, **kwargs)
|
|
|
|
# That'll work out now that we have the lazily evaluated self.CONTENT property.
|
|
|
|
if self.method in ('PUT', 'POST'):
|
|
|
|
response_obj = func(request, auth_context, self.CONTENT, *args, **kwargs)
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
else:
|
2011-02-19 13:26:27 +03:00
|
|
|
response_obj = func(request, auth_context, *args, **kwargs)
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
# Allow return value to be either Response, or an object, or None
|
2011-02-19 13:26:27 +03:00
|
|
|
if isinstance(response_obj, Response):
|
|
|
|
response = response_obj
|
|
|
|
elif response_obj is not None:
|
|
|
|
response = Response(status.HTTP_200_OK, response_obj)
|
2011-01-24 02:08:16 +03:00
|
|
|
else:
|
2011-02-19 13:26:27 +03:00
|
|
|
response = Response(status.HTTP_204_NO_CONTENT)
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
# Pre-serialize filtering (eg filter complex objects into natively serializable types)
|
2011-02-19 13:26:27 +03:00
|
|
|
response.cleaned_content = self.cleanup_response(response.raw_content)
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
except ResponseException, exc:
|
2011-02-19 13:26:27 +03:00
|
|
|
response = exc.response
|
2011-04-11 16:45:38 +04:00
|
|
|
|
|
|
|
except:
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
# Always add these headers.
|
|
|
|
#
|
|
|
|
# TODO - this isn't actually the correct way to set the vary header,
|
|
|
|
# also it's currently sub-obtimal for HTTP caching - need to sort that out.
|
2011-02-19 13:26:27 +03:00
|
|
|
response.headers['Allow'] = ', '.join(self.allowed_methods)
|
2011-03-03 11:02:32 +03:00
|
|
|
response.headers['Vary'] = 'Authenticate, Accept'
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
return self.emit(response)
|
2011-01-24 02:08:16 +03:00
|
|
|
|