attempt at fixing the examples

This commit is contained in:
Sébastien Piquemal 2012-02-10 11:05:20 +02:00
parent db0b01037a
commit b33579a7a1
11 changed files with 21 additions and 18 deletions

View File

@ -372,7 +372,7 @@ class ReadModelMixin(ModelMixin):
except model.DoesNotExist:
raise ImmediateResponse(status=status.HTTP_404_NOT_FOUND)
return self.model_instance
return Response(self.model_instance)
class CreateModelMixin(ModelMixin):
@ -428,7 +428,7 @@ class UpdateModelMixin(ModelMixin):
# 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
try:
self.model_instance = self.get_instance(*query_kwargs)
self.model_instance = self.get_instance(**query_kwargs)
for (key, val) in self.CONTENT.items():
setattr(self.model_instance, key, val)

View File

@ -355,7 +355,7 @@ class DocumentingTemplateRenderer(BaseRenderer):
'login_url': login_url,
'logout_url': logout_url,
'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),
})

View File

@ -41,7 +41,7 @@
<h1>{{ name }}</h1>
<p>{{ description }}</p>
<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 }}
{% endfor %}
{{ content|urlize_quoted_links }}</pre>{% endautoescape %}</div>
@ -63,7 +63,7 @@
{% endif %}
{# 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 %}
<form action="{{ request.get_full_path }}" method="post" {% if post_form.is_multipart %}enctype="multipart/form-data"{% endif %}>

View File

@ -31,7 +31,7 @@ class TestModelRead(TestModelsTestCase):
mixin.resource = GroupResource
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):
class GroupResource(ModelResource):

View File

@ -139,7 +139,8 @@ class MockView(ResponseMixin, DjangoView):
def get(self, request, **kwargs):
response = Response(DUMMYCONTENT, status=DUMMYSTATUS)
return self.prepare_response(response)
self.response = self.prepare_response(response)
return self.response
class HTMLView(View):

View File

@ -15,7 +15,8 @@ class ExampleView(ResponseMixin, View):
def get(self, request):
response = Response({'description': 'Some example content',
'url': reverse('mixin-view')}, status=200)
return self.prepare_response(response)
self.response = self.prepare_response(response)
return self.response
urlpatterns = patterns('',

View File

@ -67,7 +67,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 Response(status=status.HTTP_404_NOT_FOUND)
return Response(pickle.load(open(pathname, 'rb')))
def put(self, request, key):
@ -84,6 +84,6 @@ 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 Response(status=status.HTTP_404_NOT_FOUND)
os.remove(pathname)
return Response()

View File

@ -81,7 +81,8 @@ class PygmentsRoot(View):
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):

View File

@ -1,9 +1,9 @@
from django.conf.urls.defaults import patterns, url
from requestexample.views import RequestExampleView, EchoRequestContentView
from examples.views import MockView
from examples.views import ProxyView
urlpatterns = patterns('',
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'),
)

View File

@ -25,7 +25,7 @@ class MyBaseViewUsingEnhancedRequest(RequestMixin, View):
parser_classes = parsers.DEFAULT_PARSERS
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)

View File

@ -2,7 +2,7 @@ from djangorestframework.views import View
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
displaying the browsable API interface.
@ -11,10 +11,10 @@ class MockView(View):
view_class = None
def dispatch(self, request, *args, **kwargs):
request = self.prepare_request(request)
self.request = request = self.create_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)
return super(ProxyView, self).dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
return Response()
@ -31,4 +31,4 @@ class MockView(View):
elif name == '__doc__':
return self.view_class.__doc__
else:
return super(MockView, self).__getattribute__(name)
return super(ProxyView, self).__getattribute__(name)