Projet

Général

Profil

0001-sql-get-varchar-text-values-as-unicode-15802.patch

Frédéric Péters, 26 juin 2017 10:26

Télécharger (5,51 ko)

Voir les différences:

Subject: [PATCH] sql: get varchar/text values as unicode (#15802)

This matches what's being done in Django and will help integrating
w.c.s. with Django applications.
 tests/test_sql.py |  3 +++
 wcs/sql.py        | 30 ++++++++++++++++++++++--------
 2 files changed, 25 insertions(+), 8 deletions(-)
tests/test_sql.py
156 156
@postgresql
157 157
def test_sql_field_string():
158 158
    check_sql_field('0', 'hello world')
159
    check_sql_field('0', 'élo world')
159 160

  
160 161
@postgresql
161 162
def test_sql_field_email():
......
164 165
@postgresql
165 166
def test_sql_field_text():
166 167
    check_sql_field('2', 'long text')
168
    check_sql_field('2', 'long tèxt')
167 169

  
168 170
@postgresql
169 171
def test_sql_field_bool():
......
182 184
def test_sql_field_items():
183 185
    check_sql_field('6', ['apricot'])
184 186
    check_sql_field('6', ['apricot', 'pear'])
187
    check_sql_field('6', ['pomme', 'poire', 'pêche'])
185 188

  
186 189
@postgresql
187 190
def test_sql_geoloc():
wcs/sql.py
15 15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
import psycopg2
18
import psycopg2.extensions
18 19
import datetime
19 20
import time
20 21
import re
......
30 31
import wcs.tracking_code
31 32
import wcs.users
32 33

  
34
# enable psycogp2 unicode mode, this will fetch postgresql varchar/text columns
35
# as unicode objects
36
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
37
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
38

  
33 39
SQL_TYPE_MAPPING = {
34 40
    'title': None,
35 41
    'subtitle': None,
......
234 240
        return (where_clauses, parameters, None)
235 241

  
236 242

  
243
def str_encode(value):
244
    if isinstance(value, list):
245
        return [str_encode(x) for x in value]
246
    if not isinstance(value, unicode):
247
        return value
248
    return value.encode(get_publisher().site_charset)
249

  
237 250

  
238 251
def get_connection(new=False):
239 252
    if new:
......
999 1012
            if sql_type is None:
1000 1013
                continue
1001 1014
            value = row[i]
1002
            if value:
1015
            if value is not None:
1016
                value = str_encode(value)
1003 1017
                if field.key == 'ranked-items':
1004 1018
                    d = {}
1005 1019
                    for data, rank in value:
......
1020 1034
            obdata[field.id] = value
1021 1035
            i += 1
1022 1036
            if field.store_display_value:
1023
                value = row[i]
1037
                value = str_encode(row[i])
1024 1038
                obdata['%s_display' % field.id] = value
1025 1039
                i += 1
1026 1040
            if field.store_structured_value:
......
1124 1138
    @classmethod
1125 1139
    def _row2evo(cls, row):
1126 1140
        o = wcs.formdata.Evolution()
1127
        o._sql_id, o.who, o.status, o.time, o.comment = tuple(row[:5])
1141
        o._sql_id, o.who, o.status, o.time, o.comment = [str_encode(x) for x in tuple(row[:5])]
1128 1142
        if o.time:
1129 1143
            o.time = o.time.timetuple()
1130 1144
        if row[5]:
......
1319 1333
        o = cls()
1320 1334
        for static_field, value in zip(cls._table_static_fields,
1321 1335
                                       tuple(row[:len(cls._table_static_fields)])):
1322
            setattr(o, static_field[0], value)
1336
            setattr(o, static_field[0], str_encode(value))
1323 1337
        if o.receipt_time:
1324 1338
            o.receipt_time = o.receipt_time.timetuple()
1325 1339
        if o.workflow_data:
......
1559 1573
        o = cls()
1560 1574
        (o.id, o.name, o.email, o.roles, o.is_admin, o.anonymous,
1561 1575
         o.name_identifiers, o.verified_fields, o.lasso_dump,
1562
         o.last_seen, ascii_name) = tuple(row[:11])
1576
         o.last_seen, ascii_name) = [str_encode(x) for x in tuple(row[:11])]
1563 1577
        if o.last_seen:
1564 1578
            o.last_seen = time.mktime(o.last_seen.timetuple())
1565 1579
        if o.roles:
......
1688 1702
                    sql_dict['id'] = self.get_new_id()
1689 1703
                else:
1690 1704
                    break
1691
            self.id = cur.fetchone()[0]
1705
            self.id = str_encode(cur.fetchone()[0])
1692 1706
        else:
1693 1707
            column_names = sql_dict.keys()
1694 1708
            sql_dict['id'] = self.id
......
1705 1719
    @classmethod
1706 1720
    def _row2ob(cls, row):
1707 1721
        o = cls()
1708
        (o.id, o.formdef_id, o.formdata_id) = tuple(row[:3])
1722
        (o.id, o.formdef_id, o.formdata_id) = [str_encode(x) for x in tuple(row[:3])]
1709 1723
        return o
1710 1724

  
1711 1725
    @classmethod
......
1756 1770
        o = formdef.data_class()()
1757 1771
        for static_field, value in zip(cls._table_static_fields,
1758 1772
                                       tuple(row[:len(cls._table_static_fields)])):
1759
            setattr(o, static_field[0], value)
1773
            setattr(o, static_field[0], str_encode(value))
1760 1774
        # [CRITICALITY_2] transform criticality_level back to the expected
1761 1775
        # range (see [CRITICALITY_1])
1762 1776
        levels = len(formdef.workflow.criticality_levels or [0])
1763
-