From 5f4f69bc6ca5ba169df30d8ae18ceced283e4537 Mon Sep 17 00:00:00 2001 From: Glandos Date: Thu, 7 Apr 2022 21:15:48 +0200 Subject: [PATCH] Convert MAIL_DEFAULT_SENDER to a string (#1007) Fixes #1005 --- Dockerfile | 2 +- docker-compose.yml | 2 +- docs/configuration.md | 7 +++---- ihatemoney/conf-templates/ihatemoney.cfg.j2 | 2 +- ihatemoney/default_settings.py | 2 +- ihatemoney/run.py | 10 ++++++++++ ihatemoney/tests/ihatemoney.cfg | 2 +- ihatemoney/tests/ihatemoney_envvar.cfg | 2 +- ihatemoney/tests/main_test.py | 2 +- ihatemoney/utils.py | 5 ++++- 10 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index cbd3a4a1..0258d751 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ ENV DEBUG="False" \ ALLOW_PUBLIC_PROJECT_CREATION="True" \ BABEL_DEFAULT_TIMEZONE="UTC" \ GREENLET_TEST_CPP="no" \ - MAIL_DEFAULT_SENDER="('Budget manager', 'admin@example.com')" \ + MAIL_DEFAULT_SENDER="Budget manager " \ MAIL_PASSWORD="" \ MAIL_PORT="25" \ MAIL_SERVER="localhost" \ diff --git a/docker-compose.yml b/docker-compose.yml index 40c7ed21..7711089d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,7 @@ services: - ALLOW_PUBLIC_PROJECT_CREATION=True - BABEL_DEFAULT_TIMEZONE=UTC - GREENLET_TEST_CPP=no - - MAIL_DEFAULT_SENDER=('Budget manager', 'admin@example.com') + - MAIL_DEFAULT_SENDER="Budget manager " - MAIL_PASSWORD= - MAIL_PORT=25 - MAIL_SERVER=localhost diff --git a/docs/configuration.md b/docs/configuration.md index 40149d6e..d0d2b3bb 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -76,11 +76,10 @@ for details. ## MAIL_DEFAULT_SENDER -A python tuple describing the name and email address to use when sending -emails. +An email address to use when sending emails. -- **Default value:** `("Budget manager", "admin@example.com")` -- **Production value:** Any tuple you want. +- **Default value:** `Budget manager ` +- **Production value:** Any valid email address. ## SHOW_ADMIN_EMAIL diff --git a/ihatemoney/conf-templates/ihatemoney.cfg.j2 b/ihatemoney/conf-templates/ihatemoney.cfg.j2 index 8538a206..e476aca2 100644 --- a/ihatemoney/conf-templates/ihatemoney.cfg.j2 +++ b/ihatemoney/conf-templates/ihatemoney.cfg.j2 @@ -19,7 +19,7 @@ SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = "{{ secret_key }}" # A python tuple describing the name and email adress of the sender of the mails. -MAIL_DEFAULT_SENDER = ("Budget manager", "admin@example.com") # CUSTOMIZE +MAIL_DEFAULT_SENDER = "Budget manager " # CUSTOMIZE # A boolean that determines whether the admin email (MAIL_DEFAULT_SENDER) is # shown in error messages. diff --git a/ihatemoney/default_settings.py b/ihatemoney/default_settings.py index 46265f5c..6c23c9fb 100644 --- a/ihatemoney/default_settings.py +++ b/ihatemoney/default_settings.py @@ -3,7 +3,7 @@ DEBUG = SQLACHEMY_ECHO = False SQLALCHEMY_DATABASE_URI = "sqlite:////tmp/ihatemoney.db" SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = "tralala" -MAIL_DEFAULT_SENDER = ("Budget manager", "admin@example.com") +MAIL_DEFAULT_SENDER = "Budget manager " SHOW_ADMIN_EMAIL = True ACTIVATE_DEMO_PROJECT = True ADMIN_PASSWORD = "" diff --git a/ihatemoney/run.py b/ihatemoney/run.py index cea6f93e..f4357afd 100644 --- a/ihatemoney/run.py +++ b/ihatemoney/run.py @@ -102,6 +102,16 @@ def validate_configuration(app): if "MAIL_DEFAULT_SENDER" not in app.config: app.config["MAIL_DEFAULT_SENDER"] = default_settings.DEFAULT_MAIL_SENDER + if type(app.config["MAIL_DEFAULT_SENDER"]) == tuple: + (name, address) = app.config["MAIL_DEFAULT_SENDER"] + app.config["MAIL_DEFAULT_SENDER"] = f"{name} <{address}>" + warnings.warn( + "MAIL_DEFAULT_SENDER has been changed from tuple to string." + + f" It was converted to '{app.config['MAIL_DEFAULT_SENDER']}'." + + " Auto-conversion will be removed in future version.", + UserWarning, + ) + if "pbkdf2:" not in app.config["ADMIN_PASSWORD"] and app.config["ADMIN_PASSWORD"]: # Since 2.0 warnings.warn( diff --git a/ihatemoney/tests/ihatemoney.cfg b/ihatemoney/tests/ihatemoney.cfg index 0d2b7368..31247584 100644 --- a/ihatemoney/tests/ihatemoney.cfg +++ b/ihatemoney/tests/ihatemoney.cfg @@ -6,4 +6,4 @@ SQLACHEMY_ECHO = DEBUG SECRET_KEY = "supersecret" -MAIL_DEFAULT_SENDER = ("Budget manager", "admin@example.com") +MAIL_DEFAULT_SENDER = "Budget manager " diff --git a/ihatemoney/tests/ihatemoney_envvar.cfg b/ihatemoney/tests/ihatemoney_envvar.cfg index 4790bd2c..05306127 100644 --- a/ihatemoney/tests/ihatemoney_envvar.cfg +++ b/ihatemoney/tests/ihatemoney_envvar.cfg @@ -6,4 +6,4 @@ SQLACHEMY_ECHO = DEBUG SECRET_KEY = "lalatra" -MAIL_DEFAULT_SENDER = ("Budget manager", "admin@example.com") +MAIL_DEFAULT_SENDER = "Budget manager " diff --git a/ihatemoney/tests/main_test.py b/ihatemoney/tests/main_test.py index 24243fac..5d69130c 100644 --- a/ihatemoney/tests/main_test.py +++ b/ihatemoney/tests/main_test.py @@ -26,7 +26,7 @@ class ConfigurationTestCase(BaseTestCase): self.assertFalse(self.app.config["SQLALCHEMY_TRACK_MODIFICATIONS"]) self.assertEqual( self.app.config["MAIL_DEFAULT_SENDER"], - ("Budget manager", "admin@example.com"), + ("Budget manager "), ) self.assertTrue(self.app.config["ACTIVATE_DEMO_PROJECT"]) self.assertTrue(self.app.config["ALLOW_PUBLIC_PROJECT_CREATION"]) diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py index 9ed0c0d8..ddfcf356 100644 --- a/ihatemoney/utils.py +++ b/ihatemoney/utils.py @@ -1,6 +1,7 @@ import ast import csv from datetime import datetime, timedelta +import email.utils from enum import Enum from io import BytesIO, StringIO, TextIOWrapper from json import JSONEncoder, dumps @@ -53,7 +54,9 @@ def flash_email_error(error_message, category="danger"): admin email as a contact if MAIL_DEFAULT_SENDER is set to not the default value and SHOW_ADMIN_EMAIL is True. """ - (admin_name, admin_email) = current_app.config.get("MAIL_DEFAULT_SENDER") + (admin_name, admin_email) = email.utils.parseaddr( + current_app.config.get("MAIL_DEFAULT_SENDER") + ) error_extension = "." if admin_email != "admin@example.com" and current_app.config.get( "SHOW_ADMIN_EMAIL"