diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/mailman/utilities/importer.py | 31 | ||||
| -rw-r--r-- | src/mailman/utilities/tests/test_import.py | 17 |
2 files changed, 39 insertions, 9 deletions
diff --git a/src/mailman/utilities/importer.py b/src/mailman/utilities/importer.py index 14214438b..9ada4c75e 100644 --- a/src/mailman/utilities/importer.py +++ b/src/mailman/utilities/importer.py @@ -231,28 +231,43 @@ def import_config_pck(mlist, config_dict): "%(real_name)s@%(host_name)s": "$fqdn_listname", "%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s": "$listinfo_uri", } + # Collect defaults + defaults = {} + for oldvar, newvar in convert_to_uri.iteritems(): + default_value = getattr(mlist, newvar) + if not default_value: + continue + # Check if the value changed from the default + try: + default_text = decorate(mlist, default_value) + except (URLError, KeyError): + # Use case: importing the old a@ex.com into b@ex.com + # We can't check if it changed from the default + # -> don't import, we may do more harm than good and it's easy to + # change if needed + continue + defaults[newvar] = (default_value, default_text) for oldvar, newvar in convert_to_uri.iteritems(): if oldvar not in config_dict: continue text = config_dict[oldvar] + text = unicode(text, "utf-8", "replace") for oldph, newph in convert_placeholders.iteritems(): text = text.replace(oldph, newph) - default_value = getattr(mlist, newvar) - if not text and not default_value: - continue + default_value, default_text = defaults.get(newvar, (None, None)) + if not text and not (default_value or default_text): + continue # both are empty, leave it # Check if the value changed from the default try: - default_text = decorate(mlist, default_value) expanded_text = decorate_template(mlist, text) - except (URLError, KeyError): + except KeyError: # Use case: importing the old a@ex.com into b@ex.com # We can't check if it changed from the default # -> don't import, we may do more harm than good and it's easy to # change if needed continue - if not text and not default_text: - continue # both are empty, leave it - if expanded_text.strip() == default_text.strip(): + if expanded_text and default_text \ + and expanded_text.strip() == default_text.strip(): continue # keep the default # Write the custom value to the right file base_uri = "mailman:///$listname/$language/" diff --git a/src/mailman/utilities/tests/test_import.py b/src/mailman/utilities/tests/test_import.py index d4ae72fbf..2e6a4c0be 100644 --- a/src/mailman/utilities/tests/test_import.py +++ b/src/mailman/utilities/tests/test_import.py @@ -172,6 +172,7 @@ class TestBasicImport(unittest.TestCase): # moderator_password must not be unicode self._pckdict[b"mod_password"] = b'TESTVALUE' self._import() + self.assertFalse(isinstance(self._mlist.moderator_password, unicode)) self.assertEqual(self._mlist.moderator_password, b'TESTVALUE') def test_newsgroup_moderation(self): @@ -435,7 +436,7 @@ class TestConvertToURI(unittest.TestCase): # We can't check if it changed from the default # -> don't import, we may do more harm than good and it's easy to # change if needed - test_value = "TEST-VALUE" + test_value = b"TEST-VALUE" for oldvar, newvar in self._conf_mapping.iteritems(): self._mlist.mail_host = "example.com" self._pckdict[b"mail_host"] = b"test.example.com" @@ -446,6 +447,20 @@ class TestConvertToURI(unittest.TestCase): self.assertEqual(old_value, new_value, "Default value was not preserved for %s" % newvar) + def test_unicode(self): + for oldvar in self._conf_mapping: + self._pckdict[str(oldvar)] = b"Ol\xe1!" + try: + import_config_pck(self._mlist, self._pckdict) + except UnicodeDecodeError, e: + self.fail(e) + for oldvar, newvar in self._conf_mapping.iteritems(): + newattr = getattr(self._mlist, newvar) + text = decorate(self._mlist, newattr) + expected = u'Ol\ufffd!'.encode("utf-8") + # we get bytestrings because the text is stored in a file + self.assertEqual(text, expected) + class TestRosterImport(unittest.TestCase): |
