-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
155 lines (144 loc) · 5.24 KB
/
Copy pathdatabase.py
File metadata and controls
155 lines (144 loc) · 5.24 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
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
import sqlite3
import logging
from datetime import datetime
from contextlib import contextmanager
from markupsafe import escape
import bleach
logger = logging.getLogger(__name__)
class Database:
def __init__(self):
self.db_file = 'posts.db'
@contextmanager
def get_connection(self):
"""获取数据库连接"""
try:
conn = sqlite3.connect(self.db_file)
conn.row_factory = sqlite3.Row
try:
yield conn
finally:
conn.close()
except sqlite3.Error as e:
logger.error(f"Database connection error: {e}")
raise
def init_db(self):
"""初始化数据库"""
try:
with self.get_connection() as conn:
conn.execute('''
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY,
title TEXT,
content TEXT,
created_at TEXT
)
''')
conn.execute('''
CREATE TABLE IF NOT EXISTS images (
id INTEGER PRIMARY KEY,
post_id INTEGER,
filename TEXT,
FOREIGN KEY(post_id) REFERENCES posts(id)
)
''')
conn.commit()
except Exception as e:
logger.error(f"Init DB error: {e}")
raise
def get_posts(self, limit=None):
"""获取文章列表"""
try:
with self.get_connection() as conn:
query = 'SELECT * FROM posts ORDER BY id DESC'
if limit:
return conn.execute(query + ' LIMIT ?', (limit,)).fetchall()
return conn.execute(query).fetchall()
except Exception as e:
logger.error(f"Get posts error: {e}")
raise
def add_post(self, title, content):
"""添加新文章"""
try:
with self.get_connection() as conn:
cursor = conn.execute(
'INSERT INTO posts (title, content, created_at) VALUES (?, ?, ?)',
(escape(title), bleach.clean(content,
tags=['b', 'i', 'u', 'h1', 'h2', 'h3', 'p', 'br', 'ul', 'ol', 'li', 'blockquote', 'img'],
attributes={
'*': ['style'],
'img': ['src', 'alt', 'style']
},
protocols=['http', 'https']
), datetime.now().isoformat())
)
conn.commit()
return cursor.lastrowid
except Exception as e:
logger.error(f"Add post error: {e}")
raise
def get_post_by_id(self, post_id):
"""根据ID获取文章"""
try:
with self.get_connection() as conn:
cursor = conn.execute(
'SELECT * FROM posts WHERE id = ?',
(post_id,)
)
return cursor.fetchone()
except Exception as e:
logger.error(f"Get post by ID error: {e}")
raise
def add_image(self, post_id, filename):
"""添加图片记录"""
try:
with self.get_connection() as conn:
conn.execute(
'INSERT INTO images (post_id, filename) VALUES (?, ?)',
(post_id, filename)
)
conn.commit()
except Exception as e:
logger.error(f"Add image error: {e}")
raise
def get_images_by_post(self, post_id):
"""获取文章关联的图片"""
try:
with self.get_connection() as conn:
return conn.execute(
'SELECT filename FROM images WHERE post_id = ?',
(post_id,)
).fetchall()
except Exception as e:
logger.error(f"Get images by post error: {e}")
raise
def update_post(self, post_id, title, content):
"""更新文章"""
try:
with self.get_connection() as conn:
conn.execute(
'UPDATE posts SET title = ?, content = ? WHERE id = ?',
(escape(title), bleach.clean(content,
tags=['b', 'i', 'u', 'h1', 'h2', 'h3', 'p', 'br', 'ul', 'ol', 'li', 'blockquote', 'img'],
attributes={
'*': ['style'],
'img': ['src', 'alt', 'style']
},
protocols=['http', 'https']
), post_id)
)
conn.commit()
except Exception as e:
logger.error(f"Update post error: {e}")
raise
def delete_post(self, post_id):
"""删除文章"""
try:
with self.get_connection() as conn:
conn.execute(
'DELETE FROM posts WHERE id = ?',
(post_id,)
)
conn.commit()
except Exception as e:
logger.error(f"Delete post error: {e}")
raise