|
|
# 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()}"
|
|
|
|
|
|
# FRR:1. 向当前评论作者发送感谢邮件
|
|
|
# 构建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) # 调用工具函数发送邮件
|
|
|
|
|
|
# FRR:2. 若当前评论是回复(有父评论),通知父评论作者
|
|
|
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) |