def put(): ''' 회원의 password를 바꿔주는 PUT Method :parameter: pw, pw_check :return: status code 200 - 성공 401 - JWT 존재 X or 만료됨 422 - JWT Decoding 오류 411 - 비밀번호 확인 불일치 ''' parser = reqparse.RequestParser() parser.add_argument('pw', type=str) parser.add_argument('pw_check', type=str) args = parser.parse_args() _userPw = args['pw'] _userPwC = args['pw_check'] if _userPw != _userPwC: return {"message": "pw and pw_check do not match", "code": 411}, 411 current_user = get_jwt_identity() sql = f'UPDATE userlog SET user_pw = "{_userPw}" WHERE user_id = "{current_user}"' cursor.execute(sql) db.commit() db.close() return {"message": "Your password has changed normally.", "code": 200}, 200
def login(): ''' id와 pw를 받아서 로그인을 헤주는 POST Method :parameter: id, pw :return: status code 200 - 로그인 성공 410 - ID 입력 오류 411 - PW 입력 오류 ''' parser = reqparse.RequestParser() parser.add_argument('id', type=str) parser.add_argument('pw', type=str) args = parser.parse_args() _userID = args['id'] _userPW = args['pw'] if id_exist(db, cursor, _userID) == False: return {"message": "Invalid ID entered.", "code": 410}, 410 sql = f'SELECT user_pw FROM UserLog WHERE user_id = "{_userID}"' cursor.execute(sql) if _userPW != cursor.fetchone()[0]: return {"message": 'Wrong PW entered.', "code": 411}, 411 else: access_token = create_access_token(identity=_userID) return {"access_token": access_token, "code": 200}, 200
def delete(): ''' 회원 탈퇴를 위한 DELETE Method :parameter: X :return: status code 200 - 성공 401 - JWT 토큰 X or 만료 422 - JWT 디코딩 오류 410 - 해당 계정이 존재하지 않음. ''' current_user = get_jwt_identity() if id_exist(db, cursor, current_user) == False: return {"message": "The account does not exist.", "code": 410}, 410 sql = f'DELETE FROM userlog WHERE user_id="{current_user}"' cursor.execute(sql) db.commit() db.close() return { "message": "Deleting account completed successfully.", "code": 200 }, 200
def delete(date): reqp = reqparse.RequestParser() reqp.add_argument('train_num', type=str) reqp.add_argument('seat', type=str) reqp.add_argument('start', type=str) reqp.add_argument('end', type=str) args = reqp.parse_args() _id = get_jwt_identity() train_num = args['train_num'] seat = args['seat'] start = args['start'] end = args['end'] print(_id, date, train_num, seat, start, end) sql = f'SELECT * FROM reservation WHERE id = "{_id}" AND date = "{date}" AND train_num = "{train_num}" AND start = "{start}" AND end = "{end}"' cursor.execute(sql) search_data = cursor.fetchone() if search_data == None: return {"Message": "예매 X"}, 422 sql = f'DELETE FROM reservation WHERE id = "{_id}" AND date = "{date}" AND train_num = "{train_num}" AND start = "{start}" AND end = "{end}"' cursor.execute(sql) return 'ok!', 200
def signup(): ''' id와 pw, pw_check을 받아서 회원을 등록시켜주는 POST Method :parameter: id, pw, pw_check :return: status code 200 - 성공 410 - 이미 사용중인 ID 411 - 비밀번호 입력 오류 ''' try: parser = reqparse.RequestParser() parser.add_argument('id', type=str) parser.add_argument('pw', type=str) parser.add_argument('pw_check', type=str) args = parser.parse_args() _userId = args['id'] _userPw = args['pw'] _userPw_check = args['pw_check'] sql = ''' CREATE TABLE userlog ( id INT AUTO_INCREMENT PRIMARY KEY, user_id TEXT NOT NULL, user_pw TEXT NOT NULL )''' try: cursor.execute(sql) except: pass if _userPw != _userPw_check: return {"message": "pw and pw_check do not match.", "code": 411}, 411 if id_exist(db, cursor, _userId) == True: return {"message": "This ID is already in use.", "code": 410}, 410 sql = f'INSERT INTO userlog (user_id, user_pw) VALUES("{_userId}", "{_userPw}")' cursor.execute(sql) db.commit() db.close() return {"message": "Your ID has been successfully registered!", "code": 200}, 200 except SyntaxError as e: return {'error':str(e)}
def get(date): return_dict = {} req = reqparse.RequestParser() req.add_argument('train_num', type=str) req.add_argument('start', type=str) req.add_argument('end', type=str) args = req.parse_args() train_num = args['train_num'] start = args['start'] end = args['end'] sql = f'SELECT * FROM K{date} WHERE train_num = {train_num}' try: cursor.execute(sql) except: for i in range(40): return_dict[str(i + 1)] = '0' return return_dict seat_data = list(cursor.fetchall()) for i in range(40): count = 0 specific_data = list(seat_data[i]) for j in range(train_to_num[start], train_to_num[end]): if specific_data[j+1] == 1: return_dict[str(i+1)] = '1' count += 1 break if count == 0: return_dict[str(i+1)] = '0' return return_dict
def create_table(date): sql = f"CREATE TABLE K{str(date)} (" \ f"Train_Num TEXT NOT NULL," \ f"Seat TEXT NOT NULL," \ f"SeoulToGwangmyeong INT NOT NULL DEFAULT 0," \ f"GwangmyeongToCheonan_Asan INT NOT NULL DEFAULT 0," \ f"Cheonan_AsanToOsong INT NOT NULL DEFAULT 0," \ f"OsongToDaejeon INT NOT NULL DEFAULT 0," \ f"DaejeonToGimcheon_Gumi INT NOT NULL DEFAULT 0," \ f"Gimcheon_GumiToDongdaegu INT NOT NULL DEFAULT 0," \ f"DongdaeguToSingyeongju INT NOT NULL DEFAULT 0," \ f"SingyeongjuToUlsan INT NOT NULL DEFAULT 0," \ f"UlsanToBusan INT NOT NULL DEFAULT 0" \ f")" try: cursor.execute(sql) except: return count = 0 train_num_list = [ 101, 271, 103, 105, 201, 291, 107, 109, 111, 113, 115, 231, 117, 119, 251, 121, 273, 123, 233, 125, 127, 129, 131, 253, 133, 135, 203, 293, 137, 205, 295, 139, 207, 141, 209, 143, 145, 147, 235, 149, 255, 151, 153, 155, 157, 257, 159, 211, 161, 237, 275, 213, 163, 215, 165, 167, 259, 169, 261, 171, 173, 217, 281, 283 ] for i in train_num_list: for j in range(40): sql = f'INSERT INTO k{date} (Train_Num, Seat) VALUES({i}, {j+1})' cursor.execute(sql) print(count)
def get(): _userID = get_jwt_identity() return_dict = {} count = 0 sql = f'SELECT * FROM reservation WHERE id = "{_userID}"' cursor.execute(sql) reservation_data = list(cursor.fetchall()) for i in reservation_data: specific_dict = {} count += 1 specific_dict['date'] = i[1] specific_dict['train_num'] = i[2] specific_dict['seat'] = i[3] specific_dict['start'] = i[4] specific_dict['end'] = i[5] return_dict[count] = specific_dict return return_dict
def post(date): return_dict = {} _userID = get_jwt_identity() print(_userID) req = reqparse.RequestParser() req.add_argument('train_num', type=str) req.add_argument('start', type=str) req.add_argument('end', type=str) req.add_argument('seat', type=int) args = req.parse_args() train_num = args['train_num'] start = args['start'] end = args['end'] seat = args['seat'] create_table(date) train_status_list = train_list(start, end) sql = f'SELECT * FROM k{date} WHERE Train_Num = "{train_num}" AND Seat = "{seat}"' cursor.execute(sql) seat_data = list(cursor.fetchone()) for i in range(train_to_num[start], train_to_num[end]): if seat_data[i + 1] == 1: return '여기는 예약이 불가능함용', 410 sql = f'INSERT INTO reservation (id, date, train_num, seat, start, end) VALUES("{_userID}", "{str(date)}", "{train_num}", "{str(seat)}", "{start}", "{end}")' cursor.execute(sql) for i in range(9): if seat_data[i + 2] == 1: train_status_list[i] = 1 sql = f'UPDATE k{date} SET SeoulToGwangmyeong = {train_status_list[0]}, GwangmyeongToCheonan_Asan = {train_status_list[1]}, Cheonan_AsanToOsong = {train_status_list[2]}, ' \ f'OsongToDaejeon = {train_status_list[3]}, DaejeonToGimcheon_Gumi = {train_status_list[4]}, Gimcheon_GumiToDongdaegu = {train_status_list[5]}, DongdaeguToSingyeongju = {train_status_list[6]},' \ f'SingyeongjuToUlsan = {train_status_list[7]}, UlsanToBusan = {train_status_list[8]} WHERE Train_Num = "{train_num}" AND Seat = "{seat}"' cursor.execute(sql) return f'{str(date)[:4]}년 {str(date)[4:6]}월 {str(date)[6:8]}일 {train_num}번 기차 {seat}좌석 예매를 성공했습니다.', 200
def get(): return_dict = {} count = 1 reqp = reqparse.RequestParser() reqp.add_argument('start', type=str) reqp.add_argument('end', type=str) reqp.add_argument('date', type=str) args = reqp.parse_args() now = datetime.datetime.now() start = args['start'] end = args['end'] time = now.strftime("%H%M") date = args['date'] dt = datetime.date(int(date[0:4]), int(date[4:6]), int(date[6:8])) sql = f'SELECT Train_Num, Train_Name, {start}, {end}, Remark FROM FromSeoulToBusan WHERE {start} != "NULL" AND {end} != "NULL"' cursor.execute(sql) train_inform = list(cursor.fetchall()) for specific in train_inform: if specific[4] != None and date_dict[dt.strftime("%A")] not in str( specific[4]): continue if date == now.strftime("%Y%m%d"): if (specific[2] // 100) * 60 + (specific[2] % 100) < ( int(time) // 100 * 60 + int(time) % 100): continue specific_dict = {} specific_dict['train_num'] = specific[0] specific_dict['train_name'] = specific[1] time_list = [str(specific[2]), str(specific[3])] time_real_list = [] for i in time_list: if len(i) != 4: for _ in range(4 - len(i)): i = '0' + i i = i[:2] + ":" + i[2:] if i[:1] == '0': i = i[1:] else: i = i[:2] + ":" + i[2:] time_real_list.append(i) specific_dict['start_time'] = time_real_list[0] specific_dict['end_time'] = time_real_list[1] date1 = datetime.datetime(int(date[0:4]), int(date[4:6]), int(date[6:8]), specific[2] // 100, specific[2] % 100, 0) date2 = datetime.datetime(int(date[0:4]), int(date[4:6]), int(date[6:8]), specific[3] // 100, specific[3] % 100, 0) train_take_time = str(date2 - date1) if len(train_take_time) < 8: specific_dict['operating_time'] = train_take_time[:4] elif len(train_take_time) > 9: specific_dict['operating_time'] = train_take_time[8:12] return_dict[count] = specific_dict count += 1 sql = f'SELECT * FROM TrainFare WHERE Intersection = "{train_korean_name[start]}_{train_korean_name[end]}"' cursor.execute(sql) fare_dict = {} fare_data = cursor.fetchone() fare_dict['general'] = fare_data[1] fare_dict['special'] = fare_data[2] return_dict['fare'] = fare_dict return return_dict