Ticket #13721: content_type_extra.diff
File content_type_extra.diff, 9.2 KB (added by , 14 years ago) |
---|
-
django/core/files/uploadhandler.py
diff -r 9b24e08becc2 django/core/files/uploadhandler.py
a b 84 84 """ 85 85 pass 86 86 87 def new_file(self, field_name, file_name, content_type, content_length, charset=None): 87 def new_file(self, field_name, file_name, content_type, content_length, 88 charset=None, content_type_extra=None): 88 89 """ 89 90 Signal that a new file has been started. 90 91 … … 96 97 self.content_type = content_type 97 98 self.content_length = content_length 98 99 self.charset = charset 100 if content_type_extra is None: 101 content_type_extra = {} 102 self.content_type_extra = content_type_extra 99 103 100 104 def receive_data_chunk(self, raw_data, start): 101 105 """ -
django/http/multipartparser.py
diff -r 9b24e08becc2 django/http/multipartparser.py
a b 169 169 file_name = self.IE_sanitize(unescape_entities(file_name)) 170 170 171 171 content_type = meta_data.get('content-type', ('',))[0].strip() 172 content_type_extra = meta_data.get('content-type', (0,{}))[1] 173 if content_type_extra is None: 174 content_type_extra = {} 172 175 try: 173 charset = meta_data.get('content-type', (0,{}))[1].get('charset', None)176 charset = content_type_extra.get('charset', None) 174 177 except: 175 178 charset = None 176 179 … … 185 188 try: 186 189 handler.new_file(field_name, file_name, 187 190 content_type, content_length, 188 charset )191 charset, content_type_extra.copy()) 189 192 except StopFutureHandlers: 190 193 break 191 194 -
django/test/client.py
diff -r 9b24e08becc2 django/test/client.py
a b 142 142 143 143 def encode_file(boundary, key, file): 144 144 to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET) 145 content_type = mimetypes.guess_type(file.name)[0] 145 if hasattr(file, 'content_type'): 146 content_type = file.content_type 147 else: 148 content_type = mimetypes.guess_type(file.name)[0] 146 149 if content_type is None: 147 150 content_type = 'application/octet-stream' 148 151 return [ -
docs/topics/http/file-uploads.txt
diff -r 9b24e08becc2 docs/topics/http/file-uploads.txt
a b 195 195 For ``text/*`` content-types, the character set (i.e. ``utf8``) supplied 196 196 by the browser. Again, "trust but verify" is the best policy here. 197 197 198 ``UploadedFile.content_type_extra`` 199 A dict containing the extra parameters that were passed to the 200 content-type header. 201 198 202 ``UploadedFile.temporary_file_path()`` 199 203 Only files uploaded onto disk will have this method; it returns the full 200 204 path to the temporary uploaded file. … … 351 355 352 356 The default is 64*2\ :sup:`10` bytes, or 64 KB. 353 357 354 ``FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset )``358 ``FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset, content_type_extra)`` 355 359 Callback signaling that a new file upload is starting. This is called 356 360 before any data has been fed to any upload handlers. 357 361 … … 369 373 ``charset`` is the character set (i.e. ``utf8``) given by the browser. 370 374 Like ``content_length``, this sometimes won't be provided. 371 375 376 ``content_type_extra`` is a dict containing the extra parameters that 377 were passed to the content-type header. 378 372 379 This method may raise a ``StopFutureHandlers`` exception to prevent 373 380 future handlers from handling this file. 374 381 -
tests/regressiontests/file_uploads/tests.py
diff -r 9b24e08becc2 tests/regressiontests/file_uploads/tests.py
a b 170 170 got = simplejson.loads(response.content) 171 171 self.assert_('f' not in got) 172 172 173 def test_extra_content_type(self): 174 f = tempfile.NamedTemporaryFile() 175 f.write('a' * (2 ** 21)) 176 f.seek(0) 177 f.content_type = 'text/plain; blob-key=upload blob key; other=test' 178 179 response = self.client.post("/file_uploads/content_type_extra/", {'f': f}) 180 got = simplejson.loads(response.content) 181 self.assertEqual(got['f'], 'upload blob key') 182 173 183 def test_broken_custom_upload_handler(self): 174 184 f = tempfile.NamedTemporaryFile() 175 185 f.write('a' * (2 ** 21)) -
tests/regressiontests/file_uploads/uploadhandler.py
diff -r 9b24e08becc2 tests/regressiontests/file_uploads/uploadhandler.py
a b 2 2 Upload handlers to test the upload API. 3 3 """ 4 4 5 from django.core.files.uploadhandler import FileUploadHandler, StopUpload 5 from django.core.files.uploadedfile import InMemoryUploadedFile 6 from django.core.files.uploadhandler import (FileUploadHandler, StopUpload, 7 StopFutureHandlers) 8 from StringIO import StringIO 6 9 7 10 class QuotaUploadHandler(FileUploadHandler): 8 11 """ … … 32 35 """A handler that raises an exception.""" 33 36 def receive_data_chunk(self, raw_data, start): 34 37 raise CustomUploadError("Oops!") 38 39 class ContentTypeExtraUploadHandler(FileUploadHandler): 40 """ 41 File upload handler that handles content_type_extra 42 """ 43 44 def new_file(self, *args, **kwargs): 45 super(ContentTypeExtraUploadHandler, self).new_file(*args, **kwargs) 46 self.blobkey = self.content_type_extra.get('blob-key', '') 47 self.file = StringIO() 48 self.file.write(self.blobkey) 49 self.active = self.blobkey is not None 50 if self.active: 51 raise StopFutureHandlers() 52 53 def receive_data_chunk(self, raw_data, start): 54 """ 55 Add the data to the StringIO file. 56 """ 57 if not self.active: 58 return raw_data 59 60 def file_complete(self, file_size): 61 if not self.active: 62 return 63 64 self.file.seek(0) 65 return InMemoryUploadedFile( 66 file = self.file, 67 field_name = self.field_name, 68 name = self.file_name, 69 content_type = self.content_type, 70 size = file_size, 71 charset = self.charset 72 ) -
tests/regressiontests/file_uploads/urls.py
diff -r 9b24e08becc2 tests/regressiontests/file_uploads/urls.py
a b 2 2 import views 3 3 4 4 urlpatterns = patterns('', 5 (r'^upload/$', views.file_upload_view), 6 (r'^verify/$', views.file_upload_view_verify), 7 (r'^unicode_name/$', views.file_upload_unicode_name), 8 (r'^echo/$', views.file_upload_echo), 9 (r'^quota/$', views.file_upload_quota), 10 (r'^quota/broken/$', views.file_upload_quota_broken), 11 (r'^getlist_count/$', views.file_upload_getlist_count), 12 (r'^upload_errors/$', views.file_upload_errors), 5 (r'^upload/$', views.file_upload_view), 6 (r'^verify/$', views.file_upload_view_verify), 7 (r'^unicode_name/$', views.file_upload_unicode_name), 8 (r'^echo/$', views.file_upload_echo), 9 (r'^quota/$', views.file_upload_quota), 10 (r'^quota/broken/$', views.file_upload_quota_broken), 11 (r'^getlist_count/$', views.file_upload_getlist_count), 12 (r'^upload_errors/$', views.file_upload_errors), 13 (r'^content_type_extra/$', views.file_upload_content_type_extra), 13 14 ) -
tests/regressiontests/file_uploads/views.py
diff -r 9b24e08becc2 tests/regressiontests/file_uploads/views.py
a b 3 3 from django.http import HttpResponse, HttpResponseServerError 4 4 from django.utils import simplejson 5 5 from models import FileModel, UPLOAD_TO 6 from uploadhandler import QuotaUploadHandler, ErroringUploadHandler 6 from uploadhandler import (QuotaUploadHandler, ErroringUploadHandler, 7 ContentTypeExtraUploadHandler) 7 8 from django.utils.hashcompat import sha_constructor 8 9 from tests import UNICODE_FILENAME 9 10 … … 112 113 def file_upload_errors(request): 113 114 request.upload_handlers.insert(0, ErroringUploadHandler()) 114 115 return file_upload_echo(request) 116 117 def file_upload_content_type_extra(request): 118 request.upload_handlers.insert(0, ContentTypeExtraUploadHandler()) 119 r = dict([(k, f.read()) for k, f in request.FILES.items()]) 120 return HttpResponse(simplejson.dumps(r))