Ticket #13721: content_type_extra.2.diff
File content_type_extra.2.diff, 9.3 KB (added by , 14 years ago) |
---|
-
django/core/files/uploadhandler.py
diff -r 6f0361df1c82 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 6f0361df1c82 django/http/multipartparser.py
a b 171 171 file_name = self.IE_sanitize(unescape_entities(file_name)) 172 172 173 173 content_type = meta_data.get('content-type', ('',))[0].strip() 174 content_type_extra = meta_data.get('content-type', (0,{}))[1] 175 if content_type_extra is None: 176 content_type_extra = {} 174 177 try: 175 charset = meta_data.get('content-type', (0,{}))[1].get('charset', None)178 charset = content_type_extra.get('charset', None) 176 179 except: 177 180 charset = None 178 181 … … 187 190 try: 188 191 handler.new_file(field_name, file_name, 189 192 content_type, content_length, 190 charset )193 charset, content_type_extra.copy()) 191 194 except StopFutureHandlers: 192 195 break 193 196 -
django/test/client.py
diff -r 6f0361df1c82 django/test/client.py
a b 146 146 147 147 def encode_file(boundary, key, file): 148 148 to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET) 149 content_type = mimetypes.guess_type(file.name)[0] 149 if hasattr(file, 'content_type'): 150 content_type = file.content_type 151 else: 152 content_type = mimetypes.guess_type(file.name)[0] 150 153 if content_type is None: 151 154 content_type = 'application/octet-stream' 152 155 return [ -
docs/topics/http/file-uploads.txt
diff -r 6f0361df1c82 docs/topics/http/file-uploads.txt
a b 200 200 For ``text/*`` content-types, the character set (i.e. ``utf8``) supplied 201 201 by the browser. Again, "trust but verify" is the best policy here. 202 202 203 .. attribute:: UploadedFile.content_type_extra 204 205 A dict containing the extra parameters that were passed to the 206 content-type header. 207 203 208 .. attribute:: UploadedFile.temporary_file_path() 204 209 205 210 Only files uploaded onto disk will have this method; it returns the full … … 357 362 358 363 The default is 64*2\ :sup:`10` bytes, or 64 KB. 359 364 360 ``FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset )``365 ``FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset, content_type_extra)`` 361 366 Callback signaling that a new file upload is starting. This is called 362 367 before any data has been fed to any upload handlers. 363 368 … … 374 379 ``charset`` is the character set (i.e. ``utf8``) given by the browser. 375 380 Like ``content_length``, this sometimes won't be provided. 376 381 382 ``content_type_extra`` is a dict containing the extra parameters that 383 were passed to the content-type header. 384 377 385 This method may raise a ``StopFutureHandlers`` exception to prevent 378 386 future handlers from handling this file. 379 387 -
tests/regressiontests/file_uploads/tests.py
diff -r 6f0361df1c82 tests/regressiontests/file_uploads/tests.py
a b 197 197 got = simplejson.loads(response.content) 198 198 self.assertTrue('f' not in got) 199 199 200 def test_extra_content_type(self): 201 f = tempfile.NamedTemporaryFile() 202 f.write('a' * (2 ** 21)) 203 f.seek(0) 204 f.content_type = 'text/plain; blob-key=upload blob key; other=test' 205 206 response = self.client.post("/file_uploads/content_type_extra/", {'f': f}) 207 got = simplejson.loads(response.content) 208 self.assertEqual(got['f'], 'upload blob key') 209 200 210 def test_broken_custom_upload_handler(self): 201 211 f = tempfile.NamedTemporaryFile() 202 212 f.write('a' * (2 ** 21)) -
tests/regressiontests/file_uploads/uploadhandler.py
diff -r 6f0361df1c82 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 6f0361df1c82 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'^echo_content/$', views.file_upload_echo_content), 10 (r'^quota/$', views.file_upload_quota), 11 (r'^quota/broken/$', views.file_upload_quota_broken), 12 (r'^getlist_count/$', views.file_upload_getlist_count), 13 (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'^echo_content/$', views.file_upload_echo_content), 10 (r'^quota/$', views.file_upload_quota), 11 (r'^quota/broken/$', views.file_upload_quota_broken), 12 (r'^getlist_count/$', views.file_upload_getlist_count), 13 (r'^upload_errors/$', views.file_upload_errors), 14 (r'^content_type_extra/$', views.file_upload_content_type_extra), 14 15 ) -
tests/regressiontests/file_uploads/views.py
diff -r 6f0361df1c82 tests/regressiontests/file_uploads/views.py
a b 4 4 from django.http import HttpResponse, HttpResponseServerError 5 5 from django.utils import simplejson 6 6 from models import FileModel, UPLOAD_TO 7 from uploadhandler import QuotaUploadHandler, ErroringUploadHandler 7 from uploadhandler import (QuotaUploadHandler, ErroringUploadHandler, 8 ContentTypeExtraUploadHandler) 8 9 from tests import UNICODE_FILENAME 9 10 10 11 def file_upload_view(request): … … 120 121 def file_upload_errors(request): 121 122 request.upload_handlers.insert(0, ErroringUploadHandler()) 122 123 return file_upload_echo(request) 124 125 def file_upload_content_type_extra(request): 126 request.upload_handlers.insert(0, ContentTypeExtraUploadHandler()) 127 r = dict([(k, f.read()) for k, f in request.FILES.items()]) 128 return HttpResponse(simplejson.dumps(r))