aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/renderers.py
diff options
context:
space:
mode:
authorTom Christie2012-02-17 09:19:13 +0000
committerTom Christie2012-02-17 09:19:13 +0000
commitfbf76c87affc88f04bb0d0acaecc6af6442ba921 (patch)
tree5a75cbb061829694c4f714ae0e8413c584131739 /djangorestframework/renderers.py
parent426493a78f3003fdba39053b6af23b93b312a777 (diff)
parentc04cb5145c4398cfac090ca7eef032296a04446f (diff)
downloaddjango-rest-framework-fbf76c87affc88f04bb0d0acaecc6af6442ba921.tar.bz2
Merge git://github.com/sebpiq/django-rest-framework into develop
Diffstat (limited to 'djangorestframework/renderers.py')
-rw-r--r--djangorestframework/renderers.py38
1 files changed, 22 insertions, 16 deletions
diff --git a/djangorestframework/renderers.py b/djangorestframework/renderers.py
index bb0f789a..2cc9cc88 100644
--- a/djangorestframework/renderers.py
+++ b/djangorestframework/renderers.py
@@ -13,7 +13,7 @@ from django.utils import simplejson as json
from djangorestframework.compat import yaml
-from djangorestframework.utils import dict2xml, url_resolves
+from djangorestframework.utils import dict2xml, url_resolves, allowed_methods
from djangorestframework.utils.breadcrumbs import get_breadcrumbs
from djangorestframework.utils.mediatypes import get_media_type_params, add_media_type_param, media_type_matches
from djangorestframework import VERSION
@@ -45,7 +45,7 @@ class BaseRenderer(object):
media_type = None
format = None
- def __init__(self, view):
+ def __init__(self, view=None):
self.view = view
def can_handle_response(self, accept):
@@ -60,9 +60,13 @@ class BaseRenderer(object):
This may be overridden to provide for other behavior, but typically you'll
instead want to just set the :attr:`media_type` attribute on the class.
"""
- format = self.view.kwargs.get(self._FORMAT_QUERY_PARAM, None)
- if format is None:
+ # TODO: format overriding must go out of here
+ format = None
+ if self.view is not None:
+ format = self.view.kwargs.get(self._FORMAT_QUERY_PARAM, None)
+ if format is None and self.view is not None:
format = self.view.request.GET.get(self._FORMAT_QUERY_PARAM, None)
+
if format is not None:
return format == self.format
return media_type_matches(self.media_type, accept)
@@ -214,7 +218,8 @@ class DocumentingTemplateRenderer(BaseRenderer):
"""
# Find the first valid renderer and render the content. (Don't use another documenting renderer.)
- renderers = [renderer for renderer in view.renderers if not issubclass(renderer, DocumentingTemplateRenderer)]
+ renderers = [renderer for renderer in view.renderer_classes
+ if not issubclass(renderer, DocumentingTemplateRenderer)]
if not renderers:
return '[No renderers were found]'
@@ -268,32 +273,32 @@ class DocumentingTemplateRenderer(BaseRenderer):
# If we're not using content overloading there's no point in supplying a generic form,
# as the view won't treat the form's value as the content of the request.
- if not getattr(view, '_USE_FORM_OVERLOADING', False):
+ if not getattr(view.request, '_USE_FORM_OVERLOADING', False):
return None
# NB. http://jacobian.org/writing/dynamic-form-generation/
class GenericContentForm(forms.Form):
- def __init__(self, view):
+ def __init__(self, request):
"""We don't know the names of the fields we want to set until the point the form is instantiated,
as they are determined by the Resource the form is being created against.
Add the fields dynamically."""
super(GenericContentForm, self).__init__()
- contenttype_choices = [(media_type, media_type) for media_type in view._parsed_media_types]
- initial_contenttype = view._default_parser.media_type
+ contenttype_choices = [(media_type, media_type) for media_type in request._parsed_media_types]
+ initial_contenttype = request._default_parser.media_type
- self.fields[view._CONTENTTYPE_PARAM] = forms.ChoiceField(label='Content Type',
+ self.fields[request._CONTENTTYPE_PARAM] = forms.ChoiceField(label='Content Type',
choices=contenttype_choices,
initial=initial_contenttype)
- self.fields[view._CONTENT_PARAM] = forms.CharField(label='Content',
+ self.fields[request._CONTENT_PARAM] = forms.CharField(label='Content',
widget=forms.Textarea)
# If either of these reserved parameters are turned off then content tunneling is not possible
- if self.view._CONTENTTYPE_PARAM is None or self.view._CONTENT_PARAM is None:
+ if self.view.request._CONTENTTYPE_PARAM is None or self.view.request._CONTENT_PARAM is None:
return None
# Okey doke, let's do it
- return GenericContentForm(view)
+ return GenericContentForm(view.request)
def get_name(self):
try:
@@ -344,13 +349,14 @@ class DocumentingTemplateRenderer(BaseRenderer):
'name': name,
'version': VERSION,
'breadcrumblist': breadcrumb_list,
+ 'allowed_methods': allowed_methods(self.view),
'available_formats': self.view._rendered_formats,
'put_form': put_form_instance,
'post_form': post_form_instance,
'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),
})
@@ -359,8 +365,8 @@ class DocumentingTemplateRenderer(BaseRenderer):
# Munge DELETE Response code to allow us to return content
# (Do this *after* we've rendered the template so that we include
# the normal deletion response code in the output)
- if self.view.response.status == 204:
- self.view.response.status = 200
+ if self.view.response.status_code == 204:
+ self.view.response.status_code = 200
return ret