|
|
|
|
@ -0,0 +1,69 @@
|
|
|
|
|
#zjy 评论模块的工具函数
|
|
|
|
|
#zjy 提供评论相关的辅助功能,如邮件通知
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
|
|
from djangoblog.utils import get_current_site
|
|
|
|
|
from djangoblog.utils import send_email
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_comment_email(comment):
|
|
|
|
|
#zjy 当用户发表评论后,给评论者发送邮件通知;
|
|
|
|
|
#zjy 如果该评论是回复别人的,则同时给被回复的用户发送提醒邮件。
|
|
|
|
|
|
|
|
|
|
#zjy 获取当前站点域名
|
|
|
|
|
site = get_current_site().domain
|
|
|
|
|
|
|
|
|
|
#zjy 邮件主题
|
|
|
|
|
subject = _('Thanks for your comment')
|
|
|
|
|
|
|
|
|
|
#zjy 构造文章访问 URL(以便用户点进查看)
|
|
|
|
|
article_url = f"https://{site}{comment.article.get_absolute_url()}"
|
|
|
|
|
|
|
|
|
|
#zjy 给评论者自己发送的邮件内容
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#zjy 收件人 = 评论的作者本人
|
|
|
|
|
tomail = comment.author.email
|
|
|
|
|
|
|
|
|
|
#zjy 发送邮件
|
|
|
|
|
send_email([tomail], subject, html_content)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
#zjy 如果该评论存在父评论(说明是回复行为)
|
|
|
|
|
if comment.parent_comment:
|
|
|
|
|
#zjy 给被回复的人发送通知邮件
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
#zjy 父评论的作者邮箱
|
|
|
|
|
tomail = comment.parent_comment.author.email
|
|
|
|
|
|
|
|
|
|
#zjy 向被回复者发送邮件
|
|
|
|
|
send_email([tomail], subject, html_content)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
#zjy 出现错误则记录日志,但不影响评论正常流程
|
|
|
|
|
logger.error(e)
|