~abompard/mailman/import21

« back to all changes in this revision

Viewing changes to src/mailman/utilities/importer.py

  • Committer: Aurélien Bompard
  • Date: 2013-10-09 13:55:22 UTC
  • Revision ID: aurelien@bompard.org-20131009135522-prq2afhfs0e5uhy3
Deal with non-ascii bans and aliases

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
    pass
56
56
 
57
57
 
 
58
def str_to_unicode(value):
 
59
    # Convert a string to unicode when the encoding is not declared
 
60
    if isinstance(value, unicode):
 
61
        return value
 
62
    for encoding in ("ascii", "utf-8"):
 
63
        try:
 
64
            return unicode(value, encoding)
 
65
        except UnicodeDecodeError, e:
 
66
            continue
 
67
    # we did our best, use replace
 
68
    return unicode(value, 'ascii', 'replace')
 
69
 
 
70
 
58
71
def seconds_to_delta(value):
59
72
    return datetime.timedelta(seconds=value)
60
73
 
212
225
        # exist (see python issue 9666). Add them here.
213
226
        if hasattr(mlist, key) or key in ("preferred_language", ):
214
227
            if isinstance(value, str):
215
 
                for encoding in ("ascii", "utf-8"):
216
 
                    try:
217
 
                        value = unicode(value, encoding)
218
 
                    except UnicodeDecodeError, e:
219
 
                        continue
220
 
                    else:
221
 
                        break
222
 
                if isinstance(value, str): # we did our best
223
 
                    value = unicode(value, 'ascii', 'replace')
 
228
                value = str_to_unicode(value)
224
229
            # Some types require conversion.
225
230
            converter = TYPES.get(key)
226
231
            if converter is not None:
240
245
        mlist.archive_policy = ArchivePolicy.never
241
246
    # Handle ban list
242
247
    for addr in config_dict.get('ban_list', []):
243
 
        IBanManager(mlist).ban(unicode(addr))
 
248
        IBanManager(mlist).ban(str_to_unicode(addr))
244
249
    # Handle acceptable aliases
245
250
    for addr in config_dict.get('acceptable_aliases', '').splitlines():
246
251
        addr = addr.strip()
247
252
        if not addr:
248
253
            continue
249
 
        IAcceptableAliasSet(mlist).add(unicode(addr))
 
254
        IAcceptableAliasSet(mlist).add(str_to_unicode(addr))
250
255
    # Handle conversion to URIs
251
256
    convert_to_uri = {
252
257
        "welcome_msg": "welcome_message_uri",
342
347
    """
343
348
    usermanager = getUtility(IUserManager)
344
349
    for email in members:
345
 
        email = unicode(email)
 
350
        email = str_to_unicode(email)
346
351
        roster = mlist.get_roster(role)
347
352
        if roster.get_member(email) is not None:
348
353
            print("%s is already imported with role %s" % (email, role),
357
362
                original_email = merged_members[email]
358
363
            else:
359
364
                original_email = email
360
 
            user = usermanager.create_user(unicode(original_email))
 
365
            user = usermanager.create_user(str_to_unicode(original_email))
361
366
        address = usermanager.get_address(email)
362
367
        address.verified_on = datetime.datetime.now()
363
368
        mlist.subscribe(address, role)
380
385
        # if the user already exists, display_name and password will be
381
386
        # overwritten
382
387
        if email in config_dict.get("usernames", {}):
383
 
            address.display_name = unicode(config_dict["usernames"][email])
384
 
            user.display_name    = unicode(config_dict["usernames"][email])
 
388
            address.display_name = \
 
389
                    str_to_unicode(config_dict["usernames"][email])
 
390
            user.display_name    = \
 
391
                    str_to_unicode(config_dict["usernames"][email])
385
392
        if email in config_dict.get("passwords", {}):
386
393
            user.password = config.password_context.encrypt(
387
394
                                    config_dict["passwords"][email])