From 7c5294fe1245d927cb30c556beb21765cf3b9e46 Mon Sep 17 00:00:00 2001 From: Mark Sapiro Date: Mon, 20 May 2024 19:22:44 -0400 Subject: [PATCH] Add the web url replacements to the defaults in utilities.string.expand. --- src/mailman/docs/NEWS.rst | 1 + src/mailman/handlers/decorate.py | 4 -- src/mailman/utilities/string.py | 5 ++ .../tests/{test_urls.py => test_string.py} | 74 +++++++++++++++------- src/mailman/utilities/urls.py | 37 ----------- 5 files changed, 57 insertions(+), 64 deletions(-) rename src/mailman/utilities/tests/{test_urls.py => test_string.py} (54%) delete mode 100644 src/mailman/utilities/urls.py diff --git a/src/mailman/docs/NEWS.rst b/src/mailman/docs/NEWS.rst index 88e2c8aff..9bfd0dd25 100644 --- a/src/mailman/docs/NEWS.rst +++ b/src/mailman/docs/NEWS.rst @@ -34,6 +34,7 @@ Bugs fixed a race condition. (Closes #1151) * The ``nttplib`` slated to be removed in Python 3.13. Getting from PyPI. (Closes #1144) +* The web URL replacements now work in all templates. (Closes #1142) New Features ------------ diff --git a/src/mailman/handlers/decorate.py b/src/mailman/handlers/decorate.py index a72ce22a0..4227db7d8 100644 --- a/src/mailman/handlers/decorate.py +++ b/src/mailman/handlers/decorate.py @@ -29,7 +29,6 @@ from mailman.interfaces.handler import IHandler from mailman.interfaces.mailinglist import IListArchiverSet from mailman.interfaces.template import ITemplateLoader from mailman.utilities.string import expand -from mailman.utilities.urls import web_urls from public import public from zope.component import getUtility from zope.interface import implementer @@ -70,9 +69,6 @@ def process(mlist, msg, msgdata): # For backward compatibility. d['user_name_or_address'] = member.display_name or recipient d['user_address'] = recipient - if mlist.domain.base_url is not None: - d['base_url'] = mlist.domain.base_url - web_urls(d, mlist) # Calculate the archiver permalink substitution variables. This provides # the $_url placeholder for every enabled archiver. for archiver in IListArchiverSet(mlist).archivers: diff --git a/src/mailman/utilities/string.py b/src/mailman/utilities/string.py index 51b08b128..88d7ffebe 100644 --- a/src/mailman/utilities/string.py +++ b/src/mailman/utilities/string.py @@ -66,6 +66,11 @@ def expand(template, mlist=None, extras=None, template_class=Template): owner_email=mlist.owner_address, language=mlist.preferred_language.code, )) + if mlist.domain.base_url is not None: + substitutions['base_url'] = mlist.domain.base_url + for key in config.urlpatterns: + substitutions[f'{key}_url'] = expand( + config.urlpatterns[key], extras=substitutions) if extras is not None: substitutions.update(extras) return template_class(template).safe_substitute(substitutions) diff --git a/src/mailman/utilities/tests/test_urls.py b/src/mailman/utilities/tests/test_string.py similarity index 54% rename from src/mailman/utilities/tests/test_urls.py rename to src/mailman/utilities/tests/test_string.py index 606a5d349..0240d44d6 100644 --- a/src/mailman/utilities/tests/test_urls.py +++ b/src/mailman/utilities/tests/test_string.py @@ -1,4 +1,4 @@ -# Copyright (C) 2023 by the Free Software Foundation, Inc. +# Copyright (C) 2015-2023 by the Free Software Foundation, Inc. # # This file is part of GNU Mailman. # @@ -15,24 +15,50 @@ # You should have received a copy of the GNU General Public License along with # GNU Mailman. If not, see . -"""Test web url utitlities.""" +"""Test the string utilities.""" import unittest from mailman.testing.layers import ConfigLayer -from mailman.utilities.urls import web_urls +from mailman.utilities import string -class TestWebUrls(unittest.TestCase): +class TestString(unittest.TestCase): + def test_oneline_bogus_charset(self): + self.assertEqual(string.oneline('foo', 'bogus'), 'foo') + + def test_wrap_blank_paragraph(self): + self.assertEqual(string.wrap('\n\n'), '\n\n') + + +class TestWebSubstitutions(unittest.TestCase): + """Test that all the web substitutions are available.""" + layer = ConfigLayer # Create a mock MailingList object + class MockMailingList: def __init__(self, domain, list_id, mail_host): + + class preferred_language: + + def __init__(self, code): + self.code = code + self.domain = domain self.list_id = list_id self.mail_host = mail_host + self.fqdn_listname = list_id.replace('.', '@', 1) + self.display_name = 'MyList' + self.list_name = 'mylist' + self.short_listname = 'mylist' + self.description = 'description' + self.info = 'info' + self.request_address = list_id.replace('.', '-request@', 1) + self.owner_address = list_id.replace('.', '-owner@', 1) + self.preferred_language = preferred_language('en') # Create a mock domain object class MockDomain: @@ -46,29 +72,31 @@ class TestWebUrls(unittest.TestCase): mail_host = "lists.example.com" held_message_url = "https://example.com/mailman3/lists/mylist.lists.example.com/held_messages" # noqa: E501 mailing_list_url = "https://example.com/mailman3/lists/mylist.lists.example.com" # noqa: E501 - domain_url = "https://example.com/mailman3/domains/lists.example.com" # noqa: E501 + domain_url = "https://example.com/mailman3/domains/lists.example.com" pending_subscriptions_url = "https://example.com/mailman3/lists/mylist.lists.example.com/subscription_requests" # noqa: E501 pending_unsubscriptions_url = "https://example.com/mailman3/lists/mylist.lists.example.com/unsubscription_requests" # noqa: E501 - domain = self.MockDomain(None) - mlist = self.MockMailingList(domain, list_id, mail_host) - - # When domain doesn't have a base_url set. - d = {} - web_urls(d, mlist) - self.assertEqual(d, {}) - # Expected URLs are generated. domain = self.MockDomain(base_url) mlist = self.MockMailingList(domain, list_id, mail_host) - # Call the web_urls function - d = {} - web_urls(d, mlist) + # Create a template with all the substitutions. + template = """\ +$base_url +$list_id +$mail_host +$held_message_url +$mailing_list_url +$domain_url +$pending_subscriptions_url +$pending_unsubscriptions_url +""" + template = string.expand(template, mlist) # Assert the expected values - self.assertEqual(d['held_message_url'], held_message_url) - self.assertEqual(d['mailinglist_url'], mailing_list_url) - self.assertEqual(d['domain_url'], domain_url) - self.assertEqual( - d['pending_subscriptions_url'], pending_subscriptions_url) - self.assertEqual( - d['pending_unsubscriptions_url'], pending_unsubscriptions_url) + self.assertIn(base_url, template) + self.assertIn(list_id, template) + self.assertIn(mail_host, template) + self.assertIn(held_message_url, template) + self.assertIn(mailing_list_url, template) + self.assertIn(domain_url, template) + self.assertIn(pending_subscriptions_url, template) + self.assertIn(pending_unsubscriptions_url, template) diff --git a/src/mailman/utilities/urls.py b/src/mailman/utilities/urls.py deleted file mode 100644 index da8559fb4..000000000 --- a/src/mailman/utilities/urls.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (C) 2023 by the Free Software Foundation, Inc. -# -# This file is part of GNU Mailman. -# -# GNU Mailman is free software: you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free -# Software Foundation, either version 3 of the License, or (at your option) -# any later version. -# -# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -# more details. -# -# You should have received a copy of the GNU General Public License along with -# GNU Mailman. If not, see . - -"""Utilities to generate web urls for Email templates.""" - -from mailman.config import config -from mailman.utilities.string import expand - - -def web_urls(d, mlist): - """Generate web urls for the MailingList templates.""" - base_url = mlist.domain.base_url - if base_url is None: - return d - - substitutions = { - 'base_url': base_url, - 'list_id': mlist.list_id, - 'domain': mlist.mail_host, - } - - for key in config.urlpatterns: - d[f'{key}_url'] = expand(config.urlpatterns[key], extras=substitutions) -- 2.11.4.GIT