mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-18 04:02:35 +03:00
made some changes according to Tom's comments
This commit is contained in:
parent
745e6f3386
commit
587c890ef1
|
@ -7,15 +7,7 @@ from djangorestframework.response import ErrorResponse
|
||||||
from djangorestframework.serializer import Serializer, _SkipField
|
from djangorestframework.serializer import Serializer, _SkipField
|
||||||
|
|
||||||
|
|
||||||
def bound_resource_required(meth):
|
class BaseResource(object):
|
||||||
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):
|
|
||||||
"""
|
"""
|
||||||
Base class for all Resource classes, which simply defines the interface
|
Base class for all Resource classes, which simply defines the interface
|
||||||
they provide.
|
they provide.
|
||||||
|
@ -38,13 +30,13 @@ class BaseResource(Serializer):
|
||||||
Typically raises a :exc:`response.ErrorResponse` with status code 400
|
Typically raises a :exc:`response.ErrorResponse` with status code 400
|
||||||
(Bad Request) on failure.
|
(Bad Request) on failure.
|
||||||
"""
|
"""
|
||||||
return data
|
raise NotImplementedError()
|
||||||
|
|
||||||
def filter_response(self, obj):
|
def filter_response(self, obj):
|
||||||
"""
|
"""
|
||||||
Given the response content, filter it into a serializable object.
|
Given the response content, filter it into a serializable object.
|
||||||
"""
|
"""
|
||||||
return self.serialize(obj)
|
raise NotImplementedError()
|
||||||
|
|
||||||
def retrieve(self, *args, **kwargs):
|
def retrieve(self, *args, **kwargs):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
@ -52,15 +44,16 @@ class BaseResource(Serializer):
|
||||||
def create(self, *args, **kwargs):
|
def create(self, *args, **kwargs):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
@bound_resource_required
|
|
||||||
def update(self, data, *args, **kwargs):
|
def update(self, data, *args, **kwargs):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
@bound_resource_required
|
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
@bound_resource_required
|
def list(self, *args, **kwargs):
|
||||||
|
# TODO: QuerysetResource instead !?
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
def get_url(self):
|
def get_url(self):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
@ -68,7 +61,7 @@ class BaseResource(Serializer):
|
||||||
return not self.instance is None
|
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.
|
A Resource determines how a python object maps to some serializable data.
|
||||||
Objects that a resource can act on include plain Python object instances,
|
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.
|
# you should explicitly set the fields attribute on your class.
|
||||||
fields = None
|
fields = None
|
||||||
|
|
||||||
|
def validate_request(self, data, files=None):
|
||||||
|
return data
|
||||||
|
|
||||||
|
def filter_response(self, obj):
|
||||||
|
return self.serialize(obj)
|
||||||
|
|
||||||
|
|
||||||
class FormResource(Resource):
|
class FormResource(Resource):
|
||||||
"""
|
"""
|
||||||
|
@ -354,8 +353,12 @@ class ModelResource(FormResource):
|
||||||
self.instance.save()
|
self.instance.save()
|
||||||
return self.instance
|
return self.instance
|
||||||
|
|
||||||
@bound_resource_required
|
|
||||||
def update(self, data, *args, **kwargs):
|
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()
|
model = self.get_model()
|
||||||
kwargs = self._clean_url_kwargs(kwargs)
|
kwargs = self._clean_url_kwargs(kwargs)
|
||||||
data = dict(data, **kwargs)
|
data = dict(data, **kwargs)
|
||||||
|
@ -389,8 +392,12 @@ class ModelResource(FormResource):
|
||||||
self.instance.save()
|
self.instance.save()
|
||||||
return self.instance
|
return self.instance
|
||||||
|
|
||||||
@bound_resource_required
|
|
||||||
def delete(self, *args, **kwargs):
|
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()
|
self.instance.delete()
|
||||||
return self.instance
|
return self.instance
|
||||||
|
|
||||||
|
@ -404,7 +411,6 @@ class ModelResource(FormResource):
|
||||||
queryset = queryset.order_by(ordering)
|
queryset = queryset.order_by(ordering)
|
||||||
return queryset.filter(**kwargs)
|
return queryset.filter(**kwargs)
|
||||||
|
|
||||||
@bound_resource_required
|
|
||||||
def get_url(self):
|
def get_url(self):
|
||||||
"""
|
"""
|
||||||
Attempts to reverse resolve the url of the given model *instance* for
|
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
|
This method can be overridden if you need to set the resource url
|
||||||
reversing explicitly.
|
reversing explicitly.
|
||||||
"""
|
"""
|
||||||
|
if not self.is_bound():
|
||||||
|
raise Exception("resource needs to be bound") #TODO: what exception?
|
||||||
|
|
||||||
if not hasattr(self, 'view_callable'):
|
if not hasattr(self, 'view_callable'):
|
||||||
raise _SkipField
|
raise _SkipField
|
||||||
|
|
Loading…
Reference in New Issue
Block a user