2011-02-19 13:26:27 +03:00
|
|
|
from django.core.urlresolvers import reverse
|
2011-02-19 13:47:26 +03:00
|
|
|
|
2011-05-24 13:27:24 +04:00
|
|
|
from djangorestframework.views import View
|
2011-05-16 17:11:36 +04:00
|
|
|
from djangorestframework.resources import FormResource
|
2011-02-19 13:47:26 +03:00
|
|
|
from djangorestframework.response import Response
|
|
|
|
from djangorestframework import status
|
|
|
|
|
2011-02-02 02:07:55 +03:00
|
|
|
from resourceexample.forms import MyForm
|
2011-02-02 01:37:51 +03:00
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
class MyFormValidation(FormResource):
|
|
|
|
"""
|
|
|
|
A resource which applies form validation on the input.
|
|
|
|
"""
|
|
|
|
form = MyForm
|
|
|
|
|
|
|
|
|
2011-05-24 13:27:24 +04:00
|
|
|
class ExampleResource(View):
|
2011-05-16 17:11:36 +04:00
|
|
|
"""
|
|
|
|
A basic read-only resource that points to 3 other resources.
|
|
|
|
"""
|
2011-02-02 01:37:51 +03:00
|
|
|
|
2011-04-27 21:07:28 +04:00
|
|
|
def get(self, request):
|
2011-02-19 13:26:27 +03:00
|
|
|
return {"Some other resources": [reverse('another-example-resource', kwargs={'num':num}) for num in range(3)]}
|
2011-02-02 01:37:51 +03:00
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
|
2011-05-24 13:27:24 +04:00
|
|
|
class AnotherExampleResource(View):
|
2011-05-16 17:11:36 +04:00
|
|
|
"""
|
|
|
|
A basic GET-able/POST-able resource.
|
|
|
|
"""
|
|
|
|
resource = MyFormValidation
|
2011-02-02 01:37:51 +03:00
|
|
|
|
2011-04-27 21:07:28 +04:00
|
|
|
def get(self, request, num):
|
2011-02-02 01:37:51 +03:00
|
|
|
"""Handle GET requests"""
|
|
|
|
if int(num) > 2:
|
|
|
|
return Response(status.HTTP_404_NOT_FOUND)
|
|
|
|
return "GET request to AnotherExampleResource %s" % num
|
|
|
|
|
2011-04-27 21:07:28 +04:00
|
|
|
def post(self, request, num):
|
2011-02-02 01:37:51 +03:00
|
|
|
"""Handle POST requests"""
|
|
|
|
if int(num) > 2:
|
|
|
|
return Response(status.HTTP_404_NOT_FOUND)
|
2011-04-27 21:07:28 +04:00
|
|
|
return "POST request to AnotherExampleResource %s, with content: %s" % (num, repr(self.CONTENT))
|