From 453a9f00082ac47e1a4d6f59a88cb4a4a1f811cb Mon Sep 17 00:00:00 2001 From: Wes Appler Date: Mon, 8 Dec 2025 11:38:36 -0500 Subject: [PATCH] Removed `global_var` variables from email context, added them to the `MarkdownMail` renderer --- hypha/apply/users/services.py | 52 +++++++++++------------------------ hypha/apply/users/views.py | 5 +--- hypha/core/mail.py | 2 ++ 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/hypha/apply/users/services.py b/hypha/apply/users/services.py index 4799056ece..13f51668d1 100644 --- a/hypha/apply/users/services.py +++ b/hypha/apply/users/services.py @@ -92,45 +92,28 @@ def _get_url_params(self) -> None | str: return None - def get_email_context(self) -> dict: - return { - "ORG_LONG_NAME": settings.ORG_LONG_NAME, - "ORG_EMAIL": settings.ORG_EMAIL, - "ORG_SHORT_NAME": settings.ORG_SHORT_NAME, - "site": self.site, - } - def send_email_no_account_found(self, to): - context = self.get_email_context() - subject = "Log in attempt at {ORG_LONG_NAME}".format(**context) + subject = f"Log in attempt at {settings.ORG_LONG_NAME}" # Force subject to a single line to avoid header-injection issues. subject = "".join(subject.splitlines()) email = MarkdownMail("users/emails/passwordless_login_no_account_found.md") - email.send( - to=to, - subject=subject, - from_email=settings.DEFAULT_FROM_EMAIL, - context=context, - ) + email.send(to=to, subject=subject, from_email=settings.DEFAULT_FROM_EMAIL) def send_login_email(self, user): login_path = self._get_login_path(user) timeout_minutes = self.login_token_generator_class().TIMEOUT // 60 - context = self.get_email_context() - context.update( - { - "user": user, - "is_active": user.is_active, - "name": user.get_full_name(), - "username": user.get_username(), - "login_path": login_path, - "timeout_minutes": timeout_minutes, - } - ) + context = { + "user": user, + "is_active": user.is_active, + "name": user.get_full_name(), + "username": user.get_username(), + "login_path": login_path, + "timeout_minutes": timeout_minutes, + } - subject = "Log in to {username} at {ORG_LONG_NAME}".format(**context) + subject = f"Log in to {user.get_username()} at {settings.ORG_LONG_NAME}" # Force subject to a single line to avoid header-injection issues. subject = "".join(subject.splitlines()) @@ -146,15 +129,12 @@ def send_new_account_login_email(self, signup_obj): signup_path = self._get_signup_path(signup_obj) timeout_minutes = self.login_token_generator_class().TIMEOUT // 60 - context = self.get_email_context() - context.update( - { - "signup_path": signup_path, - "timeout_minutes": timeout_minutes, - } - ) + context = { + "signup_path": signup_path, + "timeout_minutes": timeout_minutes, + } - subject = "Welcome to {ORG_LONG_NAME}".format(**context) + subject = f"Welcome to {settings.ORG_LONG_NAME}" # Force subject to a single line to avoid header-injection issues. subject = "".join(subject.splitlines()) diff --git a/hypha/apply/users/views.py b/hypha/apply/users/views.py index 3152d42b56..d741ef756d 100644 --- a/hypha/apply/users/views.py +++ b/hypha/apply/users/views.py @@ -728,16 +728,13 @@ def send_confirm_access_email_view(request): user=request.user, token=generate_numeric_token ) email_context = { - "ORG_LONG_NAME": settings.ORG_LONG_NAME, - "ORG_EMAIL": settings.ORG_EMAIL, - "ORG_SHORT_NAME": settings.ORG_SHORT_NAME, "token": token_obj.token, "username": request.user.email, "site": Site.find_for_request(request), "user": request.user, "timeout_minutes": settings.PASSWORDLESS_LOGIN_TIMEOUT // 60, } - subject = "Confirmation code for {ORG_LONG_NAME}: {token}".format(**email_context) + subject = f"Confirmation code for {settings.ORG_LONG_NAME}: {token_obj.token}" email = MarkdownMail("users/emails/confirm_access.md") email.send( to=request.user.email, diff --git a/hypha/core/mail.py b/hypha/core/mail.py index 418d3ddf3a..b4d0ee7c7d 100644 --- a/hypha/core/mail.py +++ b/hypha/core/mail.py @@ -8,6 +8,7 @@ from django.template import TemplateDoesNotExist, loader from django.utils import translation +from hypha.core.context_processors import global_vars from hypha.core.utils import markdown_to_html logger = logging.getLogger(__name__) @@ -53,6 +54,7 @@ def __init__(self, template_name: str): def _render_template(self, context: Dict) -> str: try: + context.update(global_vars(None)) return loader.render_to_string(self.template_name, context) except TemplateDoesNotExist as e: logger.warning("Template '{0}' does not exists.".format(e))