mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-07 13:54:45 +03:00
attempt at fixing the examples
This commit is contained in:
parent
db0b01037a
commit
b33579a7a1
|
@ -372,7 +372,7 @@ class ReadModelMixin(ModelMixin):
|
||||||
except model.DoesNotExist:
|
except model.DoesNotExist:
|
||||||
raise ImmediateResponse(status=status.HTTP_404_NOT_FOUND)
|
raise ImmediateResponse(status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
return self.model_instance
|
return Response(self.model_instance)
|
||||||
|
|
||||||
|
|
||||||
class CreateModelMixin(ModelMixin):
|
class CreateModelMixin(ModelMixin):
|
||||||
|
@ -428,7 +428,7 @@ class UpdateModelMixin(ModelMixin):
|
||||||
# TODO: update on the url of a non-existing resource url doesn't work
|
# TODO: update on the url of a non-existing resource url doesn't work
|
||||||
# correctly at the moment - will end up with a new url
|
# correctly at the moment - will end up with a new url
|
||||||
try:
|
try:
|
||||||
self.model_instance = self.get_instance(*query_kwargs)
|
self.model_instance = self.get_instance(**query_kwargs)
|
||||||
|
|
||||||
for (key, val) in self.CONTENT.items():
|
for (key, val) in self.CONTENT.items():
|
||||||
setattr(self.model_instance, key, val)
|
setattr(self.model_instance, key, val)
|
||||||
|
|
|
@ -355,7 +355,7 @@ class DocumentingTemplateRenderer(BaseRenderer):
|
||||||
'login_url': login_url,
|
'login_url': login_url,
|
||||||
'logout_url': logout_url,
|
'logout_url': logout_url,
|
||||||
'FORMAT_PARAM': self._FORMAT_QUERY_PARAM,
|
'FORMAT_PARAM': self._FORMAT_QUERY_PARAM,
|
||||||
'METHOD_PARAM': getattr(self.view, '_METHOD_PARAM', None),
|
'METHOD_PARAM': getattr(self.view.request, '_METHOD_PARAM', None),
|
||||||
'ADMIN_MEDIA_PREFIX': getattr(settings, 'ADMIN_MEDIA_PREFIX', None),
|
'ADMIN_MEDIA_PREFIX': getattr(settings, 'ADMIN_MEDIA_PREFIX', None),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@
|
||||||
<h1>{{ name }}</h1>
|
<h1>{{ name }}</h1>
|
||||||
<p>{{ description }}</p>
|
<p>{{ description }}</p>
|
||||||
<div class='module'>
|
<div class='module'>
|
||||||
<pre><b>{{ response.status }} {{ response.status_text }}</b>{% autoescape off %}
|
<pre><b>{{ response.status_code }} {{ response.status_text }}</b>{% autoescape off %}
|
||||||
{% for key, val in response.headers.items %}<b>{{ key }}:</b> {{ val|urlize_quoted_links }}
|
{% for key, val in response.headers.items %}<b>{{ key }}:</b> {{ val|urlize_quoted_links }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{{ content|urlize_quoted_links }}</pre>{% endautoescape %}</div>
|
{{ content|urlize_quoted_links }}</pre>{% endautoescape %}</div>
|
||||||
|
@ -63,7 +63,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{# Only display the POST/PUT/DELETE forms if method tunneling via POST forms is enabled and the user has permissions on this view. #}
|
{# Only display the POST/PUT/DELETE forms if method tunneling via POST forms is enabled and the user has permissions on this view. #}
|
||||||
{% if METHOD_PARAM and response.status != 403 %}
|
{% if METHOD_PARAM and response.status_code != 403 %}
|
||||||
|
|
||||||
{% if 'POST' in view.allowed_methods %}
|
{% if 'POST' in view.allowed_methods %}
|
||||||
<form action="{{ request.get_full_path }}" method="post" {% if post_form.is_multipart %}enctype="multipart/form-data"{% endif %}>
|
<form action="{{ request.get_full_path }}" method="post" {% if post_form.is_multipart %}enctype="multipart/form-data"{% endif %}>
|
||||||
|
|
|
@ -31,7 +31,7 @@ class TestModelRead(TestModelsTestCase):
|
||||||
mixin.resource = GroupResource
|
mixin.resource = GroupResource
|
||||||
|
|
||||||
response = mixin.get(request, id=group.id)
|
response = mixin.get(request, id=group.id)
|
||||||
self.assertEquals(group.name, response.name)
|
self.assertEquals(group.name, response.raw_content.name)
|
||||||
|
|
||||||
def test_read_404(self):
|
def test_read_404(self):
|
||||||
class GroupResource(ModelResource):
|
class GroupResource(ModelResource):
|
||||||
|
|
|
@ -139,7 +139,8 @@ class MockView(ResponseMixin, DjangoView):
|
||||||
|
|
||||||
def get(self, request, **kwargs):
|
def get(self, request, **kwargs):
|
||||||
response = Response(DUMMYCONTENT, status=DUMMYSTATUS)
|
response = Response(DUMMYCONTENT, status=DUMMYSTATUS)
|
||||||
return self.prepare_response(response)
|
self.response = self.prepare_response(response)
|
||||||
|
return self.response
|
||||||
|
|
||||||
|
|
||||||
class HTMLView(View):
|
class HTMLView(View):
|
||||||
|
|
|
@ -15,7 +15,8 @@ class ExampleView(ResponseMixin, View):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
response = Response({'description': 'Some example content',
|
response = Response({'description': 'Some example content',
|
||||||
'url': reverse('mixin-view')}, status=200)
|
'url': reverse('mixin-view')}, status=200)
|
||||||
return self.prepare_response(response)
|
self.response = self.prepare_response(response)
|
||||||
|
return self.response
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
|
|
|
@ -67,7 +67,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=status.HTTP_404_NOT_FOUND)
|
||||||
return Response(pickle.load(open(pathname, 'rb')))
|
return Response(pickle.load(open(pathname, 'rb')))
|
||||||
|
|
||||||
def put(self, request, key):
|
def put(self, request, key):
|
||||||
|
@ -84,6 +84,6 @@ 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=status.HTTP_404_NOT_FOUND)
|
||||||
os.remove(pathname)
|
os.remove(pathname)
|
||||||
return Response()
|
return Response()
|
||||||
|
|
|
@ -81,7 +81,8 @@ class PygmentsRoot(View):
|
||||||
|
|
||||||
remove_oldest_files(HIGHLIGHTED_CODE_DIR, MAX_FILES)
|
remove_oldest_files(HIGHLIGHTED_CODE_DIR, MAX_FILES)
|
||||||
|
|
||||||
return Response(status.HTTP_201_CREATED, headers={'Location': reverse('pygments-instance', args=[unique_id])})
|
self.headers['Location'] = reverse('pygments-instance', args=[unique_id])
|
||||||
|
return Response(status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
|
||||||
class PygmentsInstance(View):
|
class PygmentsInstance(View):
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
from django.conf.urls.defaults import patterns, url
|
from django.conf.urls.defaults import patterns, url
|
||||||
from requestexample.views import RequestExampleView, EchoRequestContentView
|
from requestexample.views import RequestExampleView, EchoRequestContentView
|
||||||
from examples.views import MockView
|
from examples.views import ProxyView
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^$', RequestExampleView.as_view(), name='request-example'),
|
url(r'^$', RequestExampleView.as_view(), name='request-example'),
|
||||||
url(r'^content$', MockView.as_view(view_class=EchoRequestContentView), name='request-content'),
|
url(r'^content$', ProxyView.as_view(view_class=EchoRequestContentView), name='request-content'),
|
||||||
)
|
)
|
||||||
|
|
|
@ -25,7 +25,7 @@ class MyBaseViewUsingEnhancedRequest(RequestMixin, View):
|
||||||
parser_classes = parsers.DEFAULT_PARSERS
|
parser_classes = parsers.DEFAULT_PARSERS
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
request = self.prepare_request(request)
|
self.request = request = self.create_request(request)
|
||||||
return super(MyBaseViewUsingEnhancedRequest, self).dispatch(request, *args, **kwargs)
|
return super(MyBaseViewUsingEnhancedRequest, self).dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ from djangorestframework.views import View
|
||||||
from djangorestframework.response import Response
|
from djangorestframework.response import Response
|
||||||
|
|
||||||
|
|
||||||
class MockView(View):
|
class ProxyView(View):
|
||||||
"""
|
"""
|
||||||
A view that just acts as a proxy to call non-djangorestframework views, while still
|
A view that just acts as a proxy to call non-djangorestframework views, while still
|
||||||
displaying the browsable API interface.
|
displaying the browsable API interface.
|
||||||
|
@ -11,10 +11,10 @@ class MockView(View):
|
||||||
view_class = None
|
view_class = None
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
request = self.prepare_request(request)
|
self.request = request = self.create_request(request)
|
||||||
if request.method in ['PUT', 'POST']:
|
if request.method in ['PUT', 'POST']:
|
||||||
self.response = self.view_class.as_view()(request, *args, **kwargs)
|
self.response = self.view_class.as_view()(request, *args, **kwargs)
|
||||||
return super(MockView, self).dispatch(request, *args, **kwargs)
|
return super(ProxyView, self).dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
return Response()
|
return Response()
|
||||||
|
@ -31,4 +31,4 @@ class MockView(View):
|
||||||
elif name == '__doc__':
|
elif name == '__doc__':
|
||||||
return self.view_class.__doc__
|
return self.view_class.__doc__
|
||||||
else:
|
else:
|
||||||
return super(MockView, self).__getattribute__(name)
|
return super(ProxyView, self).__getattribute__(name)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user