mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +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.
|
||||
"""
|
||||
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
|
||||
|
||||
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.response import Response, ImmediateResponse
|
||||
from djangorestframework.mixins import *
|
||||
from djangorestframework.utils import allowed_methods
|
||||
from djangorestframework import resources, renderers, parsers, authentication, permissions, status
|
||||
|
||||
|
||||
|
|
|
@ -10,12 +10,12 @@ from django.core.urlresolvers import reverse
|
|||
class ExampleView(ResponseMixin, View):
|
||||
"""An example view using Django 1.3's class based views.
|
||||
Uses djangorestframework's RendererMixin to provide support for multiple output formats."""
|
||||
renderers = DEFAULT_RENDERERS
|
||||
renderer_classes = DEFAULT_RENDERERS
|
||||
|
||||
def get(self, request):
|
||||
response = Response(200, {'description': 'Some example content',
|
||||
'url': reverse('mixin-view')})
|
||||
return self.render(response)
|
||||
response = Response({'description': 'Some example content',
|
||||
'url': reverse('mixin-view')}, status=200)
|
||||
return self.prepare_response(response)
|
||||
|
||||
|
||||
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('.')]
|
||||
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)]
|
||||
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):
|
||||
"""
|
||||
|
@ -51,7 +51,8 @@ class ObjectStoreRoot(View):
|
|||
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
||||
pickle.dump(self.CONTENT, open(pathname, 'wb'))
|
||||
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):
|
||||
|
@ -67,7 +68,7 @@ class StoredObject(View):
|
|||
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
||||
if not os.path.exists(pathname):
|
||||
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):
|
||||
"""
|
||||
|
@ -75,7 +76,7 @@ class StoredObject(View):
|
|||
"""
|
||||
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
||||
pickle.dump(self.CONTENT, open(pathname, 'wb'))
|
||||
return self.CONTENT
|
||||
return Response(self.CONTENT)
|
||||
|
||||
def delete(self, request, key):
|
||||
"""
|
||||
|
@ -85,3 +86,4 @@ class StoredObject(View):
|
|||
if not os.path.exists(pathname):
|
||||
return Response(status.HTTP_404_NOT_FOUND)
|
||||
os.remove(pathname)
|
||||
return Response()
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from djangorestframework.views import View
|
||||
from djangorestframework.response import Response
|
||||
from djangorestframework.permissions import PerUserThrottling, IsAuthenticated
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
|
@ -9,7 +10,7 @@ class PermissionsExampleView(View):
|
|||
"""
|
||||
|
||||
def get(self, request):
|
||||
return [
|
||||
return Response([
|
||||
{
|
||||
'name': 'Throttling Example',
|
||||
'url': reverse('throttled-resource')
|
||||
|
@ -18,7 +19,7 @@ class PermissionsExampleView(View):
|
|||
'name': 'Logged in example',
|
||||
'url': reverse('loggedin-resource')
|
||||
},
|
||||
]
|
||||
])
|
||||
|
||||
|
||||
class ThrottlingExampleView(View):
|
||||
|
@ -36,7 +37,7 @@ class ThrottlingExampleView(View):
|
|||
"""
|
||||
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):
|
||||
|
@ -49,4 +50,4 @@ class LoggedInExampleView(View):
|
|||
permissions = (IsAuthenticated, )
|
||||
|
||||
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.
|
||||
"""
|
||||
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):
|
||||
"""
|
||||
|
@ -98,7 +98,7 @@ class PygmentsInstance(View):
|
|||
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
|
||||
if not os.path.exists(pathname):
|
||||
return Response(status.HTTP_404_NOT_FOUND)
|
||||
return open(pathname, 'r').read()
|
||||
return Response(open(pathname, 'r').read())
|
||||
|
||||
def delete(self, request, unique_id):
|
||||
"""
|
||||
|
@ -107,5 +107,5 @@ class PygmentsInstance(View):
|
|||
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
|
||||
if not os.path.exists(pathname):
|
||||
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 requestexample.views import RequestExampleView, MockView, EchoRequestContentView
|
||||
from requestexample.views import RequestExampleView, EchoRequestContentView
|
||||
from examples.views import MockView
|
||||
|
||||
|
||||
urlpatterns = patterns('',
|
||||
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.views import View as DRFView
|
||||
from djangorestframework import parsers
|
||||
from djangorestframework.response import Response
|
||||
|
||||
|
||||
class RequestExampleView(DRFView):
|
||||
|
@ -13,7 +14,7 @@ class RequestExampleView(DRFView):
|
|||
"""
|
||||
|
||||
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):
|
||||
|
@ -41,35 +42,3 @@ class EchoRequestContentView(MyBaseViewUsingEnhancedRequest):
|
|||
return HttpResponse(("Found %s in request.DATA, content : %s" %
|
||||
(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.
|
||||
"""
|
||||
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):
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
form = MyForm
|
||||
|
@ -33,7 +33,7 @@ class AnotherExampleView(View):
|
|||
"""
|
||||
if int(num) > 2:
|
||||
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):
|
||||
"""
|
||||
|
@ -42,4 +42,4 @@ class AnotherExampleView(View):
|
|||
"""
|
||||
if int(num) > 2:
|
||||
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 djangorestframework.views import View
|
||||
from djangorestframework.response import Response
|
||||
|
||||
|
||||
class Sandbox(View):
|
||||
|
@ -28,7 +29,7 @@ class Sandbox(View):
|
|||
Please feel free to browse, create, edit and delete the resources in these examples."""
|
||||
|
||||
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 Mixin-only example', 'url': reverse('mixin-view')},
|
||||
{'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': 'Permissions example', 'url': reverse('permissions-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