You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tentest/doc/DjangoBlog/comments/utils.py

58 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# FRR该模块提供评论相关的邮件通知功能
# 当用户提交评论或收到回复时,自动发送自动发送邮件通知,提升用户交互体验。
import logging # 导入日志模块,用于记录记录发送邮件过程中的错误信息
from django.utils.translation import gettext_lazy as _ # 导入国际化翻译函数
from djangoblog.utils import get_current_site # 导入获取当前站点信息的工具函数
from djangoblog.utils import send_email # 导入发送邮件的工具函数
# FRR创建日志记录器用于记录当前模块的日志信息
logger = logging.getLogger(__name__)
def send_comment_email(comment):
"""
FRR发送评论相关邮件通知
1. 向评论作者发送评论提交成功的感谢邮件
2. 若当前评论是回复(有父评论),向父评论作者发送回复通知邮件
:param comment: 评论对象Comment实例
"""
# FRR获取当前网站域名用于构建文章链接
site = get_current_site().domain
# FRR邮件主题支持国际化
subject = _('Thanks for your comment')
# FRR构建文章详情页的完整URL包含协议和域名
article_url = f"https://{site}{comment.article.get_absolute_url()}"
# FRR1. 向当前评论作者发送感谢邮件
# 构建HTML格式的邮件内容支持国际化和变量替换
html_content = _("""<p>Thank you very much for your comments on this site</p>
You can visit <a href="%(article_url)s" rel="bookmark">%(article_title)s</a>
to review your comments,
Thank you again!
<br />
If the link above cannot be opened, please copy this link to your browser.
%(article_url)s""") % {'article_url': article_url, 'article_title': comment.article.title}
tomail = comment.author.email # 收件人邮箱(当前评论作者的邮箱)
send_email([tomail], subject, html_content) # 调用工具函数发送邮件
# FRR2. 若当前评论是回复(有父评论),通知父评论作者
try:
if comment.parent_comment: # 判断当前评论是否有父评论(即是否为回复)
# 构建回复通知的HTML邮件内容
html_content = _("""Your comment on <a href="%(article_url)s" rel="bookmark">%(article_title)s</a><br/> has
received a reply. <br/> %(comment_body)s
<br/>
go check it out!
<br/>
If the link above cannot be opened, please copy this link to your browser.
%(article_url)s
""") % {'article_url': article_url, 'article_title': comment.article.title,
'comment_body': comment.parent_comment.body}
tomail = comment.parent_comment.author.email # 收件人邮箱(父评论作者的邮箱)
send_email([tomail], subject, html_content) # 发送回复通知邮件
except Exception as e:
# FRR捕获发送过程中的异常并记录日志不中断主流程
logger.error(e)