forked from liangliangyy/DjangoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
65 lines (55 loc) · 2.65 KB
/
models.py
File metadata and controls
65 lines (55 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from django.db import models
from django.conf import settings
from django.core.mail import send_mail
from blog.models import Article
from django.core.mail import EmailMultiAlternatives
from django.contrib.sites.models import Site
import _thread
# Create your models here.
class Comment(models.Model):
# url = models.URLField('地址', blank=True, null=True)
# email = models.EmailField('电子邮件', blank=True, null=True)
body = models.TextField('正文', max_length=300)
created_time = models.DateTimeField('创建时间', auto_now_add=True)
last_mod_time = models.DateTimeField('修改时间', auto_now=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='作者', on_delete=models.CASCADE)
article = models.ForeignKey(Article, verbose_name='文章', on_delete=models.CASCADE)
parent_comment = models.ForeignKey('self', verbose_name="上级评论", blank=True, null=True)
class Meta:
ordering = ['created_time']
verbose_name = "评论"
verbose_name_plural = verbose_name
def send_comment_email(self, msg):
msg.send()
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
subject = '感谢您发表的评论'
site = Site.objects.get_current().domain
article_url = "https://{site}{path}".format(site=site, path=self.article.get_absolute_url())
html_content = """
<p>非常感谢您在本站发表评论</p>
您可以访问
<a href="%s" rel="bookmark">%s</a>
来查看您的评论,
再次感谢您!
<br />
如果上面链接无法打开,请将此链接复制至浏览器。
%s
""" % (article_url, self.article.title, article_url)
tomail = self.author.email
msg = EmailMultiAlternatives(subject, html_content, from_email='no-reply@lylinux.net', to=[tomail])
msg.content_subtype = "html"
_thread.start_new_thread(self.send_comment_email, (msg,))
if self.parent_comment:
html_content = """
您在 <a href="%s" rel="bookmark">%s</a> 的评论 <br/> %s <br/> 收到回复啦.快去看看吧
<br/>
如果上面链接无法打开,请将此链接复制至浏览器。
%s
""" % (article_url, self.article.title, self.parent_comment.body, article_url)
tomail = self.parent_comment.author.email
msg = EmailMultiAlternatives(subject, html_content, from_email='no-reply@lylinux.net', to=[tomail])
msg.content_subtype = "html"
_thread.start_new_thread(self.send_comment_email, (msg,))
def __str__(self):
return self.body