forked from sixu05202004/flaskblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
181 lines (129 loc) · 5.23 KB
/
model.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# -*- coding: utf-8 -*-
from flask import url_for
from flask.ext.sqlalchemy import SQLAlchemy, BaseQuery
from werkzeug import cached_property
from datetime import datetime
from myapp import app
db = SQLAlchemy(app)
class CategoryQuery(BaseQuery):
def getall(self):
return self.all()
def getcategory_id(self, id):
return self.get(id)
class Category(db.Model):
__tablename__ = 'category'
query_class = CategoryQuery
id = db.Column(db.Integer, primary_key=True)
category_name = db.Column(db.String(200), unique=True)
def __init__(self, *args, **kwargs):
super(Category, self).__init__(*args, **kwargs)
def __repr__(self):
return '<category name %r>' % self.category_name
article_tags = db.Table('tags',
db.Column(
'tag_id', db.Integer, db.ForeignKey('tag.id')),
db.Column(
'post_id', db.Integer, db.ForeignKey('post.id')),
)
class TagQuery(BaseQuery):
def getall(self):
return self.all()
def gettag_id(self, id):
return self.get(id)
class Tag(db.Model):
__tablename__ = 'tag'
query_class = TagQuery
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
#post = db.relationship('Post', secondary=article_tags)
def __init__(self, *args, **kwargs):
super(Tag, self).__init__(*args, **kwargs)
def __repr__(self):
return '<tag name %r>' % self.name
class PostQuery(BaseQuery):
def getpost_id(self, id):
return self.get(id)
def getall(self):
return self.all()
def getpost_byname(self, name):
return self.filter(Post.post_name.ilike(name)).distinct().first()
def getpost_perpage(self, pageid, per_page):
return self.order_by(Post.post_create_time.desc()).paginate(pageid, per_page)
def hottest(self):
return self.order_by(Post.comment_count.desc(), Post.view_num.desc())
def newpost(self):
return self.order_by(Post.post_modified_time.desc())
def search(self, keywords):
criteria = []
for keyword in keywords.split():
keyword = '%' + keyword + '%'
criteria.append(db.or_(Post.post_title.ilike(keyword),
Post.post_content.ilike(keyword),
Post.tags_name.ilike(keyword)))
q = reduce(db.and_, criteria)
return self.filter(q).distinct()
def search_tag(self, keywords):
criteria = []
for keyword in keywords.split():
keyword = '%' + keyword + '%'
criteria.append(db.or_(Post.tags_name.ilike(keyword)))
q = reduce(db.and_, criteria)
return self.filter(q).distinct()
class Post(db.Model):
__tablename__ = 'post'
query_class = PostQuery
id = db.Column(db.Integer, primary_key=True)
post_content = db.Column(db.Text)
post_title = db.Column(db.String(100))
post_name = db.Column(db.String(200), unique=True)
post_create_time = db.Column(db.DateTime, default=datetime.utcnow)
view_num = db.Column(db.Integer, default=0)
comment_count = db.Column(db.Integer, default=0)
status = db.Column(db.Integer, default=1)
author_id = db.Column(db.Integer, default=1)
post_modified_time = db.Column(db.DateTime, default=datetime.utcnow)
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
categorys = db.relationship('Category', backref=db.backref(
'posts', lazy='dynamic'), lazy='select')
tags = db.relationship('Tag', secondary=article_tags,
backref=db.backref('posts', lazy='dynamic'))
tags_name = db.Column(db.Text)
def __init__(self, *args, **kwargs):
super(Post, self).__init__(*args, **kwargs)
def __repr__(self):
return '<post %r>' % self.post_title
def _url(self):
return url_for('post', name=self.post_name)
@cached_property
def url(self):
return self._url()
@cached_property
def comments(self):
allcomments = Comment.query.filter(Comment.post_id == self.id).all()
return allcomments
class CommentQuery(BaseQuery):
def getall(self):
return self.all()
def getcomment_id(self, id):
return self.get(id)
def newcomment(self):
return self.order_by(Comment.comment_create_time.desc())
class Comment(db.Model):
__tablename__ = 'comment'
query_class = CommentQuery
id = db.Column(db.Integer, primary_key=True)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
posts = db.relationship('Post', backref=db.backref('comments', lazy='dynamic'))
author_name = db.Column(db.String(50))
author_email = db.Column(db.String(100))
author_url = db.Column(db.String(1024))
author_ip = db.Column(db.String(20))
comment_create_time = db.Column(db.DateTime, default=datetime.utcnow)
content = db.Column(db.Text)
isvisible = db.Column(db.Integer, default=1)
def __init__(self, *args, **kwargs):
super(Comment, self).__init__(*args, **kwargs)
def __repr__(self):
return '<comment %r>' % self.content
def pageby(obj, pageid, per_page, orderby):
return obj.order_by(orderby).paginate(pageid, per_page)