forked from NoGameNoLife00/mybolg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_wrappers.py
77 lines (57 loc) · 2.14 KB
/
data_wrappers.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
#encoding=utf-8
from data_model import User, Tag, Entry, Category, Friend_link, tag_entry
from blogapp import db
class DataWrappers(object):
def get_all_entries(self):
return Entry.query.all()
def create_entry(self, title, content, tag, category):
ent = Entry(title=title, content=content, category_id=category, create_time=datetime.now())
#查找tag是否存在,并加入tags,如果不存在则创建tag
tag_list = tag_entry.split()
for tag_name in tag_list:
t = db.session.query(Tag).filter(Tag.name == tag_name).first()
if not t:
t = Tag(tag_name)
ent.tag.append(t)
db.session.add(ent)
db.comit()
return ent
def get_prev_entry(self, id):
ent = db.engine.execute('SELECT * FROM `entry` where id<%s order by create_time desc' % id).first()
return ent
def get_next_entry(self, id):
ent = db.engine.execute('SELECT * FROM `entry` where id>%s order by create_time asc ' % id).first()
return ent
def get_entry_by_category(self, categories):
counts = []
for category in categories:
counts.append(Entry.query.filter_by(category=category).count())
return counts
def increase_view_count(self, entry, num):
entry.view_count += num
db.session.commit()
return None
def get_entries_by_page(self, page, par_page):
pages = Entry.query.order_by(Entry.create_time.desc()).paginate(page, par_page)
return pages
def get_entry_by_id(self, id):
ent = Entry.query.get(id)
return ent
def get_all_tags(self):
ts = Tag.query.all()
return ts
def get_all_categories(self):
categories = Category.query.all()
return categories
def get_category_by_id(self, id):
cate = Category.query.get(id)
return cate
def get_tag_by_id(self, id):
tag = Tag.query.get(id)
return tag
def get_all_links(self):
links = Friend_link.query.all()
return links
def get_first_user(self):
user = User.query.all()
return user