Changeset 5184
- Timestamp:
- 05/11/07 01:03:40 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/unicode/django/http/__init__.py
r5126 r5184 4 4 from urllib import urlencode, quote 5 5 from django.utils.datastructures import MultiValueDict 6 from django.utils.encoding import smart_str 6 7 7 8 RESERVED_CHARS="!*'();:@&=+$,/?%#[]" … … 75 76 76 77 class QueryDict(MultiValueDict): 77 """A specialized MultiValueDict that takes a query string when initialized. 78 This is immutable unless you create a copy of it.""" 79 def __init__(self, query_string, mutable=False): 78 """ 79 A specialized MultiValueDict that takes a query string when initialized. 80 This is immutable unless you create a copy of it. 81 82 Values retrieved from this class are converted from the default encoding to 83 unicode (this is done on retrieval, rather than input to avoid breaking 84 references or mutating referenced objects). 85 """ 86 def __init__(self, query_string, mutable=False, encoding=None): 80 87 MultiValueDict.__init__(self) 88 if not encoding: 89 # *Important*: do not import settings any earlier because of note 90 # in core.handlers.modpython. 91 from django.conf import settings 92 self.encoding = settings.DEFAULT_CHARSET 93 else: 94 self.encoding = encoding 81 95 self._mutable = True 82 96 for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True … … 88 102 raise AttributeError, "This QueryDict instance is immutable" 89 103 104 def __getitem__(self, key): 105 return str_to_unicode(MultiValueDict.__getitem__(self, key), self.encoding) 106 90 107 def __setitem__(self, key, value): 91 108 self._assert_mutable() 92 109 MultiValueDict.__setitem__(self, key, value) 110 111 def get(self, key, default=None): 112 return str_to_unicode(MultiValueDict.get(self, key, default), self.encoding) 93 113 94 114 def __copy__(self): … … 106 126 return result 107 127 128 def getlist(self, key): 129 """ 130 Returns a copy of the list associated with "key". This isn't a 131 reference to the original list because this method converts all the 132 values to unicode (without changing the original). 133 """ 134 return [str_to_unicode(v, self.encoding) for v in MultiValueDict.getlist(self, key)] 135 108 136 def setlist(self, key, list_): 109 137 self._assert_mutable() 110 138 MultiValueDict.setlist(self, key, list_) 111 139 140 def setlistdefault(self, key, default_list=()): 141 self._assert_mutable() 142 if key not in self: 143 self.setlist(key, default_list) 144 return MultiValueDict.getlist(self, key) 145 112 146 def appendlist(self, key, value): 113 147 self._assert_mutable() … … 120 154 def pop(self, key): 121 155 self._assert_mutable() 122 return MultiValueDict.pop(self, key)156 return [str_to_unicode(v, self.encoding) for v in MultiValueDict.pop(self, key)] 123 157 124 158 def popitem(self): 125 159 self._assert_mutable() 126 return MultiValueDict.popitem(self) 160 key, values = MultiValueDict.popitem(self) 161 return str_to_unicode(key, self.encoding), [str_to_unicode(v, self.encoding) for v in values] 162 163 def keys(self): 164 return [str_to_unicode(k, self.encoding) for k in MultiValueDict.keys(self)] 165 166 def values(self): 167 return [str_to_unicode(v, self.encoding) for v in MultiValueDict.values(self)] 168 169 def items(self): 170 return [(str_to_unicode(k, self.encoding), str_to_unicode(v, self.encoding)) for k, v in MultiValueDict.items(self)] 171 172 def lists(self): 173 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)] 127 174 128 175 def clear(self): … … 141 188 output = [] 142 189 for k, list_ in self.lists(): 143 output.extend([urlencode({k: v}) for v in list_]) 190 k = smart_str(k, self.encoding) 191 output.extend([urlencode({k: smart_str(v, self.encoding)}) for v in list_]) 144 192 return '&'.join(output) 145 193 … … 307 355 host = request.META.get('HTTP_HOST', '') 308 356 return host 357 358 # It's neither necessary nor appropriate to use 359 # django.utils.encoding.smart_unicode for parsing URLs and form inputs. Thus, 360 # this slightly more restricted function. 361 def str_to_unicode(s, encoding): 362 """ 363 Convert basestring objects to unicode, using the given encoding. 364 365 Returns any non-basestring objects without change. 366 """ 367 if isinstance(s, str): 368 return unicode(s, encoding) 369 else: 370 return s 371 django/branches/unicode/tests/regressiontests/httpwrappers/tests.py
r5126 r5184 17 17 18 18 >>> q.get('foo', 'default') 19 'default'19 u'default' 20 20 21 21 >>> q.getlist('foo') … … 95 95 96 96 >>> q['name'] 97 'john'98 99 >>> q.get('foo', 'default') 100 'default'97 u'john' 98 99 >>> q.get('foo', 'default') 100 u'default' 101 101 102 102 >>> q.get('name', 'default') 103 'john'103 u'john' 104 104 105 105 >>> q.getlist('name') 106 [ 'john']106 [u'john'] 107 107 108 108 >>> q.getlist('foo') … … 112 112 113 113 >>> q.get('foo', 'default') 114 'baz'115 116 >>> q.getlist('foo') 117 [ 'bar','baz']114 u'baz' 115 116 >>> q.getlist('foo') 117 [u'bar', u'baz'] 118 118 119 119 >>> q.appendlist('foo', 'another') 120 120 121 121 >>> q.getlist('foo') 122 [ 'bar', 'baz','another']123 124 >>> q['foo'] 125 'another'122 [u'bar', u'baz', u'another'] 123 124 >>> q['foo'] 125 u'another' 126 126 127 127 >>> q.has_key('foo') … … 132 132 133 133 >>> q.items() 134 [( 'foo', 'another'), ('name','john')]134 [(u'foo', u'another'), (u'name', u'john')] 135 135 136 136 >>> q.lists() 137 [( 'foo', ['bar', 'baz', 'another']), ('name', ['john'])]137 [(u'foo', [u'bar', u'baz', u'another']), (u'name', [u'john'])] 138 138 139 139 >>> q.keys() 140 [ 'foo','name']140 [u'foo', u'name'] 141 141 142 142 >>> q.values() 143 [ 'another','john']143 [u'another', u'john'] 144 144 145 145 >>> len(q) … … 150 150 # Displays last value 151 151 >>> q['foo'] 152 'hello'152 u'hello' 153 153 154 154 >>> q.get('foo', 'not available') 155 'hello'156 157 >>> q.getlist('foo') 158 [ 'bar', 'baz', 'another','hello']155 u'hello' 156 157 >>> q.getlist('foo') 158 [u'bar', u'baz', u'another', u'hello'] 159 159 160 160 >>> q.pop('foo') 161 [ 'bar', 'baz', 'another','hello']161 [u'bar', u'baz', u'another', u'hello'] 162 162 163 163 >>> q.get('foo', 'not there') 164 'not there'164 u'not there' 165 165 166 166 >>> q.setdefault('foo', 'bar') 167 'bar'168 169 >>> q['foo'] 170 'bar'171 172 >>> q.getlist('foo') 173 [ 'bar']167 u'bar' 168 169 >>> q['foo'] 170 u'bar' 171 172 >>> q.getlist('foo') 173 [u'bar'] 174 174 175 175 >>> q.urlencode() … … 188 188 189 189 >>> q['foo'] 190 'bar'190 u'bar' 191 191 192 192 >>> q['bar'] … … 201 201 202 202 >>> q.get('foo', 'default') 203 'bar'203 u'bar' 204 204 205 205 >>> q.get('bar', 'default') 206 'default'207 208 >>> q.getlist('foo') 209 [ 'bar']206 u'default' 207 208 >>> q.getlist('foo') 209 [u'bar'] 210 210 211 211 >>> q.getlist('bar') … … 235 235 236 236 >>> q.items() 237 [( 'foo','bar')]237 [(u'foo', u'bar')] 238 238 239 239 >>> q.lists() 240 [( 'foo', ['bar'])]240 [(u'foo', [u'bar'])] 241 241 242 242 >>> q.keys() 243 [ 'foo']243 [u'foo'] 244 244 245 245 >>> q.values() 246 [ 'bar']246 [u'bar'] 247 247 248 248 >>> len(q) … … 284 284 285 285 >>> q['vote'] 286 'no'286 u'no' 287 287 288 288 >>> q['something'] = 'bar' … … 292 292 293 293 >>> q.get('vote', 'default') 294 'no'295 296 >>> q.get('foo', 'default') 297 'default'294 u'no' 295 296 >>> q.get('foo', 'default') 297 u'default' 298 298 299 299 >>> q.getlist('vote') 300 [ 'yes','no']300 [u'yes', u'no'] 301 301 302 302 >>> q.getlist('foo') … … 326 326 327 327 >>> q.items() 328 [( 'vote','no')]328 [(u'vote', u'no')] 329 329 330 330 >>> q.lists() 331 [( 'vote', ['yes','no'])]331 [(u'vote', [u'yes', u'no'])] 332 332 333 333 >>> q.keys() 334 [ 'vote']334 [u'vote'] 335 335 336 336 >>> q.values() 337 [ 'no']337 [u'no'] 338 338 339 339 >>> len(q)
