52 tests passing. Refactored a few string / byte io.

This commit is contained in:
Xavier Ordoquy 2012-11-23 01:12:33 +01:00
parent b68263fb65
commit e348ee9255
6 changed files with 23 additions and 16 deletions

View File

@ -21,6 +21,8 @@ try:
except ImportError:
from six import StringIO
from six import BytesIO
def get_concrete_model(model_cls):
try:

View File

@ -8,8 +8,6 @@ import inspect
import re
import warnings
from io import BytesIO
from django.core import validators
from django.core.exceptions import ObjectDoesNotExist, ValidationError
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.compat import parse_date, parse_datetime
from rest_framework.compat import timezone
from rest_framework.compat import BytesIO
try:
from urllib.parse import urlparse
except ImportError:

View File

@ -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.
"""
import six
from django.http import QueryDict
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
from django.http.multipartparser import MultiPartParserError
@ -55,9 +57,10 @@ class JSONParser(BaseParser):
`files` will always be `None`.
"""
try:
return json.load(stream)
data = stream.read().decode('iso-8859-1')
return json.loads(data)
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):
@ -75,9 +78,10 @@ class YAMLParser(BaseParser):
`files` will always be `None`.
"""
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:
raise ParseError('YAML parse error - %s' % unicode(exc))
raise ParseError('YAML parse error - %s' % six.u(exc))
class FormParser(BaseParser):
@ -122,7 +126,7 @@ class MultiPartParser(BaseParser):
data, files = parser.parse()
return DataAndFiles(data, files)
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):
@ -136,7 +140,7 @@ class XMLParser(BaseParser):
try:
tree = ET.parse(stream)
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())
return data

View File

@ -9,7 +9,8 @@ The wrapped request then offers a richer API, in particular :
- full support of PUT method, including support for file uploads
- 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 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.
"""
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
base_media_type == 'multipart/form-data')
@ -216,7 +217,7 @@ class Request(object):
elif hasattr(self._request, 'read'):
self._stream = self._request
else:
self._stream = StringIO(self.raw_post_data)
self._stream = BytesIO(self.raw_post_data)
def _perform_form_overloading(self):
"""
@ -251,7 +252,7 @@ class Request(object):
self._CONTENT_PARAM in self._data and
self._CONTENTTYPE_PARAM in self._data):
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)
def _parse(self):

View File

@ -1,4 +1,5 @@
from __future__ import unicode_literals
import six
from django import template
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
classes to the forms.
"""
html = unicode(value)
html = six.text_type(value)
match = class_re.search(html)
if match:
m = re.search(r'^%s$|^%s\s|\s%s\s|\s%s$' % (css_class, css_class,

View File

@ -1,4 +1,4 @@
from rest_framework.compat import StringIO
from rest_framework.compat import BytesIO
import datetime
@ -29,9 +29,9 @@ class FileSerializerTests(TestCase):
def test_create(self):
now = datetime.datetime.now()
file = StringIO('stuff')
file = BytesIO(b'stuff')
file.name = 'stuff.txt'
file.size = file.len
file.size = len(file.getvalue())
serializer = UploadedFileSerializer(data={'created': now}, files={'file': file})
uploaded_file = UploadedFile(file=file, created=now)
self.assertTrue(serializer.is_valid())