2011-02-19 13:26:27 +03:00
|
|
|
from django.conf.urls.defaults import patterns, url
|
2011-05-16 17:11:36 +04:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
|
|
|
from djangorestframework.views import ListOrCreateModelView, InstanceModelView
|
|
|
|
from djangorestframework.resources import ModelResource
|
|
|
|
|
|
|
|
from blogpost.models import BlogPost, Comment
|
|
|
|
|
2011-05-23 20:07:31 +04:00
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
class BlogPostResource(ModelResource):
|
2011-05-23 20:07:31 +04:00
|
|
|
"""
|
|
|
|
A Blog Post has a *title* and *content*, and can be associated with zero or more comments.
|
|
|
|
"""
|
2011-06-14 21:22:13 +04:00
|
|
|
class Meta:
|
|
|
|
model = BlogPost
|
|
|
|
fields = ('created', 'title', 'slug', 'content', 'url', 'comments')
|
|
|
|
ordering = ('-created',)
|
2011-05-16 17:11:36 +04:00
|
|
|
|
|
|
|
def comments(self, instance):
|
|
|
|
return reverse('comments', kwargs={'blogpost': instance.key})
|
|
|
|
|
2011-05-23 20:07:31 +04:00
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
class CommentResource(ModelResource):
|
2011-05-23 20:07:31 +04:00
|
|
|
"""
|
|
|
|
A Comment is associated with a given Blog Post and has a *username* and *comment*, and optionally a *rating*.
|
|
|
|
"""
|
2011-06-14 21:22:13 +04:00
|
|
|
class Meta:
|
|
|
|
model = Comment
|
|
|
|
fields = ('username', 'comment', 'created', 'rating', 'url', 'blogpost')
|
|
|
|
ordering = ('-created',)
|
2011-05-16 17:11:36 +04:00
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
urlpatterns = patterns('',
|
2011-05-16 17:11:36 +04:00
|
|
|
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)),
|
2011-05-23 20:07:31 +04:00
|
|
|
)
|