Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 16 additions & 36 deletions hypha/apply/users/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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())

Expand Down
5 changes: 1 addition & 4 deletions hypha/apply/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions hypha/core/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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))
Expand Down
Loading