aboutsummaryrefslogtreecommitdiffstats
path: root/src/testapp/views.py
blob: dee0b19be3b4a9c65a046e9606c3ac7dfe6c14b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from rest.resource import Resource, ModelResource, QueryModelResource
from testapp.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_operations = ('read',)

    def read(self, headers={}, *args, **kwargs):
        return (200, {'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')

class BlogPostList(QueryModelResource):
    """A resource which lists all existing blog posts."""
    allowed_operations = ('read', )
    model = BlogPost
    fields = BLOG_POST_FIELDS

class BlogPostCreator(ModelResource):
    """A resource with which blog posts may be created."""
    allowed_operations = ('create',)
    model = BlogPost
    fields = BLOG_POST_FIELDS

class BlogPostInstance(ModelResource):
    """A resource which represents a single blog post."""
    allowed_operations = ('read', 'update', 'delete')
    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_operations = ('read', )
    model = Comment
    fields = COMMENT_FIELDS

class CommentCreator(ModelResource):
    """A resource with which blog comments may be created for a given blog post."""
    allowed_operations = ('create',)
    model = Comment
    fields = COMMENT_FIELDS

class CommentInstance(ModelResource):
    """A resource which represents a single comment."""
    allowed_operations = ('read', 'update', 'delete')
    model = Comment
    fields = COMMENT_FIELDS
  

  
#
#'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',)

#######################