aboutsummaryrefslogtreecommitdiffstats
path: root/examples/blogpost
diff options
context:
space:
mode:
authortom christie tom@tomchristie.com2011-01-30 11:00:20 +0000
committertom christie tom@tomchristie.com2011-01-30 11:00:20 +0000
commit250ab0f609f32cd3e004e1f2711f9c2e4fd9b57c (patch)
tree915eeda0528dab3159958500c117e9285bc56ef3 /examples/blogpost
parent40f47a9fb31aebd965dce03ae57c036d5360d124 (diff)
downloaddjango-rest-framework-250ab0f609f32cd3e004e1f2711f9c2e4fd9b57c.tar.bz2
Lots of docs, trying to tidy up examples...
Diffstat (limited to 'examples/blogpost')
-rw-r--r--examples/blogpost/models.py8
-rw-r--r--examples/blogpost/urls.py11
-rw-r--r--examples/blogpost/views.py46
3 files changed, 13 insertions, 52 deletions
diff --git a/examples/blogpost/models.py b/examples/blogpost/models.py
index 1690245c..e1968415 100644
--- a/examples/blogpost/models.py
+++ b/examples/blogpost/models.py
@@ -30,13 +30,7 @@ class BlogPost(models.Model):
@models.permalink
def comments_url(self):
"""Link to a resource which lists all comments for this blog post."""
- return ('blogpost.views.CommentList', (), {'blogpost_id': self.key})
-
- @property
- @models.permalink
- def comment_url(self):
- """Link to a resource which can create a comment for this blog post."""
- return ('blogpost.views.CommentCreator', (), {'blogpost_id': self.key})
+ return ('blogpost.views.CommentRoot', (), {'blogpost_id': self.key})
def __unicode__(self):
return self.title
diff --git a/examples/blogpost/urls.py b/examples/blogpost/urls.py
index eccbae15..ee209b3e 100644
--- a/examples/blogpost/urls.py
+++ b/examples/blogpost/urls.py
@@ -1,11 +1,8 @@
from django.conf.urls.defaults import patterns
urlpatterns = patterns('blogpost.views',
- (r'^$', 'RootResource'),
- (r'^blog-posts/$', 'BlogPostList'),
- (r'^blog-post/$', 'BlogPostCreator'),
- (r'^blog-post/(?P<key>[^/]+)/$', 'BlogPostInstance'),
- (r'^blog-post/(?P<blogpost_id>[^/]+)/comments/$', 'CommentList'),
- (r'^blog-post/(?P<blogpost_id>[^/]+)/comment/$', 'CommentCreator'),
- (r'^blog-post/(?P<blogpost>[^/]+)/comments/(?P<id>[^/]+)/$', 'CommentInstance'),
+ (r'^$', 'BlogPostRoot'),
+ (r'^(?P<key>[^/]+)/$', 'BlogPostInstance'),
+ (r'^(?P<blogpost_id>[^/]+)/comments/$', 'CommentRoot'),
+ (r'^(?P<blogpost>[^/]+)/comments/(?P<id>[^/]+)/$', 'CommentInstance'),
)
diff --git a/examples/blogpost/views.py b/examples/blogpost/views.py
index 05e795fa..c5be2544 100644
--- a/examples/blogpost/views.py
+++ b/examples/blogpost/views.py
@@ -1,34 +1,15 @@
from flywheel.response import Response, status
from flywheel.resource import Resource
-from flywheel.modelresource import ModelResource, QueryModelResource
+from flywheel.modelresource import ModelResource, RootModelResource
from blogpost.models import BlogPost, Comment
-##### Root Resource #####
-
-class RootResource(Resource):
- """This is the top level resource for the API.
- All the sub-resources are discoverable from here."""
- allowed_methods = ('GET',)
-
- def get(self, request, *args, **kwargs):
- return Response(status.HTTP_200_OK,
- {'blog-posts': self.reverse(BlogPostList),
- 'blog-post': self.reverse(BlogPostCreator)})
-
-
-##### Blog Post Resources #####
-
BLOG_POST_FIELDS = ('created', 'title', 'slug', 'content', 'absolute_url', 'comment_url', 'comments_url')
+COMMENT_FIELDS = ('username', 'comment', 'created', 'rating', 'absolute_url', 'blogpost_url')
-class BlogPostList(QueryModelResource):
- """A resource which lists all existing blog posts."""
- allowed_methods = ('GET', )
- model = BlogPost
- fields = BLOG_POST_FIELDS
-class BlogPostCreator(ModelResource):
- """A resource with which blog posts may be created."""
- allowed_methods = ('POST',)
+class BlogPostRoot(RootModelResource):
+ """A resource with which lists all existing blog posts and creates new blog posts."""
+ allowed_methods = ('GET', 'POST',)
model = BlogPost
fields = BLOG_POST_FIELDS
@@ -38,20 +19,9 @@ class BlogPostInstance(ModelResource):
model = BlogPost
fields = BLOG_POST_FIELDS
-
-##### Comment Resources #####
-
-COMMENT_FIELDS = ('username', 'comment', 'created', 'rating', 'absolute_url', 'blogpost_url')
-
-class CommentList(QueryModelResource):
- """A resource which lists all existing comments for a given blog post."""
- allowed_methods = ('GET', )
- model = Comment
- fields = COMMENT_FIELDS
-
-class CommentCreator(ModelResource):
- """A resource with which blog comments may be created for a given blog post."""
- allowed_methods = ('POST',)
+class CommentRoot(RootModelResource):
+ """A resource which lists all existing comments for a given blog post, and creates new blog comments for a given blog post."""
+ allowed_methods = ('GET', 'POST',)
model = Comment
fields = COMMENT_FIELDS