Projet

Général

Profil

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

Frédéric Péters, 23 mai 2017 18:19

Télécharger (4,66 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        | 24 +++++++++++++++++++-----
 2 files changed, 22 insertions(+), 5 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:
......
986 999
                continue
987 1000
            value = row[i]
988 1001
            if value:
1002
                value = str_encode(value)
989 1003
                if field.key == 'ranked-items':
990 1004
                    d = {}
991 1005
                    for data, rank in value:
......
1006 1020
            obdata[field.id] = value
1007 1021
            i += 1
1008 1022
            if field.store_display_value:
1009
                value = row[i]
1023
                value = str_encode(row[i])
1010 1024
                obdata['%s_display' % field.id] = value
1011 1025
                i += 1
1012 1026
            if field.store_structured_value:
......
1305 1319
        o = cls()
1306 1320
        for static_field, value in zip(cls._table_static_fields,
1307 1321
                                       tuple(row[:len(cls._table_static_fields)])):
1308
            setattr(o, static_field[0], value)
1322
            setattr(o, static_field[0], str_encode(value))
1309 1323
        if o.receipt_time:
1310 1324
            o.receipt_time = o.receipt_time.timetuple()
1311 1325
        if o.workflow_data:
......
1545 1559
        o = cls()
1546 1560
        (o.id, o.name, o.email, o.roles, o.is_admin, o.anonymous,
1547 1561
         o.name_identifiers, o.verified_fields, o.lasso_dump,
1548
         o.last_seen, ascii_name) = tuple(row[:11])
1562
         o.last_seen, ascii_name) = [str_encode(x) for x in tuple(row[:11])]
1549 1563
        if o.last_seen:
1550 1564
            o.last_seen = time.mktime(o.last_seen.timetuple())
1551 1565
        if o.roles:
......
1691 1705
    @classmethod
1692 1706
    def _row2ob(cls, row):
1693 1707
        o = cls()
1694
        (o.id, o.formdef_id, o.formdata_id) = tuple(row[:3])
1708
        (o.id, o.formdef_id, o.formdata_id) = [str_encode(x) for x in tuple(row[:3])]
1695 1709
        return o
1696 1710

  
1697 1711
    @classmethod
......
1742 1756
        o = formdef.data_class()()
1743 1757
        for static_field, value in zip(cls._table_static_fields,
1744 1758
                                       tuple(row[:len(cls._table_static_fields)])):
1745
            setattr(o, static_field[0], value)
1759
            setattr(o, static_field[0], str_encode(value))
1746 1760
        # [CRITICALITY_2] transform criticality_level back to the expected
1747 1761
        # range (see [CRITICALITY_1])
1748 1762
        levels = len(formdef.workflow.criticality_levels or [0])
1749
-