-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaccount_DB.py
More file actions
390 lines (345 loc) Β· 12.9 KB
/
account_DB.py
File metadata and controls
390 lines (345 loc) Β· 12.9 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""
CodeCraft PMS Project
νμΌλͺ
: account_DB.py
λ§μ§λ§ μμ λ μ§ : 2025/04/07
"""
import pymysql
from mysql_connection import db_connect
from account import *
# μ¬μ©μ(νμ) μμ± ν¨μ
def insert_user(payload, Token):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
add_student = """
INSERT INTO student(s_no, s_id, s_pw, s_name, s_email, s_token, dno)
VALUES (%s, %s, %s, %s, %s, %s, %s)
"""
# λ§€κ°λ³μ λ°μΈλ© λ°©μμΌλ‘ μμ νκ² κ° μ λ¬
cur.execute(add_student, (payload.univ_id, payload.id, payload.pw, payload.name, payload.email, Token, payload.department))
connection.commit()
result = {
"s_no": payload.univ_id,
"s_name": payload.name,
"s_token": Token
}
return result
except Exception as e:
connection.rollback()
print(f"Error [insert_user] : {e}")
return e
finally:
cur.close()
connection.close()
# μ¬μ©μ(νμ) λ‘κ·ΈμΈ μ 보 νμΈ ν¨μ
def validate_user(id, pw):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT s_id, s_pw, s_no FROM student WHERE s_id = %s", (id,))
row = cur.fetchone()
if row and id == row['s_id'] and pw == row['s_pw']:
return row['s_no']
else:
return None
except Exception as e:
print(f"Error [validate_user] : {e}")
raise e # λ¬Έμμ΄ λμ μμΈλ₯Ό μ§μ λ°μμν΄
finally:
cur.close()
connection.close()
# μ¬μ©μ(νμ) λ‘κ·ΈμΈ μ±κ³΅ ν ν ν°(μΈμ
)μ DBμ μ μ₯νλ ν¨μ
# λ‘κ·ΈμΈ μ λ³΄κ° μΌμΉνμ¬ λ‘κ·ΈμΈμ μ±κ³΅νλ©΄, λ‘κ·ΈμΈν νμμ IDμ μμ±λ ν ν°μ λ§€κ° λ³μλ‘ λ°μμ ν΄λΉ μ¬μ©μμ νμ¬ μΈμ
μ μ μ§νκΈ° μν ν ν°μ μ μ₯νλ€
def save_signin_user_token(id, Token):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("UPDATE student SET s_token = %s WHERE s_id = %s", (Token, id))
connection.commit()
cur.execute("SELECT s_no, s_name, s_token FROM student WHERE s_id = %s", (id,))
result = cur.fetchone()
return result
except Exception as e:
connection.rollback()
print(f"Error [save_signin_user_token] : {e}")
return e
finally:
cur.close()
connection.close()
# μ¬μ©μ(νμ) λ‘κ·Έμμ ν¨μ
def signout_user(Token):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("UPDATE student SET s_token = NULL WHERE s_token = %s", (Token,))
connection.commit()
return True
except Exception as e:
connection.rollback()
print(f"Error [signout_user] : {e}")
return e
finally:
cur.close()
connection.close()
# μ¬μ©μ(νμ) μμ ν¨μ
def delete_user(id):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT COUNT(*) AS cnt FROM student WHERE s_id = %s", (id,))
result = cur.fetchone()
if result['cnt'] == 0:
print(f"Error [delete_user] : User ID {id} does not exist.")
return False
cur.execute("DELETE FROM student WHERE s_id = %s", (id,))
connection.commit()
return True
except Exception as e:
connection.rollback()
print(f"Error [delete_user] : {e}")
return e
finally:
cur.close()
connection.close()
# μ¬μ©μ(νμ) ν ν° νμΈ ν¨μ
def validate_user_token(id, Token):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT s_token FROM student WHERE s_id = %s", (id,))
stored_token = cur.fetchone()
# λ§€κ° λ³μλ‘ λ°μ ν ν°κ³Ό DBμ μ μ₯λ ν΄λΉ νμμ ν ν° κ°μ΄ μΌμΉνλ€λ©΄ Trueλ₯Ό λ°ν
if Token == stored_token['s_token']:
return True
else:
return False
except Exception as e:
print(f"Error [validate_user_token] : {e}")
return e
finally:
cur.close()
connection.close()
# νλ²μΌλ‘ νμμ μ΄λ¦μ μ‘°ννλ ν¨μ
# νλ²μ λ§€κ° λ³μλ‘ λ°λλ€
def fetch_student_name(univ_id):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT s_name FROM student WHERE s_no = %s", (univ_id,))
result = cur.fetchone()
return result
except Exception as e:
print(f"Error [fetch_student_name] : {e}")
return e
finally:
cur.close()
connection.close()
# νμκ°μ
μ μ 체 νκ³Ό μ 보λ₯Ό μ‘°ννλ ν¨μ
# λ§€κ° λ³μλ μμΌλ©°, μ 체 νκ³Ό μ 보λ₯Ό μ‘°ννλ€
def fetch_dept_list():
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT dno, dname FROM dept ORDER BY dno")
result = cur.fetchall()
return result
except Exception as e:
print(f"Error [fetch_dept_list] : {e}")
return e
finally:
cur.close()
connection.close()
# ------------------------------ κ΅μ κ³μ ------------------------------ #
# κ΅μ λ‘κ·ΈμΈ μ 보 νμΈ ν¨μ
def validate_professor(id, pw):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT f_no, f_id, f_pw FROM professor WHERE f_id = %s", (id,))
row = cur.fetchone()
if row and id == row['f_id'] and pw == row['f_pw']:
return row['f_no']
else:
return None
except Exception as e:
print(f"Error [validate_professor] : {e}")
raise e
finally:
cur.close()
connection.close()
# κ΅μ λ‘κ·ΈμΈ μ±κ³΅ ν ν ν°(μΈμ
)μ DBμ μ μ₯νλ ν¨μ
# κ΅μκ° λ‘κ·ΈμΈμ μ±κ³΅νλ©΄, κ΅μμ IDμ μμ±λ ν ν°μ λ§€κ° λ³μλ‘ λ°μμ ν΄λΉ κ΄λ¦¬μμ νμ¬ μΈμ
μ μ μ§νκΈ° μν ν ν°μ μ μ₯νλ€
def save_signin_professor_token(id, Token):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("UPDATE professor SET f_token = %s WHERE f_id = %s", (Token, id))
connection.commit()
cur.execute("SELECT f_no, f_name, f_token FROM professor WHERE f_id = %s", (id,))
result = cur.fetchone()
return result
except Exception as e:
connection.rollback()
print(f"Error [save_signin_professor_token] : {e}")
return e
finally:
cur.close()
connection.close()
# κ΅μ λ‘κ·Έμμ ν¨μ
def signout_professor(Token):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("UPDATE professor SET f_token = NULL WHERE f_token = %s", (Token,))
connection.commit()
return True
except Exception as e:
connection.rollback()
print(f"Error [signout_professor] : {e}")
return e
finally:
cur.close()
connection.close()
# κ΅μ ν ν° νμΈ ν¨μ
def validate_professor_token(id, Token):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT f_token FROM professor WHERE f_id = %s", (id,))
stored_token = cur.fetchone()
if Token == stored_token['f_token']:
return True
else:
return False
except Exception as e:
print(f"Error [validate_professor_token] : {e}")
return e
finally:
cur.close()
connection.close()
# νμ¬ μ¬μ©μ κ³μ μ΄ νμ λλ κ΅μμΈμ§ νλ¨νλ ν¨μ
# ν ν°μ λ§€κ° λ³μλ‘ λ°μΌλ©°, νμμ΄λ©΄ 1μ λ°ννκ³ , κ΅μμ΄λ©΄ 2λ₯Ό λ°ννλ€
def check_user_type(Token):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT COUNT(*) AS cnt FROM student WHERE s_token = %s", (Token,))
student = cur.fetchone()
cur.execute("SELECT COUNT(*) AS cnt FROM professor WHERE f_token = %s", (Token,))
professor = cur.fetchone()
if student['cnt'] == 1 and professor['cnt'] == 0:
return 1
elif student['cnt'] == 0 and professor['cnt'] == 1:
return 2
elif student['cnt'] == 0 and professor['cnt'] == 0:
print(f"Warning [check_user_type] : Token [{Token}] value is not exist in DB. (Student Count : {student['cnt']}, Professor Count : {professor['cnt']})")
return 0
else:
print(f"Warning [check_user_type] : Token [{Token}] value is unknown user type. (Student Count : {student['cnt']}, Professor Count : {professor['cnt']})")
return 0
except Exception as e:
print(f"Error [check_user_type] : {e}")
return e
finally:
cur.close()
connection.close()
# νμ¬ μ¬μ©μ μμ νκ³Όμ λͺ¨λ κ΅μ λͺ©λ‘μ μ‘°ννλ ν¨μ
# νλ²μ λ§€κ° λ³μλ‘ λ°λλ€
def fetch_professor_list(univ_id):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT f_no, f_name FROM professor WHERE dno = (SELECT dno FROM student WHERE s_no = %s)", (univ_id,))
result = cur.fetchall()
return result
except Exception as e:
print(f"Error [fetch_professor_list] : {e}")
return e
finally:
cur.close()
connection.close()
# νλ‘μ νΈ μμ± μ κ³Όλͺ© μ½λλ‘ ν΄λΉ νκ³Όμ κ΅μ λͺ©λ‘μ μ‘°ννλ ν¨μ
# κ³Όλͺ© μ½λλ₯Ό λ§€κ° λ³μλ‘ λ°λλ€
def fetch_professor_list_by_subject(subj_no):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT f_no, f_name FROM professor WHERE dno = (SELECT dno FROM subject WHERE subj_no = %s)", (subj_no,))
result = cur.fetchall()
return result
except Exception as e:
print(f"Error [fetch_professor_list_by_subject] : {e}")
return e
finally:
cur.close()
connection.close()
# ------------------------------ κ³μ μ°ΎκΈ°, λΉλ°λ²νΈ λ³κ²½ ------------------------------ #
# μ¬μ©μ(νμ)μ λΉλ°λ²νΈ(PW)λ₯Ό μ°ΎκΈ° μν΄ μ 보λ₯Ό νμΈνλ ν¨μ
# νλ², μ΄λ¦, μ΄λ©μΌ, μμ΄λλ₯Ό λ§€κ° λ³μλ‘ λ°λλ€
def find_user_pw(univ_id, name, email, id):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
name = name.strip()
email = email.strip()
id = id.strip()
cur.execute("SELECT s_pw FROM student WHERE s_no = %s AND s_name = %s AND s_email = %s AND s_id = %s", (univ_id, name, email, id))
result = cur.fetchone()
if result:
return True
else:
return False
except Exception as e:
print(f"Error [find_user_pw] : {e}")
return e
finally:
cur.close()
connection.close()
# μ¬μ©μ(νμ)μ λΉλ°λ²νΈ(PW)λ₯Ό λ³κ²½νλ ν¨μ
# νλ²κ³Ό μλ‘ λ³κ²½ν λΉλ°λ²νΈλ₯Ό λ§€κ° λ³μλ‘ λ°λλ€
def edit_user_pw(univ_id, pw):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("UPDATE student SET s_pw = %s WHERE s_no = %s", (pw, univ_id))
connection.commit()
return True
except Exception as e:
connection.rollback()
print(f"Error [edit_user_pw] : {e}")
return e
finally:
cur.close()
connection.close()
# ------------------------------ κ³μ μ 보 μ‘°ν λ° μμ ------------------------------ #
# μ¬μ©μ(νμ)μ μ 보λ₯Ό μ‘°ννλ ν¨μ
# νλ²μ λ§€κ° λ³μλ‘ λ°λλ€
def fetch_student_info(univ_id):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("SELECT * FROM student WHERE s_no = %s", (univ_id,))
result = cur.fetchone()
return result
except Exception as e:
print(f"Error [fetch_student_info] : {e}")
return e
finally:
cur.close()
connection.close()
# μ¬μ©μ(νμ)μ κ³μ μ 보λ₯Ό μμ νλ ν¨μ
# μμ νλ €λ λΉλ°λ²νΈ, μ΄λ©μΌ, νκ³Όμ μμ νλ €λ μ¬μ©μ(νμ)μ νλ²μ λ§€κ° λ³μλ‘ λ°λλ€
def edit_student_info(pw, email, dno, univ_id):
connection = db_connect()
cur = connection.cursor(pymysql.cursors.DictCursor)
try:
cur.execute("UPDATE student SET s_pw = %s, s_email = %s, dno = %s WHERE s_no = %s", (pw, email, dno, univ_id))
connection.commit()
return True
except Exception as e:
connection.rollback()
print(f"Error [edit_student_info] : {e}")
return e
finally:
cur.close()
connection.close()