mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-10-31 07:57:55 +03:00 
			
		
		
		
	Merge pull request #1377 from Ian-Foote/force_bytes
Import force_bytes on django >= 1.5
This commit is contained in:
		
						commit
						8f921160e5
					
				|  | @ -457,7 +457,7 @@ from django.test.client import RequestFactory as DjangoRequestFactory | ||||||
| from django.test.client import FakePayload | from django.test.client import FakePayload | ||||||
| try: | try: | ||||||
|     # In 1.5 the test client uses force_bytes |     # In 1.5 the test client uses force_bytes | ||||||
|     from django.utils.encoding import force_bytes_or_smart_bytes |     from django.utils.encoding import force_bytes as force_bytes_or_smart_bytes | ||||||
| except ImportError: | except ImportError: | ||||||
|     # In 1.3 and 1.4 the test client just uses smart_str |     # In 1.3 and 1.4 the test client just uses smart_str | ||||||
|     from django.utils.encoding import smart_str as force_bytes_or_smart_bytes |     from django.utils.encoding import smart_str as force_bytes_or_smart_bytes | ||||||
|  |  | ||||||
|  | @ -10,6 +10,7 @@ from __future__ import unicode_literals | ||||||
| 
 | 
 | ||||||
| import copy | import copy | ||||||
| import json | import json | ||||||
|  | import django | ||||||
| from django import forms | from django import forms | ||||||
| from django.core.exceptions import ImproperlyConfigured | from django.core.exceptions import ImproperlyConfigured | ||||||
| from django.http.multipartparser import parse_header | from django.http.multipartparser import parse_header | ||||||
|  | @ -597,7 +598,7 @@ class MultiPartRenderer(BaseRenderer): | ||||||
|     media_type = 'multipart/form-data; boundary=BoUnDaRyStRiNg' |     media_type = 'multipart/form-data; boundary=BoUnDaRyStRiNg' | ||||||
|     format = 'multipart' |     format = 'multipart' | ||||||
|     charset = 'utf-8' |     charset = 'utf-8' | ||||||
|     BOUNDARY = 'BoUnDaRyStRiNg' |     BOUNDARY = 'BoUnDaRyStRiNg' if django.VERSION >= (1, 5) else b'BoUnDaRyStRiNg' | ||||||
| 
 | 
 | ||||||
|     def render(self, data, accepted_media_type=None, renderer_context=None): |     def render(self, data, accepted_media_type=None, renderer_context=None): | ||||||
|         return encode_multipart(self.BOUNDARY, data) |         return encode_multipart(self.BOUNDARY, data) | ||||||
|  |  | ||||||
|  | @ -1,6 +1,8 @@ | ||||||
| # -- coding: utf-8 -- | # -- coding: utf-8 -- | ||||||
| 
 | 
 | ||||||
| from __future__ import unicode_literals | from __future__ import unicode_literals | ||||||
|  | from io import BytesIO | ||||||
|  | 
 | ||||||
| from django.contrib.auth.models import User | from django.contrib.auth.models import User | ||||||
| from django.test import TestCase | from django.test import TestCase | ||||||
| from rest_framework.compat import patterns, url | from rest_framework.compat import patterns, url | ||||||
|  | @ -143,3 +145,10 @@ class TestAPIRequestFactory(TestCase): | ||||||
|         force_authenticate(request, user=user) |         force_authenticate(request, user=user) | ||||||
|         response = view(request) |         response = view(request) | ||||||
|         self.assertEqual(response.data['user'], 'example') |         self.assertEqual(response.data['user'], 'example') | ||||||
|  | 
 | ||||||
|  |     def test_upload_file(self): | ||||||
|  |         # This is a 1x1 black png | ||||||
|  |         simple_png = BytesIO(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc````\x00\x00\x00\x05\x00\x01\xa5\xf6E@\x00\x00\x00\x00IEND\xaeB`\x82') | ||||||
|  |         simple_png.name = 'test.png' | ||||||
|  |         factory = APIRequestFactory() | ||||||
|  |         factory.post('/', data={'image': simple_png}) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user