-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_utils.py
284 lines (244 loc) · 9.2 KB
/
db_utils.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import sqlite3
from werkzeug.security import generate_password_hash, check_password_hash
def db_init():
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
# 创建用户表
c.execute('''
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
password_hash TEXT NOT NULL,
role TEXT NOT NULL CHECK(role IN ('admin', 'teacher', 'student'))
)
''')
# 创建教师信息表
c.execute('''
CREATE TABLE IF NOT EXISTS teachers (
teacher_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER UNIQUE NOT NULL,
name TEXT NOT NULL,
gender TEXT,
title TEXT,
department TEXT,
office_number TEXT,
phone TEXT,
FOREIGN KEY (user_id) REFERENCES users (user_id)
)
''')
# 创建学生信息表
c.execute('''
CREATE TABLE IF NOT EXISTS students (
student_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER UNIQUE NOT NULL,
name TEXT NOT NULL,
gender TEXT,
student_number TEXT,
class TEXT,
phone TEXT,
FOREIGN KEY (user_id) REFERENCES users (user_id)
)
''')
# 创建教师空闲时间表
c.execute('''
CREATE TABLE IF NOT EXISTS teacher_availability (
availability_id INTEGER PRIMARY KEY AUTOINCREMENT,
teacher_id INTEGER NOT NULL,
date TEXT NOT NULL,
start_time TEXT NOT NULL,
end_time TEXT NOT NULL,
is_booked BOOLEAN NOT NULL DEFAULT 0,
FOREIGN KEY (teacher_id) REFERENCES teachers (teacher_id)
)
''')
# 创建预约表
c.execute('''
CREATE TABLE IF NOT EXISTS appointments (
appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,
student_id INTEGER NOT NULL,
teacher_id INTEGER NOT NULL,
date TEXT NOT NULL,
start_time TEXT NOT NULL,
end_time TEXT NOT NULL,
status TEXT NOT NULL CHECK(status IN ('pending', 'approved', 'rejected', 'canceled')),
FOREIGN KEY (student_id) REFERENCES students (student_id),
FOREIGN KEY (teacher_id) REFERENCES teachers (teacher_id)
)
''')
conn.commit()
conn.close()
def generate_test_users():
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
# 清空用户表中的数据
c.execute('DELETE FROM users')
users = [
('admin', 'admin123', 'admin'),
('teacher1', 'teacher123', 'teacher'),
('teacher2', 'teacher123', 'teacher'),
('student1', 'student123', 'student'),
('student2', 'student123', 'student')
]
for username, password, role in users:
password_hash = generate_password_hash(password)
c.execute('INSERT INTO users (username, password_hash, role) VALUES (?, ?, ?)', (username, password_hash, role))
conn.commit()
conn.close()
def add_user(username, password, role):
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
password_hash = generate_password_hash(password)
c.execute('INSERT INTO users (username, password_hash, role) VALUES (?, ?, ?)', (username, password_hash, role))
conn.commit()
conn.close()
def get_all_users():
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
c.execute('SELECT * FROM users')
users = c.fetchall()
conn.close()
return users
def get_all_teachers():
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
c.execute('SELECT * FROM teachers')
teachers = c.fetchall()
conn.close()
return teachers
def get_appointments_by_teacher(teacher_id):
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
c.execute('SELECT * FROM appointments WHERE teacher_id = ?', (teacher_id,))
appointments = c.fetchall()
conn.close()
return appointments
def get_password_hash_by_username(username):
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
c.execute('SELECT password_hash FROM users WHERE username = ?', (username,))
result = c.fetchone()
conn.close()
return result[0] if result else None
def delete_user(user_id):
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
# 删除用户表中的记录
c.execute('DELETE FROM users WHERE user_id = ?', (user_id,))
# 同时删除教师和学生表中相关记录
c.execute('DELETE FROM teachers WHERE user_id = ?', (user_id,))
c.execute('DELETE FROM students WHERE user_id = ?', (user_id,))
conn.commit()
conn.close()
def update_user_info(user_id, username=None, password=None, role=None):
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
if username:
c.execute('UPDATE users SET username = ? WHERE user_id = ?', (username, user_id))
if password:
password_hash = generate_password_hash(password)
c.execute('UPDATE users SET password_hash = ? WHERE user_id = ?', (password_hash, user_id))
if role:
c.execute('UPDATE users SET role = ? WHERE user_id = ?', (role, user_id))
conn.commit()
conn.close()
def get_appointments_by_student(student_id):
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
c.execute('SELECT * FROM appointments WHERE student_id = ?', (student_id,))
appointments = c.fetchall()
conn.close()
return appointments
def create_appointment(student_id, teacher_id, date, start_time, end_time):
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
c.execute('''
INSERT INTO appointments (student_id, teacher_id, date, start_time, end_time, status)
VALUES (?, ?, ?, ?, ?, 'pending')
''', (student_id, teacher_id, date, start_time, end_time))
conn.commit()
conn.close()
def update_appointment_status(appointment_id, status):
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
if status not in ['approved', 'rejected', 'canceled']:
raise ValueError("Invalid status")
c.execute('UPDATE appointments SET status = ? WHERE appointment_id = ?', (status, appointment_id))
conn.commit()
conn.close()
if status == 'approved':
# 同时更新教师的空闲时间表
c.execute('''
UPDATE teacher_availability
SET is_booked = 1
WHERE teacher_id = (
SELECT teacher_id
FROM appointments
WHERE appointment_id = ?
)
AND date = (
SELECT date
FROM appointments
WHERE appointment_id = ?
)
AND start_time = (
SELECT start_time
FROM appointments
WHERE appointment_id = ?
)
AND end_time = (
SELECT end_time
FROM appointments
WHERE appointment_id = ?
)
''', (appointment_id, appointment_id, appointment_id, appointment_id))
conn.commit()
conn.close()
def delete_appointment(appointment_id):
conn = sqlite3.connect('school_appointment.db')
c = conn.cursor()
c.execute('DELETE FROM appointments WHERE appointment_id = ?', (appointment_id,))
conn.commit()
conn.close()
if __name__ == '__main__':
db_init()
generate_test_users()
print("Database initialized and test users generated.")
# # 测试新增用户
# add_user('student3', 'student123', 'student')
# print("New user added.")
# 测试获取所有用户
users = get_all_users()
print("All users:", users)
# # 测试获取所有教师
# teachers = get_all_teachers()
# print("All teachers:", teachers)
# # 测试按照教师ID获取所有预约
# appointments = get_appointments_by_teacher(1)
# print("Appointments for teacher 1:", appointments)
# # 测试根据用户名获取密码哈希
# password_hash = get_password_hash_by_username('admin')
# print("Password hash for admin:", password_hash)
# # 测试删除用户
# delete_user(1)
# print("User with ID 1 deleted.")
# # 测试更新用户信息
# update_user_info(2, username='new_teacher1', password='newpassword123', role='teacher')
# print("User with ID 2 updated.")
# # 测试根据学生ID获取所有预约
# student_appointments = get_appointments_by_student(1)
# print("Appointments for student 1:", student_appointments)
# # 测试创建预约
# create_appointment(1, 2, '2024-06-10', '10:00', '11:00')
# print("Appointment created for student 1 with teacher 2.")
# # 测试更新预约状态为 approved
# update_appointment_status(1, 'approved')
# print("Appointment with ID 1 approved.")
# # 测试更新预约状态为 rejected
# update_appointment_status(1, 'rejected')
# print("Appointment with ID 1 rejected.")
# # 测试更新预约状态为 canceled
# update_appointment_status(1, 'canceled')
# print("Appointment with ID 1 canceled.")
# # 测试删除预约
# delete_appointment(1)
# print("Appointment with ID 1 deleted.")