django-rest-framework/examples/requestexample/views.py

45 lines
1.4 KiB
Python
Raw Normal View History

2012-01-25 02:11:54 +04:00
from djangorestframework.compat import View
from django.http import HttpResponse
from djangorestframework.mixins import RequestMixin
from djangorestframework.utils import reverse
2012-01-25 02:11:54 +04:00
from djangorestframework.views import View as DRFView
from djangorestframework import parsers
from djangorestframework.response import Response
2012-01-25 02:11:54 +04:00
class RequestExampleView(DRFView):
"""
A container view for request examples.
"""
def get(self, request):
return Response([{'name': 'request.DATA Example', 'url': reverse('request-content', request)},])
2012-01-25 02:11:54 +04:00
class MyBaseViewUsingEnhancedRequest(RequestMixin, View):
"""
Base view enabling the usage of enhanced requests with user defined views.
"""
2012-02-07 18:22:14 +04:00
parser_classes = parsers.DEFAULT_PARSERS
2012-01-25 02:11:54 +04:00
def dispatch(self, request, *args, **kwargs):
2012-02-10 13:05:20 +04:00
self.request = request = self.create_request(request)
2012-01-25 02:11:54 +04:00
return super(MyBaseViewUsingEnhancedRequest, self).dispatch(request, *args, **kwargs)
class EchoRequestContentView(MyBaseViewUsingEnhancedRequest):
"""
A view that just reads the items in `request.DATA` and echoes them back.
"""
def post(self, request, *args, **kwargs):
return HttpResponse(("Found %s in request.DATA, content : %s" %
(type(request.DATA), request.DATA)))
def put(self, request, *args, **kwargs):
return HttpResponse(("Found %s in request.DATA, content : %s" %
(type(request.DATA), request.DATA)))