diff options
Diffstat (limited to 'examples/blogpost')
| -rw-r--r-- | examples/blogpost/models.py | 34 | ||||
| -rw-r--r-- | examples/blogpost/tests.py | 1 | ||||
| -rw-r--r-- | examples/blogpost/urls.py | 37 | ||||
| -rw-r--r-- | examples/blogpost/views.py | 32 |
4 files changed, 37 insertions, 67 deletions
diff --git a/examples/blogpost/models.py b/examples/blogpost/models.py index 01a91e15..c4925a15 100644 --- a/examples/blogpost/models.py +++ b/examples/blogpost/models.py @@ -12,6 +12,8 @@ RATING_CHOICES = ((0, 'Awful'), (3, 'Good'), (4, 'Excellent')) +MAX_POSTS = 10 + class BlogPost(models.Model): key = models.CharField(primary_key=True, max_length=64, default=uuid_str, editable=False) title = models.CharField(max_length=128) @@ -19,28 +21,13 @@ class BlogPost(models.Model): created = models.DateTimeField(auto_now_add=True) slug = models.SlugField(editable=False, default='') - class Meta: - ordering = ('created',) - - @models.permalink - def get_absolute_url(self): - return ('blog-post', (), {'key': self.key}) - - @property - @models.permalink - def comments_url(self): - """Link to a resource which lists all comments for this blog post.""" - return ('comments', (), {'blogpost': self.key}) - - def __unicode__(self): - return self.title - def save(self, *args, **kwargs): self.slug = slugify(self.title) super(self.__class__, self).save(*args, **kwargs) - for obj in self.__class__.objects.order_by('-pk')[10:]: + for obj in self.__class__.objects.order_by('-created')[MAX_POSTS:]: obj.delete() + class Comment(models.Model): blogpost = models.ForeignKey(BlogPost, editable=False, related_name='comments') username = models.CharField(max_length=128) @@ -48,16 +35,3 @@ class Comment(models.Model): rating = models.IntegerField(blank=True, null=True, choices=RATING_CHOICES, help_text='How did you rate this post?') created = models.DateTimeField(auto_now_add=True) - class Meta: - ordering = ('created',) - - @models.permalink - def get_absolute_url(self): - return ('comment', (), {'blogpost': self.blogpost.key, 'id': self.id}) - - @property - @models.permalink - def blogpost_url(self): - """Link to the blog post resource which this comment corresponds to.""" - return ('blog-post', (), {'key': self.blogpost.key}) - diff --git a/examples/blogpost/tests.py b/examples/blogpost/tests.py index d4084e72..9b9a682f 100644 --- a/examples/blogpost/tests.py +++ b/examples/blogpost/tests.py @@ -1,6 +1,7 @@ """Test a range of REST API usage of the example application. """ +from django.core.urlresolvers import reverse from django.test import TestCase from django.core.urlresolvers import reverse from django.utils import simplejson as json diff --git a/examples/blogpost/urls.py b/examples/blogpost/urls.py index 1306b0d7..c677b8fa 100644 --- a/examples/blogpost/urls.py +++ b/examples/blogpost/urls.py @@ -1,9 +1,36 @@ from django.conf.urls.defaults import patterns, url -from blogpost.views import BlogPosts, BlogPostInstance, Comments, CommentInstance +from django.core.urlresolvers import reverse + +from djangorestframework.views import ListOrCreateModelView, InstanceModelView +from djangorestframework.resources import ModelResource + +from blogpost.models import BlogPost, Comment + + +class BlogPostResource(ModelResource): + """ + A Blog Post has a *title* and *content*, and can be associated with zero or more comments. + """ + model = BlogPost + fields = ('created', 'title', 'slug', 'content', 'url', 'comments') + ordering = ('-created',) + + def comments(self, instance): + return reverse('comments', kwargs={'blogpost': instance.key}) + + +class CommentResource(ModelResource): + """ + A Comment is associated with a given Blog Post and has a *username* and *comment*, and optionally a *rating*. + """ + model = Comment + fields = ('username', 'comment', 'created', 'rating', 'url', 'blogpost') + ordering = ('-created',) + urlpatterns = patterns('', - url(r'^$', BlogPosts.as_view(), name='blog-posts'), - url(r'^(?P<key>[^/]+)/$', BlogPostInstance.as_view(), name='blog-post'), - url(r'^(?P<blogpost>[^/]+)/comments/$', Comments.as_view(), name='comments'), - url(r'^(?P<blogpost>[^/]+)/comments/(?P<id>[^/]+)/$', CommentInstance.as_view(), name='comment'), + url(r'^$', ListOrCreateModelView.as_view(resource=BlogPostResource), name='blog-posts-root'), + url(r'^(?P<key>[^/]+)/$', InstanceModelView.as_view(resource=BlogPostResource)), + url(r'^(?P<blogpost>[^/]+)/comments/$', ListOrCreateModelView.as_view(resource=CommentResource), name='comments'), + url(r'^(?P<blogpost>[^/]+)/comments/(?P<id>[^/]+)/$', InstanceModelView.as_view(resource=CommentResource)), ) diff --git a/examples/blogpost/views.py b/examples/blogpost/views.py deleted file mode 100644 index 59a3fb9f..00000000 --- a/examples/blogpost/views.py +++ /dev/null @@ -1,32 +0,0 @@ -from djangorestframework.modelresource import ModelResource, RootModelResource - -from blogpost import models - -BLOG_POST_FIELDS = ('created', 'title', 'slug', 'content', 'absolute_url', 'comment_url', 'comments_url') -COMMENT_FIELDS = ('username', 'comment', 'created', 'rating', 'absolute_url', 'blogpost_url') -MAX_POSTS = 10 - -class BlogPosts(RootModelResource): - """A resource with which lists all existing blog posts and creates new blog posts.""" - anon_allowed_methods = allowed_methods = ('GET', 'POST',) - model = models.BlogPost - fields = BLOG_POST_FIELDS - -class BlogPostInstance(ModelResource): - """A resource which represents a single blog post.""" - anon_allowed_methods = allowed_methods = ('GET', 'PUT', 'DELETE') - model = models.BlogPost - fields = BLOG_POST_FIELDS - -class Comments(RootModelResource): - """A resource which lists all existing comments for a given blog post, and creates new blog comments for a given blog post.""" - anon_allowed_methods = allowed_methods = ('GET', 'POST',) - model = models.Comment - fields = COMMENT_FIELDS - -class CommentInstance(ModelResource): - """A resource which represents a single comment.""" - anon_allowed_methods = allowed_methods = ('GET', 'PUT', 'DELETE') - model = models.Comment - fields = COMMENT_FIELDS - |
