Projet

Général

Profil

0001-general-remove-legacy-switch-to-utf-8-code-6107.patch

Frédéric Péters, 11 novembre 2019 20:34

Télécharger (6,85 ko)

Voir les différences:

Subject: [PATCH] general: remove legacy "switch to utf-8" code (#6107)

 wcs/admin/settings.py   | 92 +----------------------------------------
 wcs/publisher.py        | 12 ------
 wcs/qommon/publisher.py |  2 +-
 3 files changed, 2 insertions(+), 104 deletions(-)
wcs/admin/settings.py
399 399
    _q_exports = ['', 'themes', 'users',
400 400
            'template', 'misc', 'emails', 'debug_options', 'language',
401 401
            ('import', 'p_import'), 'export', 'identification', 'sitename',
402
            'sms', 'certificates', 'texts', 'utf8switch', 'install_theme',
402
            'sms', 'certificates', 'texts', 'install_theme',
403 403
            'session', 'download_theme', 'smstest', 'postgresql',
404 404
            ('admin-permissions', 'admin_permissions'), 'geolocation',
405 405
            'theme_preview', 'filetypes',
......
498 498
        r += htmltext('<h2>%s</h2>') % _('Customisation')
499 499
        r += htmltext('<div>')
500 500

  
501
        if not get_cfg('misc', {}).get('charset'):
502
            r += htmltext('<div class="infonotice">')
503
            r += htmltext('<p>')
504
            r += _('This site is still using ISO-8859-15 as its character set; '\
505
                   'it is advised to update to UTF-8.')
506
            r += ' '
507
            r += htmltext('<a href="utf8switch">%s</a>') % _('Switch to UTF-8 encoding')
508
            r += htmltext('</p>')
509
            r += htmltext('</div>')
510

  
511 501
        r += htmltext('<dl>')
512 502
        if enabled('sitename'):
513 503
            r += htmltext('<dt><a href="sitename">%s</a></dt> <dd>%s</dd>') % (
......
1176 1166

  
1177 1167
        return r.getvalue()
1178 1168

  
1179

  
1180
    def utf8switch(self):
1181
        def toutf8(x):
1182
            if x is None:
1183
                return None
1184
            return unicode(x, 'iso-8859-1').encode('utf-8')
1185

  
1186
        all_elems = []
1187

  
1188
        for formdef in FormDef.select():
1189
            formdef.name = toutf8(formdef.name)
1190
            for field in formdef.fields:
1191
                for attr in ('label', 'hint', 'condition', 'items'):
1192
                    if hasattr(field, attr) and type(getattr(field, attr)) is str:
1193
                        setattr(field, attr, toutf8(getattr(field, attr)))
1194
                    elif hasattr(field, attr) and type(getattr(field, attr)) is list:
1195
                        setattr(field, attr, [toutf8(x) for x in getattr(field, attr)])
1196
                if hasattr(field, 'prefill') and getattr(field, 'prefill'):
1197
                    if field.prefill.get('type') == 'string':
1198
                        field.prefill['value'] = toutf8(field.prefill.get('value'))
1199

  
1200
            form_data_class = formdef.data_class()
1201
            for form in form_data_class.select():
1202
                for k, v in form.data.items():
1203
                    if type(v) is str:
1204
                        form.data[k] = toutf8(v)
1205
                if form.evolution:
1206
                    for evo in form.evolution:
1207
                        if evo.comment:
1208
                            evo.comment = toutf8(evo.comment)
1209
                all_elems.append(form)
1210
            all_elems.append(formdef)
1211

  
1212
        for workflow in Workflow.select():
1213
            workflow.name = toutf8(workflow.name)
1214
            if workflow.possible_status:
1215
                for status in workflow.possible_status:
1216
                    status.name = toutf8(status.name)
1217
                    if not status.items:
1218
                        continue
1219
                    for item in status.items:
1220
                        for attr in ('label', 'subject', 'body', 'message'):
1221
                            if hasattr(item, attr) and type(getattr(item, attr)) is str:
1222
                                setattr(item, attr, toutf8(getattr(item, attr)))
1223
            all_elems.append(workflow)
1224

  
1225
        from ..categories import Category
1226
        for category in Category.select():
1227
            category.name = toutf8(category.name)
1228
            category.description = toutf8(category.description)
1229
            all_elems.append(category)
1230

  
1231
        for role in Role.select():
1232
            role.name = toutf8(role.name)
1233
            role.details = toutf8(role.details)
1234
            all_elems.append(role)
1235

  
1236
        for user in get_publisher().user_class.select():
1237
            user.name = toutf8(user.name)
1238
            if hasattr(user, 'formdata') and user.formdata:
1239
                for k, v in user.formdata.items():
1240
                    if type(v) is str:
1241
                        user.formdata[k] = toutf8(v)
1242
            all_elems.append(user)
1243

  
1244
        for cfg_section in get_publisher().cfg.values():
1245
            for k, v in cfg_section.items():
1246
                if type(v) is str:
1247
                    cfg_section[k] = toutf8(v)
1248

  
1249
        for elem in all_elems:
1250
            elem.store()
1251

  
1252
        misc_cfg = get_cfg('misc', {})
1253
        misc_cfg['charset'] = 'utf-8'
1254
        get_publisher().cfg['misc'] = misc_cfg
1255
        get_publisher().write_cfg()
1256

  
1257
        return redirect('.')
1258

  
1259 1169
    def postgresql(self):
1260 1170
        postgresql_cfg = get_cfg('postgresql', {})
1261 1171
        if not get_publisher().has_site_option('postgresql') and not postgresql_cfg:
wcs/publisher.py
125 125

  
126 126
    def set_config(self, request=None, skip_sql=False):
127 127
        QommonPublisher.set_config(self, request = request)
128
        filename = os.path.join(self.app_dir, 'config.pck')
129
        if os.path.exists(filename):
130
            # a configuration file exists, it may not have a charset
131
            # specified so we default to iso-8859-15 as it was the only
132
            # possible value
133
            self.site_charset = self.cfg.get('misc', {}).get('charset', 'iso-8859-15')
134
        else:
135
            # new site, set the charset, and write it down for further
136
            # requests
137
            self.site_charset = 'utf-8'
138
            self.cfg['misc'] = {'charset': 'utf-8'}
139
            self.write_cfg()
140 128
        if request:
141 129
            request.response.charset = self.site_charset
142 130

  
wcs/qommon/publisher.py
87 87
    default_theme = 'default'
88 88

  
89 89
    site_options = None
90
    site_charset = 'iso-8859-1'
90
    site_charset = 'utf-8'
91 91
    default_configuration_path = None
92 92
    auto_create_appdir = True
93 93
    missing_appdir_redirect = None
94
-