Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert MAIL_DEFAULT_SENDER to a string #1007

Merged
merged 3 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 <admin@example.com>" \
MAIL_PASSWORD="" \
MAIL_PORT="25" \
MAIL_SERVER="localhost" \
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <admin@example.com>"
- MAIL_PASSWORD=
- MAIL_PORT=25
- MAIL_SERVER=localhost
Expand Down
7 changes: 3 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <admin@example.com>`
- **Production value:** Any valid email address.

## SHOW_ADMIN_EMAIL

Expand Down
2 changes: 1 addition & 1 deletion ihatemoney/conf-templates/ihatemoney.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 <admin@example.com>" # CUSTOMIZE

# A boolean that determines whether the admin email (MAIL_DEFAULT_SENDER) is
# shown in error messages.
Expand Down
2 changes: 1 addition & 1 deletion ihatemoney/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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 <admin@example.com>"
SHOW_ADMIN_EMAIL = True
ACTIVATE_DEMO_PROJECT = True
ADMIN_PASSWORD = ""
Expand Down
10 changes: 10 additions & 0 deletions ihatemoney/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']}'."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

+ " 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(
Expand Down
2 changes: 1 addition & 1 deletion ihatemoney/tests/ihatemoney.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ SQLACHEMY_ECHO = DEBUG

SECRET_KEY = "supersecret"

MAIL_DEFAULT_SENDER = ("Budget manager", "admin@example.com")
MAIL_DEFAULT_SENDER = "Budget manager <admin@example.com>"
2 changes: 1 addition & 1 deletion ihatemoney/tests/ihatemoney_envvar.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ SQLACHEMY_ECHO = DEBUG

SECRET_KEY = "lalatra"

MAIL_DEFAULT_SENDER = ("Budget manager", "admin@example.com")
MAIL_DEFAULT_SENDER = "Budget manager <admin@example.com>"
2 changes: 1 addition & 1 deletion ihatemoney/tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_default_configuration(self):
self.assertFalse(self.app.config["SQLALCHEMY_TRACK_MODIFICATIONS"])
self.assertEqual(
self.app.config["MAIL_DEFAULT_SENDER"],
("Budget manager", "admin@example.com"),
("Budget manager <admin@example.com>"),
)
self.assertTrue(self.app.config["ACTIVATE_DEMO_PROJECT"])
self.assertTrue(self.app.config["ALLOW_PUBLIC_PROJECT_CREATION"])
Expand Down
5 changes: 4 additions & 1 deletion ihatemoney/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
zorun marked this conversation as resolved.
Show resolved Hide resolved
"SHOW_ADMIN_EMAIL"
Expand Down