perf: 优化用户创建邮件信息,支持部分标签

This commit is contained in:
ibuler
2021-11-03 11:17:18 +08:00
committed by Jiangjie.Bai
parent d57f52ee24
commit bbe2678df3
3 changed files with 79 additions and 63 deletions

View File

@@ -1,4 +1,5 @@
from urllib.parse import urljoin
from collections import defaultdict
from django.utils import timezone
from django.utils.translation import ugettext as _
@@ -13,13 +14,19 @@ class UserCreatedMsg(UserMessage):
def get_html_msg(self) -> dict:
user = self.user
subject = str(settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT)
honorific = str(settings.EMAIL_CUSTOM_USER_CREATED_HONORIFIC)
content = str(settings.EMAIL_CUSTOM_USER_CREATED_BODY)
mail_context = {
'subject': str(settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT),
'honorific': str(settings.EMAIL_CUSTOM_USER_CREATED_HONORIFIC),
'content': str(settings.EMAIL_CUSTOM_USER_CREATED_BODY)
}
user_info = {'username': user.username, 'name': user.name, 'email': user.email}
# 转换成 defaultdict否则 format 时会报 KeyError
user_info = defaultdict(str, **user_info)
mail_context = {k: v.format_map(user_info) for k, v in mail_context.items()}
context = {
'honorific': honorific,
'content': content,
**mail_context,
'user': user,
'rest_password_url': reverse('authentication:reset-password', external=True),
'rest_password_token': user.generate_reset_token(),
@@ -28,7 +35,7 @@ class UserCreatedMsg(UserMessage):
}
message = render_to_string('users/_msg_user_created.html', context)
return {
'subject': subject,
'subject': mail_context['subject'],
'message': message
}