def login(self, user_id: str, password: str, terminal: str) -> (int, str, str): token = "" try: code, message = self.check_password(user_id, password) if code != 200: return code, message, "" token = jwt_encode(user_id, terminal) # cursor = self.conn.execute( # "UPDATE user set token= ? , terminal = ? where user_id = ?", # (token, terminal, user_id), ) # if cursor.rowcount == 0: # return error.error_authorization_fail() + ("", ) # self.conn.commit() row = db_session.query(User).filter_by(user_id=user_id).first() if row is None: return error.error_authorization_fail() + ("", ) row.token = token row.terminal = terminal db_session.commit() except sqlite.Error as e: return 528, "{}".format(str(e)), "" except BaseException as e: return 530, "{}".format(str(e)), "" return 200, "ok", token
def change_password(self, user_id: str, old_password: str, new_password: str) -> (int, str): try: code, message = self.check_password(user_id, old_password) if code != 200: return code, message terminal = "terminal_{}".format(str(time.time())) token = jwt_encode(user_id, terminal) # cursor = self.conn.execute( # "UPDATE user set password = ?, token= ? , terminal = ? where user_id = ?", # (new_password, token, terminal, user_id), ) # if cursor.rowcount == 0: # return error.error_authorization_fail() # self.conn.commit() row = db_session.query(User).filter_by(user_id=user_id).first() if row is None: return error.error_authorization_fail() row.password = new_password row.token = token row.terminal = terminal db_session.commit() except sqlite.Error as e: return 528, "{}".format(str(e)) except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def logout(self, user_id: str, token: str) -> (int, str): try: code, message = self.check_token(user_id, token) if code != 200: return code, message terminal = "terminal_{}".format(str(time.time())) dummy_token = jwt_encode(user_id, terminal) # cursor = self.conn.execute( # "UPDATE user SET token = ?, terminal = ? WHERE user_id=?", # (dummy_token, terminal, user_id), ) # if cursor.rowcount == 0: # return error.error_authorization_fail() # self.conn.commit() row = db_session.query(User).filter_by(user_id=user_id).first() if row is None: return error.error_authorization_fail() row.token = dummy_token row.terminal = terminal db_session.commit() except sqlite.Error as e: return 528, "{}".format(str(e)) except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def send_stock(self, user_id: str, order_id: str) -> (int, str): try: print(user_id, order_id) row = New_Order.query.filter_by(order_id=order_id).first() if row is None: return error.error_invalid_order_id(order_id) row = User.query.filter_by(user_id=user_id).first() if row is None: return error.error_non_exist_user_id(user_id) db_session.query(New_Order_Detail).filter( New_Order_Detail.order_id == order_id).update( {New_Order_Detail.state: 2}) db_session.commit() except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def check_password(self, user_id: str, password: str) -> (int, str): row = db_session.query(User).filter_by(user_id=user_id).first() if row is None: return error.error_authorization_fail() if password != row.password: return error.error_authorization_fail() return 200, "ok"
def check_token(self, user_id: str, token: str) -> (int, str): row = db_session.query(User).filter_by(user_id=user_id).first() if row is None: return error.error_authorization_fail() db_token = row.token if not self.__check_token(user_id, db_token, token): return error.error_authorization_fail() return 200, "ok"
def add_stock_level(self, user_id: str, store_id: str, book_id: str, add_stock_level: int): try: if not self.user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if not self.store_id_exist(store_id): return error.error_non_exist_store_id(store_id) if not self.book_id_exist(store_id, book_id): return error.error_non_exist_book_id(book_id) print(user_id, store_id, book_id) # self.conn.execute("UPDATE store SET stock_level = stock_level + ? " # "WHERE store_id = ? AND book_id = ?", (add_stock_level, store_id, book_id)) # self.conn.commit() db_session.query(Store).filter( Store.store_id == store_id, Store.book_id == book_id).update( {Store.stock_level: Store.stock_level + add_stock_level}) db_session.commit() # except Error as e: # logging.info("528, {}".format(str(e))) # return 528, "{}".format(str(e)), "" except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def unregister(self, user_id: str, password: str) -> (int, str): try: code, message = self.check_password(user_id, password) if code != 200: return code, message # cursor = self.conn.execute("DELETE from user where user_id=?", (user_id,)) # if cursor.rowcount == 1: # self.conn.commit() # else: # return error.error_authorization_fail() row = db_session.query(User).filter_by(user_id=user_id).first() if row is None: return error.error_authorization_fail() else: db_session.delete(row) db_session.commit() except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"