From 8a691d59e745271aafc871d89b4b1d86ccd26288 Mon Sep 17 00:00:00 2001 From: moanos Date: Fri, 11 Jul 2025 16:53:01 +0200 Subject: [PATCH] feat: add base url in order to properly do URLs in e-mails --- src/fellchensammlung/mail.py | 6 +++--- src/notfellchen/settings.py | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/fellchensammlung/mail.py b/src/fellchensammlung/mail.py index e376fac..94c0c46 100644 --- a/src/fellchensammlung/mail.py +++ b/src/fellchensammlung/mail.py @@ -7,7 +7,7 @@ from django.utils.translation import gettext_lazy as _ from django.conf import settings from django.core import mail from fellchensammlung.models import User, Notification, TrustLevel, NotificationTypeChoices -from notfellchen.settings import host +from notfellchen.settings import base_url NEWLINE = "\r\n" @@ -17,7 +17,7 @@ def mail_admins_new_report(report): Sends an e-mail to all users that should handle the report. """ for moderator in User.objects.filter(trust_level__gt=TrustLevel.MODERATOR): - report_url = "https://" + host + report.get_absolute_url() + report_url = base_url + report.get_absolute_url() context = {"report_url": report_url, "user_comment": report.user_comment, } @@ -39,7 +39,7 @@ def send_notification_email(notification_pk): context = {"notification": notification, } if notification.notification_type == NotificationTypeChoices.NEW_REPORT_COMMENT or notification.notification_type == NotificationTypeChoices.NEW_REPORT_AN: context["user_comment"] = notification.report.user_comment - context["report_url"] = notification.report.get_absolute_url() + context["report_url"] = f"{base_url}{notification.report.get_absolute_url()}" html_message = render_to_string('fellchensammlung/mail/notifications/report.html', context) elif notification.notification_type == NotificationTypeChoices.NEW_USER: html_message = render_to_string('fellchensammlung/mail/notifications/new-user.html', context) diff --git a/src/notfellchen/settings.py b/src/notfellchen/settings.py index d283cd7..df3d12e 100644 --- a/src/notfellchen/settings.py +++ b/src/notfellchen/settings.py @@ -145,6 +145,10 @@ MEDIA_URL = config.get("urls", "media", fallback="/media/") # Take all three into account when modifying host = config.get("notfellchen", "host", fallback='*') +# The base URL will be used to build URLS +# See https://forum.djangoproject.com/t/putting-full-url-link-on-email-how-to-get-current-domain-name-to-put-on-url/13806/3 +base_url = config.get("notfellchen", "base_url", fallback=f"https://{host}") + # see https://docs.djangoproject.com/en/3.2/ref/settings/#std-setting-ALLOWED_HOSTS ALLOWED_HOSTS = [host] CSRF_TRUSTED_ORIGINS = [f"https://{host}"]