Code

Ticket #952: corrected.diff

File corrected.diff, 5.8 KB (added by me@…, 8 years ago)

Why I never get it right the first time I wonder

Line 
1Index: django/conf/project_template/settings.py
2===================================================================
3--- django/conf/project_template/settings.py    (revision 1510)
4+++ django/conf/project_template/settings.py    (working copy)
5@@ -15,6 +15,7 @@
6 DATABASE_PASSWORD = ''         # Not used with sqlite3.
7 DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
8 DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
9+DATABASE_CLIENT_CHARSET = ''   # Set to empty string to guess from DEFAULT_CHARSET. Not used with sqlite3.
10 
11 # Local time zone for this installation. All choices can be found here:
12 # http://www.postgresql.org/docs/current/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
13Index: django/conf/global_settings.py
14===================================================================
15--- django/conf/global_settings.py      (revision 1510)
16+++ django/conf/global_settings.py      (working copy)
17@@ -78,6 +78,7 @@
18 DATABASE_PASSWORD = ''         # Not used with sqlite3.
19 DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
20 DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
21+DATABASE_CLIENT_CHARSET = ''   # Set to empty string to guess from DEFAULT_CHARSET. Not used with sqlite3.
22 
23 # Host for sending e-mail.
24 EMAIL_HOST = 'localhost'
25Index: django/core/db/backends/postgresql.py
26===================================================================
27--- django/core/db/backends/postgresql.py       (revision 1510)
28+++ django/core/db/backends/postgresql.py       (working copy)
29@@ -13,9 +13,18 @@
30     def __init__(self):
31         self.connection = None
32         self.queries = []
33+        self.charset_mappings = {
34+            'utf-8':'UNICODE',
35+            'windows-1251':'WINDOWS1251',
36+            'windows-1250':'WINDOWS1250',
37+            'shift-jis':'SJIS',
38+            'koi8-r':'KOI8',
39+            'koi8-u':'KOI8',
40+        }
41 
42     def cursor(self):
43-        from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG, TIME_ZONE
44+        from django.conf.settings import DEFAULT_CHARSET, DATABASE_USER, DATABASE_NAME, DATABASE_HOST, \
45+                                         DATABASE_PORT, DATABASE_PASSWORD, DATABASE_CLIENT_CHARSET, DEBUG, TIME_ZONE
46         if self.connection is None:
47             if DATABASE_NAME == '':
48                 from django.core.exceptions import ImproperlyConfigured
49@@ -33,6 +42,20 @@
50             self.connection.set_isolation_level(1) # make transactions transparent to all cursors
51         cursor = self.connection.cursor()
52         cursor.execute("SET TIME ZONE %s", [TIME_ZONE])
53+       
54+        try:
55+            charset = self.charset_mappings[DEFAULT_CHARSET.lower()]
56+        except KeyError:
57+            if (not DATABASE_CLIENT_CHARSET):
58+                from django.core.exceptions import ImproperlyConfigured
59+                raise ImproperlyConfigured, 'You need to specify DATABASE_CLIENT_CHARSET in your Django settings \
60+                    file because Django cannot determine it automatically. You should set it to the name \
61+                    that PostgreSQL uses for the character set "'+DEFAULT_CHARSET+'"'
62+            else:
63+                charset = DATABASE_CLIENT_CHARSET
64+
65+        self.connection.cursor().execute("SET CLIENT_ENCODING TO "+charset)
66+       
67         if DEBUG:
68             return base.CursorDebugWrapper(cursor, self)
69         return cursor
70Index: django/core/db/backends/mysql.py
71===================================================================
72--- django/core/db/backends/mysql.py    (revision 1510)
73+++ django/core/db/backends/mysql.py    (working copy)
74@@ -51,9 +51,23 @@
75     def __init__(self):
76         self.connection = None
77         self.queries = []
78+        self.charset_mappings = {
79+            'utf-8':'utf8',
80+            'windows-1251':'cp1251',
81+            'windows-1250':'cp1250',
82+            'shift-jis':'sjis',
83+            'utf-16':'ucs2',
84+            'koi8-r':'koi8r',
85+            'koi8-u':'koi8u',
86+        }
87 
88     def cursor(self):
89-        from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG
90+        from django.conf.settings import DEFAULT_CHARSET, DATABASE_USER, DATABASE_NAME, DATABASE_HOST, \
91+                                         DATABASE_PORT, DATABASE_PASSWORD, DATABASE_CLIENT_CHARSET, DEBUG
92+       
93+        # additionally import charset-related settings that might not be present
94+        from django.conf.settings import DEFAULT_CHARSET
95+       
96         if self.connection is None:
97             kwargs = {
98                 'user': DATABASE_USER,
99@@ -65,6 +79,22 @@
100             if DATABASE_PORT:
101                 kwargs['port'] = DATABASE_PORT
102             self.connection = Database.connect(**kwargs)
103+
104+            try:
105+                charset = self.charset_mappings[DEFAULT_CHARSET.lower()]
106+            except KeyError:
107+                if (not DATABASE_CLIENT_CHARSET):
108+                    from django.core.exceptions import ImproperlyConfigured
109+                    raise ImproperlyConfigured, 'You need to specify DATABASE_CLIENT_CHARSET in your Django settings \
110+                        file because Django cannot determine it automatically. You should set it to the name \
111+                        that MySQL uses for the character set "'+DEFAULT_CHARSET+'"'
112+                else:
113+                    charset = DATABASE_CLIENT_CHARSET
114+            try:
115+                self.connection.cursor().execute("SET NAMES "+charset)
116+            except Database.OperationalError: # this is MySQL older than 4.1
117+                pass
118+               
119         if DEBUG:
120             return base.CursorDebugWrapper(MysqlDebugWrapper(self.connection.cursor()), self)
121         return self.connection.cursor()