Code

Ticket #333: 333.diff

File 333.diff, 7.4 KB (added by Maniac <Maniac@…>, 9 years ago)

splitting DEFAULT_MIME_TYPE into DEFAULT_CONTENT_TYPE and DEFAULT_CHARSET

Line 
1Index: django/utils/httpwrappers.py
2===================================================================
3--- django/utils/httpwrappers.py        (revision 775)
4+++ django/utils/httpwrappers.py        (working copy)
5@@ -139,8 +139,8 @@
6     "A basic HTTP response, with content and dictionary-accessed headers"
7     def __init__(self, content='', mimetype=None):
8         if not mimetype:
9-            from django.conf.settings import DEFAULT_MIME_TYPE
10-            mimetype = DEFAULT_MIME_TYPE
11+            from django.conf.settings import DEFAULT_CONTENT_TYPE, DEFAULT_CHARSET
12+            mimetype = DEFAULT_CONTENT_TYPE + '; charset=' + DEFAULT_CHARSET
13         self.content = content
14         self.headers = {'Content-Type':mimetype}
15         self.cookies = SimpleCookie()
16Index: django/conf/global_settings.py
17===================================================================
18--- django/conf/global_settings.py      (revision 775)
19+++ django/conf/global_settings.py      (working copy)
20@@ -32,9 +32,11 @@
21 # notifications and other various e-mails.
22 MANAGERS = ADMINS
23 
24-# Default MIME type to use for all HttpResponse objects, if a MIME type
25-# isn't manually specified. This is directly used as the Content-Type header.
26-DEFAULT_MIME_TYPE = 'text/html; charset=utf-8'
27+# Default content type and charset to use for all HttpResponse objects, if a MIME type
28+# isn't manually specified. These are used to construct the Content-Type header
29+# in the form DEFAULT_CONTENT_TYPE + '; charset=' + DEFAULT_CHARSET
30+DEFAULT_CONTENT_TYPE = 'text/html'
31+DEFAULT_CHARSET = 'utf-8'
32 
33 # E-mail address that error messages come from.
34 SERVER_EMAIL = 'root@localhost'
35Index: django/core/formfields.py
36===================================================================
37--- django/core/formfields.py   (revision 775)
38+++ django/core/formfields.py   (working copy)
39@@ -1,6 +1,7 @@
40 from django.core import validators
41 from django.core.exceptions import PermissionDenied
42 from django.utils.html import escape
43+from django.conf import settings
44 
45 FORM_FIELD_ID_PREFIX = 'id_'
46 
47@@ -221,7 +222,7 @@
48         self.validator_list = [self.isValidLength, self.hasNoNewlines] + validator_list
49 
50     def isValidLength(self, data, form):
51-        if data and self.maxlength and len(data) > self.maxlength:
52+        if data and self.maxlength and len(data.decode(settings.DEFAULT_CHARSET)) > self.maxlength:
53             raise validators.ValidationError, "Ensure your text is less than %s characters." % self.maxlength
54 
55     def hasNoNewlines(self, data, form):
56@@ -235,7 +236,7 @@
57         if self.maxlength:
58             maxlength = 'maxlength="%s" ' % self.maxlength
59         if isinstance(data, unicode):
60-            data = data.encode('utf-8')
61+            data = data.encode(settings.DEFAULT_CHARSET)
62         return '<input type="text" id="%s" class="v%s%s" name="%s" size="%s" value="%s" %s/>' % \
63             (FORM_FIELD_ID_PREFIX + self.field_name, self.__class__.__name__, self.is_required and ' required' or '',
64             self.field_name, self.length, escape(data), maxlength)
65@@ -264,7 +265,7 @@
66         if data is None:
67             data = ''
68         if isinstance(data, unicode):
69-            data = data.encode('utf-8')
70+            data = data.encode(settings.DEFAULT_CHARSET)
71         return '<textarea id="%s" class="v%s%s" name="%s" rows="%s" cols="%s">%s</textarea>' % \
72             (FORM_FIELD_ID_PREFIX + self.field_name, self.__class__.__name__, self.is_required and ' required' or '',
73             self.field_name, self.rows, self.cols, escape(data))
74Index: django/core/handlers/wsgi.py
75===================================================================
76--- django/core/handlers/wsgi.py        (revision 775)
77+++ django/core/handlers/wsgi.py        (working copy)
78@@ -167,6 +167,6 @@
79         response_headers = response.headers.items()
80         for c in response.cookies.values():
81             response_headers.append(('Set-Cookie', c.output(header='')))
82-        output = [response.get_content_as_string('utf-8')]
83+        output = [response.get_content_as_string(settings.DEFAULT_CHARSET)]
84         start_response(status, response_headers)
85         return output
86Index: django/core/handlers/modpython.py
87===================================================================
88--- django/core/handlers/modpython.py   (revision 775)
89+++ django/core/handlers/modpython.py   (working copy)
90@@ -157,7 +157,8 @@
91     for c in http_response.cookies.values():
92         mod_python_req.headers_out.add('Set-Cookie', c.output(header=''))
93     mod_python_req.status = http_response.status_code
94-    mod_python_req.write(http_response.get_content_as_string('utf-8'))
95+    from django.conf import settings
96+    mod_python_req.write(http_response.get_content_as_string(settings.DEFAULT_CHARSET))
97 
98 def handler(req):
99     # mod_python hooks into this function.
100Index: django/core/template.py
101===================================================================
102--- django/core/template.py     (revision 775)
103+++ django/core/template.py     (working copy)
104@@ -55,6 +55,7 @@
105 '\n<html>\n\n</html>\n'
106 """
107 import re
108+from django.conf import settings
109 
110 __all__ = ('Template','Context','compile_string')
111 
112@@ -474,7 +475,7 @@
113         if not isinstance(output, basestring):
114             output = str(output)
115         elif isinstance(output, unicode):
116-            output = output.encode('utf-8')
117+            output = output.encode(settings.DEFAULT_CHARSET)
118         return output
119 
120 def register_tag(token_command, callback_function):
121Index: django/middleware/common.py
122===================================================================
123--- django/middleware/common.py (revision 775)
124+++ django/middleware/common.py (working copy)
125@@ -78,7 +78,7 @@
126 
127         # Use ETags, if requested
128         if settings.USE_ETAGS:
129-            etag = md5.new(response.get_content_as_string('utf-8')).hexdigest()
130+            etag = md5.new(response.get_content_as_string(settings.DEFAULT_CHARSET)).hexdigest()
131             if request.META.get('HTTP_IF_NONE_MATCH') == etag:
132                 response = httpwrappers.HttpResponseNotModified()
133             else:
134Index: django/middleware/cache.py
135===================================================================
136--- django/middleware/cache.py  (revision 775)
137+++ django/middleware/cache.py  (working copy)
138@@ -76,7 +76,7 @@
139         Sets the cache, if needed.
140         """
141         if request._cache_middleware_set_cache:
142-            content = response.get_content_as_string('utf-8')
143+            content = response.get_content_as_string(settings.DEFAULT_CHARSET)
144             if request._cache_middleware_accepts_gzip:
145                 content = compress_string(content)
146                 response.content = content
147Index: django/views/decorators/cache.py
148===================================================================
149--- django/views/decorators/cache.py    (revision 775)
150+++ django/views/decorators/cache.py    (working copy)
151@@ -1,6 +1,7 @@
152 from django.core.cache import cache
153 from django.utils.httpwrappers import HttpResponseNotModified
154 from django.utils.text import compress_string
155+from django.conf import settings
156 import datetime, md5
157 
158 def cache_page(view_func, cache_timeout, key_prefix=''):
159@@ -25,7 +26,7 @@
160         response = cache.get(cache_key, None)
161         if response is None:
162             response = view_func(request, *args, **kwargs)
163-            content = response.get_content_as_string('utf-8')
164+            content = response.get_content_as_string(settings.DEFAULT_CHARSET)
165             if accepts_gzip:
166                 content = compress_string(content)
167                 response.content = content