8000 [soc2009/http-wsgi-improvements] Clean up charset-related code in Htt… · alex-python/django@c2d80a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit c2d80a5

Browse files
committed
[soc2009/http-wsgi-improvements] Clean up charset-related code in HttpResponse. Refs django#10190.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/http-wsgi-improvements@11448 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent d76879d commit c2d80a5

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

django/http/__init__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,9 @@ class HttpResponse(object):
277277
def __init__(self, content='', mimetype=None, status=None,
278278
content_type=None, request=None):
279279
from django.conf import settings
280-
accept_charset = None
281-
self._charset = settings.DEFAULT_CHARSET
282280
if mimetype:
283281
content_type = mimetype # Mimetype arg is an alias for content-type
284-
if request:
285-
accept_charset = request.META.get("ACCEPT_CHARSET")
286-
if accept_charset or content_type:
287-
encoding = get_response_encoding(content_type, accept_charset)
288-
(self._charset, self._codec) = encoding
282+
self.determine_charset(content_type, request)
289283
if not content_type:
290284
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
291285
self._charset)
@@ -294,8 +288,7 @@ def __init__(self, content='', mimetype=None, status=None,
294288
if hasattr(content, 'close'):
295289
content.close()
296290
self.cookies = SimpleCookie()
297-
if status:
298-
self.status_code = status
291+
self.status_code = status
299292
# _headers is a mapping of the lower-case name to the original case of
300293
# the header (required for working with legacy systems) and the header
301294
# value.
@@ -306,6 +299,15 @@ def __str__(self):
306299
return '\n'.join(['%s: %s' % (k, v) for k, v in self._headers.values()]) \
307300
+ "\n\n" + self.content
308301

302+
def determine_charset(self, content_type, request):
303+
self._charset = settings.DEFAULT_CHARSET
304+
accept_charset = None
305+
if request:
306+
accept_charset = request.META.get("ACCEPT_CHARSET")
307+
if accept_charset or content_type:
308+
encoding = get_response_encoding(content_type, accept_charset)
309+
(self._charset, self._codec) = encoding
310+
309311
def _convert_to_ascii(self, *values):
310312
"""Converts all values to ascii strings."""
311313
for value in values:
@@ -390,7 +392,8 @@ def _get_status_code(self):
390392
return self._status_code
391393

392394
def _set_status_code(self, value):
393-
self._status_code = value
395+
if value:
396+
self._status_code = value
394397

395398
status_code = property(_get_status_code, _set_status_code)
396399

0 commit comments

Comments
 (0)
0