forked from dengmin/logpress-tornado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
153 lines (125 loc) · 3.96 KB
/
models.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
#coding=utf-8
try:
import psyco
psyco.full()
except:pass
import peewee
import datetime
import hashlib
import urllib
from core import db
from lib.helpers import create_token,cached_property
from core import smtp_server,settings
from config import DOMAIN as domain
class User(db.Model):
username = peewee.CharField()
password = peewee.CharField()
email = peewee.CharField()
@staticmethod
def create_password(raw):
salt = create_token(8)
passwd = '%s%s%s' % (salt, raw, 'blog_engine')
hsh = hashlib.sha1(passwd).hexdigest()
return "%s$%s" % (salt, hsh)
def check_password(self, raw):
if '$' not in self.password:
return False
salt, hsh = self.password.split('$')
passwd = '%s%s%s' % (salt, raw, 'blog_engine')
verify = hashlib.sha1(passwd).hexdigest()
return verify == hsh
class Meta:
db_table = 'users'
class Category(db.Model):
name = peewee.CharField()
slug = peewee.CharField()
@property
def url(self):
return '/category/%s'%(urllib.quote(self.name.encode('utf8')))
class Meta:
db_table = 'category'
class Post(db.Model):
title = peewee.CharField()
slug = peewee.CharField(index=True, max_length=100)
category = peewee.ForeignKeyField(Category, related_name='posts')
content = peewee.TextField()
readnum = peewee.IntegerField(default=0)
tags = peewee.CharField(null=True)
slug = peewee.CharField(null=True)
created = peewee.DateTimeField(default=datetime.datetime.now)
@property
def url(self):
return '/post/post-%d.html'%(self.id)
@property
def absolute_url(self):
return '%s%s'%(domain,self.url)
@property
def comment_feed(self):
return '%s/archive/%s/feed'(domain,self.id)
@cached_property
def prev(self):
posts = Post.select().where(Post.created < self.created)\
.order_by(Post.created)
return posts.get() if posts.exists() else None
@cached_property
def next(self):
posts = Post.select().where(Post.created > self.created)\
.order_by(Post.created)
return posts.get() if posts.exists() else None
@property
def summary(self):
return self.content.split('<!--more-->')[0] if self.content else self.content
def taglist(self):
if self.tags:
tags = [tag.strip() for tag in self.tags.split(",")]
return set(tags)
else:
return None
class Meta:
db_table = "posts"
order_by = ('-created',)
class Tag(db.Model):
name = peewee.CharField(max_length=50)
post = peewee.IntegerField()
@property
def url(self):
return '/tag/%s'%(urllib.quote(self.name.encode('utf8')))
class Comment(db.Model):
post = peewee.ForeignKeyField(Post, related_name='comments')
author = peewee.CharField()
website = peewee.CharField(null=True)
email = peewee.CharField()
content = peewee.TextField()
ip = peewee.TextField()
parent_id = peewee.IntegerField(null=True)
created = peewee.DateTimeField(default=datetime.datetime.now)
@property
def parent(self):
p = Comment.select().where(Comment.parent_id==self.parent_id,Comment.id==self.id)
return p.get() if p.exists() else None
@property
def url(self):
return '%s/post/post-%s.html#comment-%s'%(domain,self.post.id,self.id)
def gravatar_url(self,size=80):
return 'http://www.gravatar.com/avatar/%s?d=identicon&s=%d' % \
(hashlib.md5(self.email.strip().lower().encode('utf-8')).hexdigest(),
size)
class Meta:
db_table ='comments'
class Link(db.Model):
name = peewee.CharField()
url = peewee.CharField()
class Meta:
db_table = 'links'
from playhouse.signals import post_save
from lib.mail.message import TemplateEmailMessage
@post_save(sender=Comment)
def send_email(model_class, instance,created):
if instance.parent_id == '0':
message = TemplateEmailMessage(u"收到新的评论",'mail/new_comment.html',
settings['smtp_user'],to=[settings['admin_email']],connection=smtp_server,params={'comment':instance})
else:
message = TemplateEmailMessage(u"评论有新的回复",'mail/reply_comment.html',
settings['smtp_user'],to=[instance.email],connection=smtp_server,params={'comment':instance})
message.send()