diff --git a/AUTHORS b/AUTHORS index 27eaf5bd1d..3aaa3d57ba 100644 --- a/AUTHORS +++ b/AUTHORS @@ -101,6 +101,7 @@ answer newbie questions, and generally made Django that much better: dusk@woofle.net Andy Dustman Clint Ecker + eibaan@gmail.com enlight Enrico A. Murat Eren diff --git a/django/http/__init__.py b/django/http/__init__.py index 22de62729a..7cd47481dc 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -2,7 +2,7 @@ import os from Cookie import SimpleCookie from pprint import pformat from urllib import urlencode -from django.utils.datastructures import MultiValueDict +from django.utils.datastructures import MultiValueDict, FileDict from django.utils.encoding import smart_str, iri_to_uri, force_unicode RESERVED_CHARS="!*'();:@&=+$,/?%#[]" @@ -88,11 +88,11 @@ def parse_file_upload(header_dict, post_data): # directory separator, which may not be the same as the # client's one.) filename = name_dict['filename'][name_dict['filename'].rfind("\\")+1:] - FILES.appendlist(name_dict['name'], { + FILES.appendlist(name_dict['name'], FileDict({ 'filename': filename, 'content-type': 'Content-Type' in submessage and submessage['Content-Type'] or None, 'content': submessage.get_payload(), - }) + })) else: POST.appendlist(name_dict['name'], submessage.get_payload()) return POST, FILES diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index 60bc0051a2..4b60d1d194 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -267,3 +267,16 @@ class DotExpandedDict(dict): current[bits[-1]] = v except TypeError: # Special-case if current isn't a dict. current = {bits[-1] : v} + +class FileDict(dict): + """ + A dictionary used to hold uploaded file contents. The only special feature + here is that repr() of this object won't dump the entire contents of the + file to the output. A handy safeguard for a large file upload. + """ + def __repr__(self): + if 'content' in self: + d = dict(self, content='') + return dict.__repr__(d) + return dict.__repr__(self) + diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py index 18eb4fcccd..3920e1ca40 100644 --- a/tests/regressiontests/datastructures/tests.py +++ b/tests/regressiontests/datastructures/tests.py @@ -64,4 +64,13 @@ True ['Holovaty'] >>> d['person']['2']['firstname'] ['Adrian'] + +### FileDict ################################################################ + +>>> d = FileDict({'content': 'once upon a time...'}) +>>> repr(d) +"{'content': ''}" +>>> d = FileDict({'other-key': 'once upon a time...'}) +>>> repr(d) +"{'other-key': 'once upon a time...'}" """