Fix up ModelResource issues

This commit is contained in:
Tom Christie 2011-04-27 18:53:54 +01:00
parent 5a59f339c1
commit 5921e5c84e
4 changed files with 12 additions and 10 deletions

View File

@ -15,8 +15,10 @@ from djangorestframework.breadcrumbs import get_breadcrumbs
from djangorestframework.description import get_name, get_description
from djangorestframework import status
from decimal import Decimal
from urllib import quote_plus
import string
import re
from decimal import Decimal
# TODO: Rename verbose to something more appropriate
# TODO: Maybe None could be handled more cleanly. It'd be nice if it was handled by default,

View File

@ -408,6 +408,9 @@ class ModelResource(Resource):
return
class InstanceModelResource(ModelResource):
http_method_names = ['get', 'put', 'delete', 'head', 'options', 'trace', 'patch'] # Bit of a hack, these - needs fixing.
class RootModelResource(ModelResource):
"""A Resource which provides default operations for list and create."""
queryset = None
@ -416,7 +419,7 @@ class RootModelResource(ModelResource):
queryset = self.queryset if self.queryset else self.model.objects.all()
return queryset.filter(**kwargs)
put = delete = None
http_method_names = ['get', 'post', 'head', 'options', 'trace', 'patch']
class QueryModelResource(ModelResource):
"""Resource with default operations for list.
@ -428,4 +431,4 @@ class QueryModelResource(ModelResource):
queryset = self.queryset if self.queryset else self.model.objects.all()
return queryset.filer(**kwargs)
post = put = delete = None
http_method_names = ['get', 'head', 'options', 'trace', 'patch']

View File

@ -54,7 +54,7 @@ class Resource(RequestMixin, ResponseMixin, AuthMixin, View):
@property
def allowed_methods(self):
return [method.upper() for method in self.http_method_names if getattr(self, method, None)]
return [method.upper() for method in self.http_method_names if hasattr(self, method)]
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."""
@ -97,9 +97,6 @@ class Resource(RequestMixin, ResponseMixin, AuthMixin, View):
# Get the appropriate handler method
if self.method.lower() in self.http_method_names:
handler = getattr(self, self.method.lower(), self.http_method_not_allowed)
# If a previously defined method has been disabled
if handler is None:
handler = self.http_method_not_allowed
else:
handler = self.http_method_not_allowed

View File

@ -1,4 +1,4 @@
from djangorestframework.modelresource import ModelResource, RootModelResource
from djangorestframework.modelresource import InstanceModelResource, RootModelResource
from blogpost import models
@ -11,7 +11,7 @@ class BlogPosts(RootModelResource):
model = models.BlogPost
fields = BLOG_POST_FIELDS
class BlogPostInstance(ModelResource):
class BlogPostInstance(InstanceModelResource):
"""A resource which represents a single blog post."""
model = models.BlogPost
fields = BLOG_POST_FIELDS
@ -21,7 +21,7 @@ class Comments(RootModelResource):
model = models.Comment
fields = COMMENT_FIELDS
class CommentInstance(ModelResource):
class CommentInstance(InstanceModelResource):
"""A resource which represents a single comment."""
model = models.Comment
fields = COMMENT_FIELDS