2011-06-15 17:09:57 +04:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
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):
|
2011-12-29 17:31:12 +04:00
|
|
|
return reverse('comments', kwargs={'blogpost': instance.key})
|
2011-06-15 17:09:57 +04:00
|
|
|
|
|
|
|
|
|
|
|
class CommentResource(ModelResource):
|
|
|
|
"""
|
2011-12-29 17:31:12 +04:00
|
|
|
A Comment is associated with a given Blog Post and has a *username* and *comment*, and optionally a *rating*.
|
2011-06-15 17:09:57 +04:00
|
|
|
"""
|
|
|
|
model = Comment
|
|
|
|
fields = ('username', 'comment', 'created', 'rating', 'url', 'blogpost')
|
|
|
|
ordering = ('-created',)
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-06-15 17:09:57 +04:00
|
|
|
def blogpost(self, instance):
|
2011-12-29 17:31:12 +04:00
|
|
|
return reverse('blog-post', kwargs={'key': instance.blogpost.key})
|