made some changes according to Tom's comments

This commit is contained in:
Sébastien Piquemal 2012-01-06 00:02:59 +02:00
parent 745e6f3386
commit 587c890ef1

View File

@ -7,15 +7,7 @@ from djangorestframework.response import ErrorResponse
from djangorestframework.serializer import Serializer, _SkipField
def bound_resource_required(meth):
def _decorated(self, *args, **kwargs):
if not self.is_bound():
raise Exception("resource needs to be bound") #TODO: what exception?
return meth(self, *args, **kwargs)
return _decorated
class BaseResource(Serializer):
class BaseResource(object):
"""
Base class for all Resource classes, which simply defines the interface
they provide.
@ -38,13 +30,13 @@ class BaseResource(Serializer):
Typically raises a :exc:`response.ErrorResponse` with status code 400
(Bad Request) on failure.
"""
return data
raise NotImplementedError()
def filter_response(self, obj):
"""
Given the response content, filter it into a serializable object.
"""
return self.serialize(obj)
raise NotImplementedError()
def retrieve(self, *args, **kwargs):
raise NotImplementedError()
@ -52,15 +44,16 @@ class BaseResource(Serializer):
def create(self, *args, **kwargs):
raise NotImplementedError()
@bound_resource_required
def update(self, data, *args, **kwargs):
raise NotImplementedError()
@bound_resource_required
def delete(self, *args, **kwargs):
raise NotImplementedError()
@bound_resource_required
def list(self, *args, **kwargs):
# TODO: QuerysetResource instead !?
raise NotImplementedError()
def get_url(self):
raise NotImplementedError()
@ -68,7 +61,7 @@ class BaseResource(Serializer):
return not self.instance is None
class Resource(BaseResource):
class Resource(BaseResource, Serializer):
"""
A Resource determines how a python object maps to some serializable data.
Objects that a resource can act on include plain Python object instances,
@ -90,6 +83,12 @@ class Resource(BaseResource):
# you should explicitly set the fields attribute on your class.
fields = None
def validate_request(self, data, files=None):
return data
def filter_response(self, obj):
return self.serialize(obj)
class FormResource(Resource):
"""
@ -354,8 +353,12 @@ class ModelResource(FormResource):
self.instance.save()
return self.instance
@bound_resource_required
def update(self, data, *args, **kwargs):
# The resource needs to be bound to an
# instance, or updating is not possible
if not self.is_bound():
raise Exception("resource needs to be bound") #TODO: what exception?
model = self.get_model()
kwargs = self._clean_url_kwargs(kwargs)
data = dict(data, **kwargs)
@ -389,8 +392,12 @@ class ModelResource(FormResource):
self.instance.save()
return self.instance
@bound_resource_required
def delete(self, *args, **kwargs):
# The resource needs to be bound to an
# instance, or updating is not possible
if not self.is_bound():
raise Exception("resource needs to be bound") #TODO: what exception?
self.instance.delete()
return self.instance
@ -404,7 +411,6 @@ class ModelResource(FormResource):
queryset = queryset.order_by(ordering)
return queryset.filter(**kwargs)
@bound_resource_required
def get_url(self):
"""
Attempts to reverse resolve the url of the given model *instance* for
@ -416,6 +422,8 @@ class ModelResource(FormResource):
This method can be overridden if you need to set the resource url
reversing explicitly.
"""
if not self.is_bound():
raise Exception("resource needs to be bound") #TODO: what exception?
if not hasattr(self, 'view_callable'):
raise _SkipField