Changeset 5464
- Timestamp:
- 06/11/07 15:02:08 (1 year ago)
- Files:
-
- django/branches/unicode/django/http/__init__.py (modified) (8 diffs)
- django/branches/unicode/django/test/client.py (modified) (1 diff)
- django/branches/unicode/tests/regressiontests/httpwrappers/tests.py (modified) (6 diffs)
- django/branches/unicode/tests/regressiontests/test_client_regress/models.py (modified) (2 diffs)
- django/branches/unicode/tests/regressiontests/test_client_regress/urls.py (modified) (1 diff)
- django/branches/unicode/tests/regressiontests/test_client_regress/views.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/unicode/django/http/__init__.py
r5461 r5464 4 4 from urllib import urlencode 5 5 from django.utils.datastructures import MultiValueDict 6 from django.utils.encoding import smart_str, iri_to_uri 6 from django.utils.encoding import smart_str, iri_to_uri, force_unicode 7 7 8 8 RESERVED_CHARS="!*'();:@&=+$,/?%#[]" … … 50 50 def _set_encoding(self, val): 51 51 """ 52 Sets the encoding used for GET/POST accesses. 52 Sets the encoding used for GET/POST accesses. If the GET or POST 53 dictionary has already been created it is removed and recreated on the 54 next access (so that it is decoded correctly). 53 55 """ 54 56 self._encoding = val 55 57 if hasattr(self, '_get'): 56 self.GET.encoding = val58 del self._get 57 59 if hasattr(self, '_post'): 58 self.POST.encoding = val60 del self._post 59 61 60 62 def _get_encoding(self): … … 114 116 self._mutable = True 115 117 for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True 116 self.appendlist( key, value)118 self.appendlist(force_unicode(key, errors='replace'), force_unicode(value, errors='replace')) 117 119 self._mutable = mutable 118 120 … … 121 123 raise AttributeError, "This QueryDict instance is immutable" 122 124 123 def __getitem__(self, key):124 return str_to_unicode(MultiValueDict.__getitem__(self, key), self.encoding)125 126 125 def __setitem__(self, key, value): 127 126 self._assert_mutable() 127 key = str_to_unicode(key, self.encoding) 128 value = str_to_unicode(value, self.encoding) 128 129 MultiValueDict.__setitem__(self, key, value) 129 130 … … 131 132 self._assert_mutable() 132 133 super(QueryDict, self).__delitem__(key) 133 134 def get(self, key, default=None):135 return str_to_unicode(MultiValueDict.get(self, key, default), self.encoding)136 134 137 135 def __copy__(self): … … 149 147 return result 150 148 151 def getlist(self, key):152 """153 Returns a copy of the list associated with "key". This isn't a154 reference to the original list because this method converts all the155 values to unicode (without changing the original).156 """157 return [str_to_unicode(v, self.encoding) for v in MultiValueDict.getlist(self, key)]158 159 149 def setlist(self, key, list_): 160 150 self._assert_mutable() 151 key = str_to_unicode(key, self.encoding) 152 list_ = [str_to_unicode(elt, self.encoding) for elt in list_] 161 153 MultiValueDict.setlist(self, key, list_) 162 154 … … 169 161 def appendlist(self, key, value): 170 162 self._assert_mutable() 163 key = str_to_unicode(key, self.encoding) 164 value = str_to_unicode(value, self.encoding) 171 165 MultiValueDict.appendlist(self, key, value) 172 166 173 167 def update(self, other_dict): 174 168 self._assert_mutable() 175 MultiValueDict.update(self, other_dict) 169 f = lambda s: str_to_unicode(s, self.encoding) 170 d = dict([(f(k), f(v)) for k, v in other_dict.items()]) 171 MultiValueDict.update(self, d) 176 172 177 173 def pop(self, key, *args): 178 174 self._assert_mutable() 179 val = MultiValueDict.pop(self, key, *args) 180 if isinstance(val, list): 181 return [str_to_unicode(v, self.encoding) for v in val] 182 return str_to_unicode(val, self.encoding) 175 return MultiValueDict.pop(self, key, *args) 183 176 184 177 def popitem(self): 185 178 self._assert_mutable() 186 key, values = MultiValueDict.popitem(self) 187 return str_to_unicode(key, self.encoding), [str_to_unicode(v, self.encoding) for v in values] 188 189 def keys(self): 190 return [str_to_unicode(k, self.encoding) for k in MultiValueDict.keys(self)] 191 192 def values(self): 193 return [str_to_unicode(v, self.encoding) for v in MultiValueDict.values(self)] 194 195 def items(self): 196 return [(str_to_unicode(k, self.encoding), str_to_unicode(v, self.encoding)) for k, v in MultiValueDict.items(self)] 197 198 def lists(self): 199 return [(str_to_unicode(k, self.encoding), [str_to_unicode(v, self.encoding) for v in v_list]) for k, v_list in MultiValueDict.lists(self)] 179 return MultiValueDict.popitem(self) 200 180 201 181 def clear(self): … … 203 183 MultiValueDict.clear(self) 204 184 205 def setdefault(self, *args): 206 self._assert_mutable() 207 return MultiValueDict.setdefault(self, *args) 185 def setdefault(self, key, default=None): 186 self._assert_mutable() 187 key = str_to_unicode(key, self.encoding) 188 default = str_to_unicode(default, self.encoding) 189 return MultiValueDict.setdefault(self, key, default) 208 190 209 191 def copy(self): django/branches/unicode/django/test/client.py
r5338 r5464 69 69 lines.extend([ 70 70 '--' + boundary, 71 'Content-Disposition: form-data; name="%s"' % to_str(key), 72 '', 73 '--' + boundary, 74 'Content-Disposition: form-data; name="%s_file"; filename="%s"' % (to_str(key), to_str(value.name)), 71 'Content-Disposition: form-data; name="%s"; filename="%s"' % (to_str(key), to_str(value.name)), 75 72 'Content-Type: application/octet-stream', 76 73 '', django/branches/unicode/tests/regressiontests/httpwrappers/tests.py
r5310 r5464 17 17 18 18 >>> q.get('foo', 'default') 19 u'default'19 'default' 20 20 21 21 >>> q.getlist('foo') … … 104 104 105 105 >>> q.get('foo', 'default') 106 u'default'106 'default' 107 107 108 108 >>> q.get('name', 'default') … … 168 168 169 169 >>> q.pop('foo', 'not there') 170 u'not there'170 'not there' 171 171 172 172 >>> q.get('foo', 'not there') 173 u'not there'173 'not there' 174 174 175 175 >>> q.setdefault('foo', 'bar') … … 202 202 Traceback (most recent call last): 203 203 ... 204 MultiValueDictKeyError: "Key 'bar' not found in <MultiValueDict: { 'foo': ['bar']}>"204 MultiValueDictKeyError: "Key 'bar' not found in <MultiValueDict: {u'foo': [u'bar']}>" 205 205 206 206 >>> q['something'] = 'bar' … … 213 213 214 214 >>> q.get('bar', 'default') 215 u'default'215 'default' 216 216 217 217 >>> q.getlist('foo') … … 304 304 305 305 >>> q.get('foo', 'default') 306 u'default'306 'default' 307 307 308 308 >>> q.getlist('vote') django/branches/unicode/tests/regressiontests/test_client_regress/models.py
r5241 r5464 5 5 from django.test import Client, TestCase 6 6 from django.core import mail 7 import os 7 8 8 9 class AssertTemplateUsedTests(TestCase): … … 163 164 self.assertEqual(str(e), "The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])") 164 165 166 class AssertFileUploadTests(TestCase): 167 def test_simple_upload(self): 168 fd = open(os.path.join(os.path.dirname(__file__), "views.py")) 169 post_data = { 170 'name': 'Ringo', 171 'file_field': fd, 172 } 173 response = self.client.post('/test_client_regress/file_upload/', post_data) 174 self.assertEqual(response.status_code, 200) django/branches/unicode/tests/regressiontests/test_client_regress/urls.py
r5185 r5464 1 1 from django.conf.urls.defaults import * 2 from django.views.generic.simple import redirect_to3 2 import views 4 3 5 4 urlpatterns = patterns('', 6 5 (r'^no_template_view/$', views.no_template_view), 6 (r'^file_upload/$', views.file_upload_view), 7 7 ) django/branches/unicode/tests/regressiontests/test_client_regress/views.py
r5185 r5464 1 1 from django.core.mail import EmailMessage, SMTPConnection 2 from django.http import HttpResponse 2 from django.http import HttpResponse, HttpResponseServerError 3 3 from django.shortcuts import render_to_response 4 4 … … 7 7 return HttpResponse("No template used") 8 8 9 def file_upload_view(request): 10 """ 11 Check that a file upload can be updated into the POST dictionary without 12 going pear-shaped. 13 """ 14 form_data = request.POST.copy() 15 form_data.update(request.FILES) 16 if isinstance(form_data['file_field'], dict) and isinstance(form_data['name'], unicode): 17 return HttpResponse('') 18 else: 19 return HttpResponseServerError() 20
