mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
52 tests passing. Refactored a few string / byte io.
This commit is contained in:
parent
b68263fb65
commit
e348ee9255
|
@ -21,6 +21,8 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from six import StringIO
|
from six import StringIO
|
||||||
|
|
||||||
|
from six import BytesIO
|
||||||
|
|
||||||
|
|
||||||
def get_concrete_model(model_cls):
|
def get_concrete_model(model_cls):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -8,8 +8,6 @@ import inspect
|
||||||
import re
|
import re
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from io import BytesIO
|
|
||||||
|
|
||||||
from django.core import validators
|
from django.core import validators
|
||||||
from django.core.exceptions import ObjectDoesNotExist, ValidationError
|
from django.core.exceptions import ObjectDoesNotExist, ValidationError
|
||||||
from django.core.urlresolvers import resolve, get_script_prefix
|
from django.core.urlresolvers import resolve, get_script_prefix
|
||||||
|
@ -25,6 +23,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||||
from rest_framework.reverse import reverse
|
from rest_framework.reverse import reverse
|
||||||
from rest_framework.compat import parse_date, parse_datetime
|
from rest_framework.compat import parse_date, parse_datetime
|
||||||
from rest_framework.compat import timezone
|
from rest_framework.compat import timezone
|
||||||
|
from rest_framework.compat import BytesIO
|
||||||
try:
|
try:
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
|
@ -5,6 +5,8 @@ They give us a generic way of being able to handle various media types
|
||||||
on the request, such as form content or json encoded data.
|
on the request, such as form content or json encoded data.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import six
|
||||||
|
|
||||||
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
|
from django.http.multipartparser import MultiPartParserError
|
||||||
|
@ -55,9 +57,10 @@ class JSONParser(BaseParser):
|
||||||
`files` will always be `None`.
|
`files` will always be `None`.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return json.load(stream)
|
data = stream.read().decode('iso-8859-1')
|
||||||
|
return json.loads(data)
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
raise ParseError('JSON parse error - %s' % unicode(exc))
|
raise ParseError('JSON parse error - %s' % six.text_type(exc))
|
||||||
|
|
||||||
|
|
||||||
class YAMLParser(BaseParser):
|
class YAMLParser(BaseParser):
|
||||||
|
@ -75,9 +78,10 @@ class YAMLParser(BaseParser):
|
||||||
`files` will always be `None`.
|
`files` will always be `None`.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return yaml.safe_load(stream)
|
data = stream.read().decode('iso-8859-1')
|
||||||
|
return yaml.safe_load(data)
|
||||||
except (ValueError, yaml.parser.ParserError) as exc:
|
except (ValueError, yaml.parser.ParserError) as exc:
|
||||||
raise ParseError('YAML parse error - %s' % unicode(exc))
|
raise ParseError('YAML parse error - %s' % six.u(exc))
|
||||||
|
|
||||||
|
|
||||||
class FormParser(BaseParser):
|
class FormParser(BaseParser):
|
||||||
|
@ -122,7 +126,7 @@ class MultiPartParser(BaseParser):
|
||||||
data, files = parser.parse()
|
data, files = parser.parse()
|
||||||
return DataAndFiles(data, files)
|
return DataAndFiles(data, files)
|
||||||
except MultiPartParserError as exc:
|
except MultiPartParserError as exc:
|
||||||
raise ParseError('Multipart form parse error - %s' % unicode(exc))
|
raise ParseError('Multipart form parse error - %s' % six.u(exc))
|
||||||
|
|
||||||
|
|
||||||
class XMLParser(BaseParser):
|
class XMLParser(BaseParser):
|
||||||
|
@ -136,7 +140,7 @@ class XMLParser(BaseParser):
|
||||||
try:
|
try:
|
||||||
tree = ET.parse(stream)
|
tree = ET.parse(stream)
|
||||||
except (ExpatError, ETParseError, ValueError) as exc:
|
except (ExpatError, ETParseError, ValueError) as exc:
|
||||||
raise ParseError('XML parse error - %s' % unicode(exc))
|
raise ParseError('XML parse error - %s' % six.u(exc))
|
||||||
data = self._xml_convert(tree.getroot())
|
data = self._xml_convert(tree.getroot())
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -9,7 +9,8 @@ The wrapped request then offers a richer API, in particular :
|
||||||
- full support of PUT method, including support for file uploads
|
- full support of PUT method, including support for file uploads
|
||||||
- form overloading of HTTP method, content type and content
|
- form overloading of HTTP method, content type and content
|
||||||
"""
|
"""
|
||||||
from rest_framework.compat import StringIO
|
import six
|
||||||
|
from rest_framework.compat import BytesIO
|
||||||
|
|
||||||
from django.http.multipartparser import parse_header
|
from django.http.multipartparser import parse_header
|
||||||
from rest_framework import exceptions
|
from rest_framework import exceptions
|
||||||
|
@ -20,7 +21,7 @@ def is_form_media_type(media_type):
|
||||||
"""
|
"""
|
||||||
Return True if the media type is a valid form media type.
|
Return True if the media type is a valid form media type.
|
||||||
"""
|
"""
|
||||||
base_media_type, params = parse_header(media_type.encode('utf8'))
|
base_media_type, params = parse_header(media_type.encode('iso-8859-1'))
|
||||||
return (base_media_type == 'application/x-www-form-urlencoded' or
|
return (base_media_type == 'application/x-www-form-urlencoded' or
|
||||||
base_media_type == 'multipart/form-data')
|
base_media_type == 'multipart/form-data')
|
||||||
|
|
||||||
|
@ -216,7 +217,7 @@ class Request(object):
|
||||||
elif hasattr(self._request, 'read'):
|
elif hasattr(self._request, 'read'):
|
||||||
self._stream = self._request
|
self._stream = self._request
|
||||||
else:
|
else:
|
||||||
self._stream = StringIO(self.raw_post_data)
|
self._stream = BytesIO(self.raw_post_data)
|
||||||
|
|
||||||
def _perform_form_overloading(self):
|
def _perform_form_overloading(self):
|
||||||
"""
|
"""
|
||||||
|
@ -251,7 +252,7 @@ class Request(object):
|
||||||
self._CONTENT_PARAM in self._data and
|
self._CONTENT_PARAM in self._data and
|
||||||
self._CONTENTTYPE_PARAM in self._data):
|
self._CONTENTTYPE_PARAM in self._data):
|
||||||
self._content_type = self._data[self._CONTENTTYPE_PARAM]
|
self._content_type = self._data[self._CONTENTTYPE_PARAM]
|
||||||
self._stream = StringIO(self._data[self._CONTENT_PARAM])
|
self._stream = BytesIO(self._data[self._CONTENT_PARAM].encode('iso-8859-1'))
|
||||||
self._data, self._files = (Empty, Empty)
|
self._data, self._files = (Empty, Empty)
|
||||||
|
|
||||||
def _parse(self):
|
def _parse(self):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
import six
|
||||||
|
|
||||||
from django import template
|
from django import template
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
|
@ -104,7 +105,7 @@ def add_class(value, css_class):
|
||||||
In the case of REST Framework, the filter is used to add Bootstrap-specific
|
In the case of REST Framework, the filter is used to add Bootstrap-specific
|
||||||
classes to the forms.
|
classes to the forms.
|
||||||
"""
|
"""
|
||||||
html = unicode(value)
|
html = six.text_type(value)
|
||||||
match = class_re.search(html)
|
match = class_re.search(html)
|
||||||
if match:
|
if match:
|
||||||
m = re.search(r'^%s$|^%s\s|\s%s\s|\s%s$' % (css_class, css_class,
|
m = re.search(r'^%s$|^%s\s|\s%s\s|\s%s$' % (css_class, css_class,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from rest_framework.compat import StringIO
|
from rest_framework.compat import BytesIO
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
@ -29,9 +29,9 @@ class FileSerializerTests(TestCase):
|
||||||
|
|
||||||
def test_create(self):
|
def test_create(self):
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
file = StringIO('stuff')
|
file = BytesIO(b'stuff')
|
||||||
file.name = 'stuff.txt'
|
file.name = 'stuff.txt'
|
||||||
file.size = file.len
|
file.size = len(file.getvalue())
|
||||||
serializer = UploadedFileSerializer(data={'created': now}, files={'file': file})
|
serializer = UploadedFileSerializer(data={'created': now}, files={'file': file})
|
||||||
uploaded_file = UploadedFile(file=file, created=now)
|
uploaded_file = UploadedFile(file=file, created=now)
|
||||||
self.assertTrue(serializer.is_valid())
|
self.assertTrue(serializer.is_valid())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user