mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
First hacky pass at displaying raw data
This commit is contained in:
parent
c3e273a90e
commit
8d590ebfde
|
@ -10,9 +10,9 @@ from django.core.files.uploadhandler import StopFutureHandlers
|
||||||
from django.http import QueryDict
|
from django.http import QueryDict
|
||||||
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
|
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
|
||||||
from django.http.multipartparser import MultiPartParserError, parse_header, ChunkIter
|
from django.http.multipartparser import MultiPartParserError, parse_header, ChunkIter
|
||||||
from rest_framework.compat import yaml, etree
|
from rest_framework.compat import etree, six, yaml
|
||||||
from rest_framework.exceptions import ParseError
|
from rest_framework.exceptions import ParseError
|
||||||
from rest_framework.compat import six
|
from rest_framework.renderers import UnicodeJSONRenderer
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
|
@ -32,6 +32,8 @@ class BaseParser(object):
|
||||||
|
|
||||||
media_type = None
|
media_type = None
|
||||||
|
|
||||||
|
supports_html_forms = False
|
||||||
|
|
||||||
def parse(self, stream, media_type=None, parser_context=None):
|
def parse(self, stream, media_type=None, parser_context=None):
|
||||||
"""
|
"""
|
||||||
Given a stream to read from, return the parsed representation.
|
Given a stream to read from, return the parsed representation.
|
||||||
|
@ -47,6 +49,7 @@ class JSONParser(BaseParser):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
media_type = 'application/json'
|
media_type = 'application/json'
|
||||||
|
renderer_class = UnicodeJSONRenderer
|
||||||
|
|
||||||
def parse(self, stream, media_type=None, parser_context=None):
|
def parse(self, stream, media_type=None, parser_context=None):
|
||||||
"""
|
"""
|
||||||
|
@ -91,6 +94,7 @@ class FormParser(BaseParser):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
media_type = 'application/x-www-form-urlencoded'
|
media_type = 'application/x-www-form-urlencoded'
|
||||||
|
supports_html_forms = True
|
||||||
|
|
||||||
def parse(self, stream, media_type=None, parser_context=None):
|
def parse(self, stream, media_type=None, parser_context=None):
|
||||||
"""
|
"""
|
||||||
|
@ -109,6 +113,7 @@ class MultiPartParser(BaseParser):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
media_type = 'multipart/form-data'
|
media_type = 'multipart/form-data'
|
||||||
|
supports_html_forms = True
|
||||||
|
|
||||||
def parse(self, stream, media_type=None, parser_context=None):
|
def parse(self, stream, media_type=None, parser_context=None):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -24,7 +24,7 @@ from rest_framework.settings import api_settings
|
||||||
from rest_framework.request import clone_request
|
from rest_framework.request import clone_request
|
||||||
from rest_framework.utils import encoders
|
from rest_framework.utils import encoders
|
||||||
from rest_framework.utils.breadcrumbs import get_breadcrumbs
|
from rest_framework.utils.breadcrumbs import get_breadcrumbs
|
||||||
from rest_framework import exceptions, parsers, status, VERSION
|
from rest_framework import exceptions, status, VERSION
|
||||||
|
|
||||||
|
|
||||||
class BaseRenderer(object):
|
class BaseRenderer(object):
|
||||||
|
@ -482,7 +482,7 @@ class BrowsableAPIRenderer(BaseRenderer):
|
||||||
if method in ('DELETE', 'OPTIONS'):
|
if method in ('DELETE', 'OPTIONS'):
|
||||||
return True # Don't actually need to return a form
|
return True # Don't actually need to return a form
|
||||||
|
|
||||||
if not getattr(view, 'get_serializer', None) or not parsers.FormParser in view.parser_classes:
|
if not getattr(view, 'get_serializer', None) or not any(parser.supports_html_forms for parser in view.parser_classes):
|
||||||
return
|
return
|
||||||
|
|
||||||
serializer = view.get_serializer(instance=obj)
|
serializer = view.get_serializer(instance=obj)
|
||||||
|
@ -561,6 +561,29 @@ class BrowsableAPIRenderer(BaseRenderer):
|
||||||
view = renderer_context['view']
|
view = renderer_context['view']
|
||||||
request = renderer_context['request']
|
request = renderer_context['request']
|
||||||
response = renderer_context['response']
|
response = renderer_context['response']
|
||||||
|
|
||||||
|
obj = getattr(view, 'object', None)
|
||||||
|
if getattr(view, 'get_serializer', None):
|
||||||
|
serializer = view.get_serializer(instance=obj)
|
||||||
|
else:
|
||||||
|
serializer = None
|
||||||
|
|
||||||
|
parsers = []
|
||||||
|
for parser_class in view.parser_classes:
|
||||||
|
content = None
|
||||||
|
renderer_class = getattr(parser_class, 'renderer_class', None)
|
||||||
|
if renderer_class and serializer:
|
||||||
|
renderer = renderer_class()
|
||||||
|
context = renderer_context.copy()
|
||||||
|
context['indent'] = 4
|
||||||
|
content = renderer.render(serializer.data, accepted_media_type, context)
|
||||||
|
print content
|
||||||
|
parsers.append({
|
||||||
|
'media_type': parser_class.media_type,
|
||||||
|
'content': content
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
media_types = [parser.media_type for parser in view.parser_classes]
|
media_types = [parser.media_type for parser in view.parser_classes]
|
||||||
|
|
||||||
renderer = self.get_default_renderer(view)
|
renderer = self.get_default_renderer(view)
|
||||||
|
|
|
@ -304,6 +304,8 @@ class BaseSerializer(WritableField):
|
||||||
ret.empty = obj is None
|
ret.empty = obj is None
|
||||||
|
|
||||||
for field_name, field in self.fields.items():
|
for field_name, field in self.fields.items():
|
||||||
|
if obj is None and field.read_only:
|
||||||
|
continue
|
||||||
field.initialize(parent=self, field_name=field_name)
|
field.initialize(parent=self, field_name=field_name)
|
||||||
key = self.get_field_key(field_name)
|
key = self.get_field_key(field_name)
|
||||||
value = field.field_to_native(obj, field_name)
|
value = field.field_to_native(obj, field_name)
|
||||||
|
|
|
@ -158,7 +158,7 @@ class BasicTests(TestCase):
|
||||||
'email': '',
|
'email': '',
|
||||||
'content': '',
|
'content': '',
|
||||||
'created': None,
|
'created': None,
|
||||||
'sub_comment': ''
|
#'sub_comment': ''
|
||||||
}
|
}
|
||||||
self.assertEqual(serializer.data, expected)
|
self.assertEqual(serializer.data, expected)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user