diff options
Diffstat (limited to 'src/testapp/views.py')
| -rw-r--r-- | src/testapp/views.py | 143 |
1 files changed, 88 insertions, 55 deletions
diff --git a/src/testapp/views.py b/src/testapp/views.py index 33e56bbd..eca69cc3 100644 --- a/src/testapp/views.py +++ b/src/testapp/views.py @@ -1,78 +1,111 @@ -from rest.resource import Resource, ModelResource -from testapp.forms import ExampleForm -from testapp.models import ExampleModel, ExampleContainer, BlogPost, Comment +from rest.resource import Resource, ModelResource, QueryModelResource +from testapp.models import BlogPost, Comment class RootResource(Resource): - """This is my docstring - """ + """This is the top level resource for the API. + All the sub-resources are discoverable from here.""" allowed_operations = ('read',) def read(self, headers={}, *args, **kwargs): - return (200, {'read-only-api': self.reverse(ReadOnlyResource), - 'write-only-api': self.reverse(WriteOnlyResource), - 'read-write-api': self.reverse(ReadWriteResource), - 'model-api': self.reverse(ModelFormResource), - 'create-container': self.reverse(ContainerFactory), - 'blog-post-creator': self.reverse(BlogPostCreator)}, {}) + return (200, {'blog-posts': self.reverse(BlogPostList), + 'blog-post': self.reverse(BlogPostCreator)}, {}) -class ReadOnlyResource(Resource): - """This is my docstring - """ - allowed_operations = ('read',) - - def read(self, headers={}, *args, **kwargs): - return (200, {'ExampleString': 'Example', - 'ExampleInt': 1, - 'ExampleDecimal': 1.0}, {}) +# Blog Post Resources +class BlogPostList(QueryModelResource): + """A resource which lists all existing blog posts.""" + allowed_operations = ('read', ) + model = BlogPost -class WriteOnlyResource(Resource): - """This is my docstring - """ - allowed_operations = ('update',) - def update(self, data, headers={}, *args, **kwargs): - return (200, data, {}) +class BlogPostCreator(ModelResource): + """A resource with which blog posts may be created.""" + allowed_operations = ('create',) + model = BlogPost + fields = ('created', 'title', 'slug', 'content', 'absolute_url', 'comment_url', 'comments_url') -class ReadWriteResource(Resource): +class BlogPostInstance(ModelResource): + """A resource which represents a single blog post.""" allowed_operations = ('read', 'update', 'delete') - create_form = ExampleForm - update_form = ExampleForm + model = BlogPost + fields = ('created', 'title', 'slug', 'content', 'absolute_url', 'comment_url', 'comments_url') -class ModelFormResource(ModelResource): - allowed_operations = ('read', 'update', 'delete') - model = ExampleModel +# Comment Resources -# Nice things: form validation is applied to any input type -# html forms for output -# output always serialized nicely -class ContainerFactory(ModelResource): +class CommentList(QueryModelResource): + """A resource which lists all existing comments for a given blog post.""" + allowed_operations = ('read', ) + model = Comment + + +class CommentCreator(ModelResource): + """A resource with which blog comments may be created for a given blog post.""" allowed_operations = ('create',) - model = ExampleContainer - fields = ('absolute_uri', 'name', 'key') - form_fields = ('name',) + model = Comment + fields = ('username', 'comment', 'created', 'rating', 'absolute_url', 'blogpost_url') -class ContainerInstance(ModelResource): +class CommentInstance(ModelResource): + """A resource which represents a single comment.""" allowed_operations = ('read', 'update', 'delete') - model = ExampleContainer - fields = ('absolute_uri', 'name', 'key') - form_fields = ('name',) + model = Comment + fields = ('username', 'comment', 'created', 'rating', 'absolute_url', 'blogpost_url') + +# +#'read-only-api': self.reverse(ReadOnlyResource), +# 'write-only-api': self.reverse(WriteOnlyResource), +# 'read-write-api': self.reverse(ReadWriteResource), +# 'model-api': self.reverse(ModelFormResource), +# 'create-container': self.reverse(ContainerFactory), +# +#class ReadOnlyResource(Resource): +# """This is my docstring +# """ +# allowed_operations = ('read',) +# +# def read(self, headers={}, *args, **kwargs): +# return (200, {'ExampleString': 'Example', +# 'ExampleInt': 1, +# 'ExampleDecimal': 1.0}, {}) +# +# +#class WriteOnlyResource(Resource): +# """This is my docstring +# """ +# allowed_operations = ('update',) +# +# def update(self, data, headers={}, *args, **kwargs): +# return (200, data, {}) +# +# +#class ReadWriteResource(Resource): +# allowed_operations = ('read', 'update', 'delete') +# create_form = ExampleForm +# update_form = ExampleForm +# +# +#class ModelFormResource(ModelResource): +# allowed_operations = ('read', 'update', 'delete') +# model = ExampleModel +# +## Nice things: form validation is applied to any input type +## html forms for output +## output always serialized nicely +#class ContainerFactory(ModelResource): +# allowed_operations = ('create',) +# model = ExampleContainer +# fields = ('absolute_uri', 'name', 'key') +# form_fields = ('name',) +# +# +#class ContainerInstance(ModelResource): +# allowed_operations = ('read', 'update', 'delete') +# model = ExampleContainer +# fields = ('absolute_uri', 'name', 'key') +# form_fields = ('name',) ####################### - -class BlogPostCreator(ModelResource): - """A Resource with which blog posts may be created. - This is distinct from blog post instance so that it is discoverable by the client. - (ie the client doens't need to know how to form a blog post url in order to create a blog post)""" - allowed_operations = ('create',) - model = BlogPost - -class BlogPostInstance(ModelResource): - """Represents a single Blog Post.""" - allowed_operations = ('read', 'update', 'delete') - model = BlogPost
\ No newline at end of file |
