aboutsummaryrefslogtreecommitdiffstats
path: root/examples/resourceexample/views.py
blob: 29651fbf5da57b1ee8eca20b7eca36b1055a77f6 (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
from django.core.urlresolvers import reverse

from djangorestframework.views import View
from djangorestframework.resources import FormResource
from djangorestframework.response import Response
from djangorestframework import status

from resourceexample.forms import MyForm

class MyFormValidation(FormResource):
    """
    A resource which applies form validation on the input.
    """
    form = MyForm

    
class ExampleResource(View):
    """
    A basic read-only resource that points to 3 other resources.
    """

    def get(self, request):
        return {"Some other resources": [reverse('another-example-resource', kwargs={'num':num}) for num in range(3)]}


class AnotherExampleResource(View):
    """
    A basic GET-able/POST-able resource.
    """
    resource = MyFormValidation

    def get(self, request, num):
        """Handle GET requests"""
        if int(num) > 2:
            return Response(status.HTTP_404_NOT_FOUND)
        return "GET request to AnotherExampleResource %s" % num
    
    def post(self, request, num):
        """Handle POST requests"""
        if int(num) > 2:
            return Response(status.HTTP_404_NOT_FOUND)
        return "POST request to AnotherExampleResource %s, with content: %s" % (num, repr(self.CONTENT))