aboutsummaryrefslogtreecommitdiffstats
path: root/src/testapp/views.py
blob: 33e56bbd0fb21c59e1f73bd948df5687fa0b651a (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
from rest.resource import Resource, ModelResource
from testapp.forms import ExampleForm
from testapp.models import ExampleModel, ExampleContainer, BlogPost, Comment
 
class RootResource(Resource):
    """This is my docstring
    """
    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)}, {})


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