mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-19 04:53:20 +03:00
modified examples, somethin' still broken, can't find what
This commit is contained in:
parent
6963fd3623
commit
2cdff1b01e
|
@ -58,7 +58,7 @@ class RequestMixin(object):
|
||||||
to parse its content.
|
to parse its content.
|
||||||
"""
|
"""
|
||||||
if not hasattr(self, '_parsers'):
|
if not hasattr(self, '_parsers'):
|
||||||
self._parsers = [r(self) for r in self.parser_classes]
|
self._parsers = [p(self) for p in self.parser_classes]
|
||||||
return self._parsers
|
return self._parsers
|
||||||
|
|
||||||
def prepare_request(self, request):
|
def prepare_request(self, request):
|
||||||
|
|
|
@ -15,6 +15,7 @@ from django.views.decorators.csrf import csrf_exempt
|
||||||
from djangorestframework.compat import View as DjangoView, apply_markdown
|
from djangorestframework.compat import View as DjangoView, apply_markdown
|
||||||
from djangorestframework.response import Response, ImmediateResponse
|
from djangorestframework.response import Response, ImmediateResponse
|
||||||
from djangorestframework.mixins import *
|
from djangorestframework.mixins import *
|
||||||
|
from djangorestframework.utils import allowed_methods
|
||||||
from djangorestframework import resources, renderers, parsers, authentication, permissions, status
|
from djangorestframework import resources, renderers, parsers, authentication, permissions, status
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,12 @@ from django.core.urlresolvers import reverse
|
||||||
class ExampleView(ResponseMixin, View):
|
class ExampleView(ResponseMixin, View):
|
||||||
"""An example view using Django 1.3's class based views.
|
"""An example view using Django 1.3's class based views.
|
||||||
Uses djangorestframework's RendererMixin to provide support for multiple output formats."""
|
Uses djangorestframework's RendererMixin to provide support for multiple output formats."""
|
||||||
renderers = DEFAULT_RENDERERS
|
renderer_classes = DEFAULT_RENDERERS
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
response = Response(200, {'description': 'Some example content',
|
response = Response({'description': 'Some example content',
|
||||||
'url': reverse('mixin-view')})
|
'url': reverse('mixin-view')}, status=200)
|
||||||
return self.render(response)
|
return self.prepare_response(response)
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
|
|
|
@ -41,7 +41,7 @@ class ObjectStoreRoot(View):
|
||||||
filepaths = [os.path.join(OBJECT_STORE_DIR, file) for file in os.listdir(OBJECT_STORE_DIR) if not file.startswith('.')]
|
filepaths = [os.path.join(OBJECT_STORE_DIR, file) for file in os.listdir(OBJECT_STORE_DIR) if not file.startswith('.')]
|
||||||
ctime_sorted_basenames = [item[0] for item in sorted([(os.path.basename(path), os.path.getctime(path)) for path in filepaths],
|
ctime_sorted_basenames = [item[0] for item in sorted([(os.path.basename(path), os.path.getctime(path)) for path in filepaths],
|
||||||
key=operator.itemgetter(1), reverse=True)]
|
key=operator.itemgetter(1), reverse=True)]
|
||||||
return [reverse('stored-object', kwargs={'key':key}) for key in ctime_sorted_basenames]
|
return Response([reverse('stored-object', kwargs={'key':key}) for key in ctime_sorted_basenames])
|
||||||
|
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
"""
|
"""
|
||||||
|
@ -51,7 +51,8 @@ class ObjectStoreRoot(View):
|
||||||
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
||||||
pickle.dump(self.CONTENT, open(pathname, 'wb'))
|
pickle.dump(self.CONTENT, open(pathname, 'wb'))
|
||||||
remove_oldest_files(OBJECT_STORE_DIR, MAX_FILES)
|
remove_oldest_files(OBJECT_STORE_DIR, MAX_FILES)
|
||||||
return Response(status.HTTP_201_CREATED, self.CONTENT, {'Location': reverse('stored-object', kwargs={'key':key})})
|
self.headers['Location'] = reverse('stored-object', kwargs={'key':key})
|
||||||
|
return Response(self.CONTENT, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
|
||||||
class StoredObject(View):
|
class StoredObject(View):
|
||||||
|
@ -67,7 +68,7 @@ class StoredObject(View):
|
||||||
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
||||||
if not os.path.exists(pathname):
|
if not os.path.exists(pathname):
|
||||||
return Response(status.HTTP_404_NOT_FOUND)
|
return Response(status.HTTP_404_NOT_FOUND)
|
||||||
return pickle.load(open(pathname, 'rb'))
|
return Response(pickle.load(open(pathname, 'rb')))
|
||||||
|
|
||||||
def put(self, request, key):
|
def put(self, request, key):
|
||||||
"""
|
"""
|
||||||
|
@ -75,7 +76,7 @@ class StoredObject(View):
|
||||||
"""
|
"""
|
||||||
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
||||||
pickle.dump(self.CONTENT, open(pathname, 'wb'))
|
pickle.dump(self.CONTENT, open(pathname, 'wb'))
|
||||||
return self.CONTENT
|
return Response(self.CONTENT)
|
||||||
|
|
||||||
def delete(self, request, key):
|
def delete(self, request, key):
|
||||||
"""
|
"""
|
||||||
|
@ -85,3 +86,4 @@ class StoredObject(View):
|
||||||
if not os.path.exists(pathname):
|
if not os.path.exists(pathname):
|
||||||
return Response(status.HTTP_404_NOT_FOUND)
|
return Response(status.HTTP_404_NOT_FOUND)
|
||||||
os.remove(pathname)
|
os.remove(pathname)
|
||||||
|
return Response()
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from djangorestframework.views import View
|
from djangorestframework.views import View
|
||||||
|
from djangorestframework.response import Response
|
||||||
from djangorestframework.permissions import PerUserThrottling, IsAuthenticated
|
from djangorestframework.permissions import PerUserThrottling, IsAuthenticated
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
|
|
||||||
|
@ -9,7 +10,7 @@ class PermissionsExampleView(View):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
return [
|
return Response([
|
||||||
{
|
{
|
||||||
'name': 'Throttling Example',
|
'name': 'Throttling Example',
|
||||||
'url': reverse('throttled-resource')
|
'url': reverse('throttled-resource')
|
||||||
|
@ -18,7 +19,7 @@ class PermissionsExampleView(View):
|
||||||
'name': 'Logged in example',
|
'name': 'Logged in example',
|
||||||
'url': reverse('loggedin-resource')
|
'url': reverse('loggedin-resource')
|
||||||
},
|
},
|
||||||
]
|
])
|
||||||
|
|
||||||
|
|
||||||
class ThrottlingExampleView(View):
|
class ThrottlingExampleView(View):
|
||||||
|
@ -36,7 +37,7 @@ class ThrottlingExampleView(View):
|
||||||
"""
|
"""
|
||||||
Handle GET requests.
|
Handle GET requests.
|
||||||
"""
|
"""
|
||||||
return "Successful response to GET request because throttle is not yet active."
|
return Response("Successful response to GET request because throttle is not yet active.")
|
||||||
|
|
||||||
|
|
||||||
class LoggedInExampleView(View):
|
class LoggedInExampleView(View):
|
||||||
|
@ -49,4 +50,4 @@ class LoggedInExampleView(View):
|
||||||
permissions = (IsAuthenticated, )
|
permissions = (IsAuthenticated, )
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
return 'You have permission to view this resource'
|
return Response('You have permission to view this resource')
|
||||||
|
|
|
@ -61,7 +61,7 @@ class PygmentsRoot(View):
|
||||||
Return a list of all currently existing snippets.
|
Return a list of all currently existing snippets.
|
||||||
"""
|
"""
|
||||||
unique_ids = [os.path.split(f)[1] for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)]
|
unique_ids = [os.path.split(f)[1] for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)]
|
||||||
return [reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids]
|
return Response([reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids])
|
||||||
|
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
"""
|
"""
|
||||||
|
@ -98,7 +98,7 @@ class PygmentsInstance(View):
|
||||||
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
|
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
|
||||||
if not os.path.exists(pathname):
|
if not os.path.exists(pathname):
|
||||||
return Response(status.HTTP_404_NOT_FOUND)
|
return Response(status.HTTP_404_NOT_FOUND)
|
||||||
return open(pathname, 'r').read()
|
return Response(open(pathname, 'r').read())
|
||||||
|
|
||||||
def delete(self, request, unique_id):
|
def delete(self, request, unique_id):
|
||||||
"""
|
"""
|
||||||
|
@ -107,5 +107,5 @@ class PygmentsInstance(View):
|
||||||
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
|
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
|
||||||
if not os.path.exists(pathname):
|
if not os.path.exists(pathname):
|
||||||
return Response(status.HTTP_404_NOT_FOUND)
|
return Response(status.HTTP_404_NOT_FOUND)
|
||||||
return os.remove(pathname)
|
return Response(os.remove(pathname))
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from django.conf.urls.defaults import patterns, url
|
from django.conf.urls.defaults import patterns, url
|
||||||
from requestexample.views import RequestExampleView, MockView, EchoRequestContentView
|
from requestexample.views import RequestExampleView, EchoRequestContentView
|
||||||
|
from examples.views import MockView
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^$', RequestExampleView.as_view(), name='request-example'),
|
url(r'^$', RequestExampleView.as_view(), name='request-example'),
|
||||||
|
|
|
@ -5,6 +5,7 @@ from django.core.urlresolvers import reverse
|
||||||
from djangorestframework.mixins import RequestMixin
|
from djangorestframework.mixins import RequestMixin
|
||||||
from djangorestframework.views import View as DRFView
|
from djangorestframework.views import View as DRFView
|
||||||
from djangorestframework import parsers
|
from djangorestframework import parsers
|
||||||
|
from djangorestframework.response import Response
|
||||||
|
|
||||||
|
|
||||||
class RequestExampleView(DRFView):
|
class RequestExampleView(DRFView):
|
||||||
|
@ -13,7 +14,7 @@ class RequestExampleView(DRFView):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
return [{'name': 'request.DATA Example', 'url': reverse('request-content')},]
|
return Response([{'name': 'request.DATA Example', 'url': reverse('request-content')},])
|
||||||
|
|
||||||
|
|
||||||
class MyBaseViewUsingEnhancedRequest(RequestMixin, View):
|
class MyBaseViewUsingEnhancedRequest(RequestMixin, View):
|
||||||
|
@ -41,35 +42,3 @@ class EchoRequestContentView(MyBaseViewUsingEnhancedRequest):
|
||||||
return HttpResponse(("Found %s in request.DATA, content : %s" %
|
return HttpResponse(("Found %s in request.DATA, content : %s" %
|
||||||
(type(request.DATA), request.DATA)))
|
(type(request.DATA), request.DATA)))
|
||||||
|
|
||||||
|
|
||||||
class MockView(DRFView):
|
|
||||||
"""
|
|
||||||
A view that just acts as a proxy to call non-djangorestframework views, while still
|
|
||||||
displaying the browsable API interface.
|
|
||||||
"""
|
|
||||||
|
|
||||||
view_class = None
|
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
|
||||||
self.request = request
|
|
||||||
if self.get_request().method in ['PUT', 'POST']:
|
|
||||||
self.response = self.view_class.as_view()(request, *args, **kwargs)
|
|
||||||
return super(MockView, self).dispatch(request, *args, **kwargs)
|
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
|
||||||
return
|
|
||||||
|
|
||||||
def put(self, request, *args, **kwargs):
|
|
||||||
return self.response.content
|
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
|
||||||
return self.response.content
|
|
||||||
|
|
||||||
def __getattribute__(self, name):
|
|
||||||
if name == '__name__':
|
|
||||||
return self.view_class.__name__
|
|
||||||
elif name == '__doc__':
|
|
||||||
return self.view_class.__doc__
|
|
||||||
else:
|
|
||||||
return super(MockView, self).__getattribute__(name)
|
|
||||||
|
|
||||||
|
|
|
@ -16,12 +16,12 @@ class ExampleView(View):
|
||||||
"""
|
"""
|
||||||
Handle GET requests, returning a list of URLs pointing to 3 other views.
|
Handle GET requests, returning a list of URLs pointing to 3 other views.
|
||||||
"""
|
"""
|
||||||
return {"Some other resources": [reverse('another-example', kwargs={'num':num}) for num in range(3)]}
|
return Response({"Some other resources": [reverse('another-example', kwargs={'num':num}) for num in range(3)]})
|
||||||
|
|
||||||
|
|
||||||
class AnotherExampleView(View):
|
class AnotherExampleView(View):
|
||||||
"""
|
"""
|
||||||
A basic view, that can be handle GET and POST requests.
|
A basic view, that can handle GET and POST requests.
|
||||||
Applies some simple form validation on POST requests.
|
Applies some simple form validation on POST requests.
|
||||||
"""
|
"""
|
||||||
form = MyForm
|
form = MyForm
|
||||||
|
@ -33,7 +33,7 @@ class AnotherExampleView(View):
|
||||||
"""
|
"""
|
||||||
if int(num) > 2:
|
if int(num) > 2:
|
||||||
return Response(status.HTTP_404_NOT_FOUND)
|
return Response(status.HTTP_404_NOT_FOUND)
|
||||||
return "GET request to AnotherExampleResource %s" % num
|
return Response("GET request to AnotherExampleResource %s" % num)
|
||||||
|
|
||||||
def post(self, request, num):
|
def post(self, request, num):
|
||||||
"""
|
"""
|
||||||
|
@ -42,4 +42,4 @@ class AnotherExampleView(View):
|
||||||
"""
|
"""
|
||||||
if int(num) > 2:
|
if int(num) > 2:
|
||||||
return Response(status.HTTP_404_NOT_FOUND)
|
return Response(status.HTTP_404_NOT_FOUND)
|
||||||
return "POST request to AnotherExampleResource %s, with content: %s" % (num, repr(self.CONTENT))
|
return Response("POST request to AnotherExampleResource %s, with content: %s" % (num, repr(self.CONTENT)))
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from djangorestframework.views import View
|
from djangorestframework.views import View
|
||||||
|
from djangorestframework.response import Response
|
||||||
|
|
||||||
|
|
||||||
class Sandbox(View):
|
class Sandbox(View):
|
||||||
|
@ -28,7 +29,7 @@ class Sandbox(View):
|
||||||
Please feel free to browse, create, edit and delete the resources in these examples."""
|
Please feel free to browse, create, edit and delete the resources in these examples."""
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
return [{'name': 'Simple Resource example', 'url': reverse('example-resource')},
|
return Response([{'name': 'Simple Resource example', 'url': reverse('example-resource')},
|
||||||
{'name': 'Simple ModelResource example', 'url': reverse('model-resource-root')},
|
{'name': 'Simple ModelResource example', 'url': reverse('model-resource-root')},
|
||||||
{'name': 'Simple Mixin-only example', 'url': reverse('mixin-view')},
|
{'name': 'Simple Mixin-only example', 'url': reverse('mixin-view')},
|
||||||
{'name': 'Object store API', 'url': reverse('object-store-root')},
|
{'name': 'Object store API', 'url': reverse('object-store-root')},
|
||||||
|
@ -36,4 +37,4 @@ class Sandbox(View):
|
||||||
{'name': 'Blog posts API', 'url': reverse('blog-posts-root')},
|
{'name': 'Blog posts API', 'url': reverse('blog-posts-root')},
|
||||||
{'name': 'Permissions example', 'url': reverse('permissions-example')},
|
{'name': 'Permissions example', 'url': reverse('permissions-example')},
|
||||||
{'name': 'Simple request mixin example', 'url': reverse('request-example')}
|
{'name': 'Simple request mixin example', 'url': reverse('request-example')}
|
||||||
]
|
])
|
||||||
|
|
34
examples/views.py
Normal file
34
examples/views.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
from djangorestframework.views import View
|
||||||
|
from djangorestframework.response import Response
|
||||||
|
|
||||||
|
|
||||||
|
class MockView(View):
|
||||||
|
"""
|
||||||
|
A view that just acts as a proxy to call non-djangorestframework views, while still
|
||||||
|
displaying the browsable API interface.
|
||||||
|
"""
|
||||||
|
|
||||||
|
view_class = None
|
||||||
|
|
||||||
|
def dispatch(self, request, *args, **kwargs):
|
||||||
|
request = self.prepare_request(request)
|
||||||
|
if request.method in ['PUT', 'POST']:
|
||||||
|
self.response = self.view_class.as_view()(request, *args, **kwargs)
|
||||||
|
return super(MockView, self).dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
return Response()
|
||||||
|
|
||||||
|
def put(self, request, *args, **kwargs):
|
||||||
|
return Response(self.response.content)
|
||||||
|
|
||||||
|
def post(self, request, *args, **kwargs):
|
||||||
|
return Response(self.response.content)
|
||||||
|
|
||||||
|
def __getattribute__(self, name):
|
||||||
|
if name == '__name__':
|
||||||
|
return self.view_class.__name__
|
||||||
|
elif name == '__doc__':
|
||||||
|
return self.view_class.__doc__
|
||||||
|
else:
|
||||||
|
return super(MockView, self).__getattribute__(name)
|
Loading…
Reference in New Issue
Block a user