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.
59 lines
3.0 KiB
59 lines
3.0 KiB
#hz 评论模块的工具函数
|
|
#hz 本模块提供了评论相关的工具函数,主要包括发送评论通知邮件的功能
|
|
|
|
import logging
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from djangoblog.utils import get_current_site
|
|
from djangoblog.utils import send_email
|
|
|
|
#hz 获取当前模块的日志记录器,用于记录错误信息
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
#hz 发送评论通知邮件函数
|
|
#hz 当用户提交评论后,向评论作者发送感谢邮件;如果是回复评论,则向被回复的用户发送通知邮件
|
|
def send_comment_email(comment):
|
|
#hz 获取当前网站的域名
|
|
site = get_current_site().domain
|
|
#hz 邮件主题
|
|
subject = _('Thanks for your comment')
|
|
#hz 构建文章的完整URL地址
|
|
article_url = f"https://{site}{comment.article.get_absolute_url()}"
|
|
#hz 构建发送给评论作者的邮件内容(感谢邮件)
|
|
#hz 包含文章链接和标题,感谢用户评论
|
|
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}
|
|
#hz 获取评论作者的邮箱地址
|
|
tomail = comment.author.email
|
|
#hz 向评论作者发送感谢邮件
|
|
send_email([tomail], subject, html_content)
|
|
#hz 处理回复评论的情况:如果当前评论是回复其他评论,则向被回复的用户发送通知邮件
|
|
try:
|
|
#hz 检查当前评论是否有父评论(是否为回复评论)
|
|
if comment.parent_comment:
|
|
#hz 构建发送给被回复用户的邮件内容(回复通知邮件)
|
|
#hz 包含文章链接、标题和被回复的评论内容,通知用户有人回复了他们的评论
|
|
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}
|
|
#hz 获取被回复评论的作者邮箱地址
|
|
tomail = comment.parent_comment.author.email
|
|
#hz 向被回复的用户发送通知邮件
|
|
send_email([tomail], subject, html_content)
|
|
#hz 捕获发送邮件过程中可能出现的异常,记录错误日志但不影响主流程
|
|
except Exception as e:
|
|
logger.error(e)
|