Changeset 7205 for django/trunk/django/http/__init__.py
- Timestamp:
- 03/07/08 21:46:33 (9 months ago)
- Files:
-
- django/trunk/django/http/__init__.py (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/http/__init__.py
r7204 r7205 4 4 from urllib import urlencode 5 5 from urlparse import urljoin 6 from django.utils.datastructures import MultiValueDict, FileDict7 from django.utils.encoding import smart_str, iri_to_uri, force_unicode8 from utils import *9 10 RESERVED_CHARS="!*'();:@&=+$,/?%#[]"11 12 6 try: 13 7 # The mod_python version is more efficient, so try importing it first. … … 16 10 from cgi import parse_qsl 17 11 12 from django.utils.datastructures import MultiValueDict, FileDict 13 from django.utils.encoding import smart_str, iri_to_uri, force_unicode 14 15 from utils import * 16 17 RESERVED_CHARS="!*'();:@&=+$,/?%#[]" 18 19 18 20 class Http404(Exception): 19 21 pass 20 22 21 23 class HttpRequest(object): 22 " A basic HTTP request"24 """A basic HTTP request.""" 23 25 24 26 # The encoding used in GET/POST dicts. None means use default setting. … … 47 49 48 50 def get_host(self): 49 " Returns the HTTP host using the environment or request headers."51 """Returns the HTTP host using the environment or request headers.""" 50 52 # We try three options, in order of decreasing preference. 51 53 if 'HTTP_X_FORWARDED_HOST' in self.META: … … 99 101 100 102 def parse_file_upload(header_dict, post_data): 101 " Returns a tuple of (POST QueryDict, FILES MultiValueDict)"103 """Returns a tuple of (POST QueryDict, FILES MultiValueDict).""" 102 104 import email, email.Message 103 105 from cgi import parse_header … … 131 133 return POST, FILES 132 134 135 133 136 class QueryDict(MultiValueDict): 134 137 """ … … 149 152 self._mutable = True 150 153 for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True 151 self.appendlist(force_unicode(key, encoding, errors='replace'), force_unicode(value, encoding, errors='replace')) 154 self.appendlist(force_unicode(key, encoding, errors='replace'), 155 force_unicode(value, encoding, errors='replace')) 152 156 self._mutable = mutable 153 157 154 158 def _assert_mutable(self): 155 159 if not self._mutable: 156 raise AttributeError , "This QueryDict instance is immutable"160 raise AttributeError("This QueryDict instance is immutable") 157 161 158 162 def __setitem__(self, key, value): … … 223 227 224 228 def copy(self): 225 " Returns a mutable copy of this object."229 """Returns a mutable copy of this object.""" 226 230 return self.__deepcopy__({}) 227 231 … … 244 248 245 249 class HttpResponse(object): 246 " A basic HTTP response, with content and dictionary-accessed headers"250 """A basic HTTP response, with content and dictionary-accessed headers.""" 247 251 248 252 status_code = 200 … … 273 277 274 278 def __str__(self): 275 " Full HTTP message, including headers"279 """Full HTTP message, including headers.""" 276 280 return '\n'.join(['%s: %s' % (key, value) 277 281 for key, value in self._headers.values()]) \ … … 279 283 280 284 def _convert_to_ascii(self, *values): 281 " Convert all values to ascii strings"285 """Converts all values to ascii strings.""" 282 286 for value in values: 283 287 if isinstance(value, unicode): … … 304 308 305 309 def has_header(self, header): 306 " Case-insensitive check for a header"310 """Case-insensitive check for a header.""" 307 311 return self._headers.has_key(header.lower()) 308 312 … … 315 319 return self._headers.get(header.lower(), (None, alternate))[1] 316 320 317 def set_cookie(self, key, value='', max_age=None, expires=None, path='/', domain=None, secure=False): 321 def set_cookie(self, key, value='', max_age=None, expires=None, path='/', 322 domain=None, secure=False): 318 323 self.cookies[key] = value 319 324 if max_age is not None: … … 330 335 def delete_cookie(self, key, path='/', domain=None): 331 336 self.set_cookie(key, max_age=0, path=path, domain=domain, 332 expires='Thu, 01-Jan-1970 00:00:00 GMT')337 expires='Thu, 01-Jan-1970 00:00:00 GMT') 333 338 334 339 def _get_content(self): … … 361 366 def write(self, content): 362 367 if not self._is_string: 363 raise Exception , "This %s instance is not writable" % self.__class__368 raise Exception("This %s instance is not writable" % self.__class__) 364 369 self._container.append(content) 365 370 … … 369 374 def tell(self): 370 375 if not self._is_string: 371 raise Exception , "This %s instance cannot tell its position" % self.__class__376 raise Exception("This %s instance cannot tell its position" % self.__class__) 372 377 return sum([len(chunk) for chunk in self._container]) 373 378 … … 426 431 def str_to_unicode(s, encoding): 427 432 """ 428 Convert basestring objects to unicode, using the given encoding. Illegaly433 Converts basestring objects to unicode, using the given encoding. Illegally 429 434 encoded input characters are replaced with Unicode "unknown" codepoint 430 435 (\ufffd). … … 436 441 else: 437 442 return s 438
