def do_changename(page): # 输入密码 password = get_input(message="请输入密码:", warning="密码不能为空", is_password=True) if page.user.password != encrypt_password(password, page.user.username): input("密码错误...按任意键继续") page.show() # 输入新昵称 nickname = get_input(message="请输入昵称(1-10位):", check_re=r"^\S{1,10}$") logger.debug(f"昵称:{nickname}") page.user.nickname = nickname UserinfoDAO.update(page.user) input("昵称修改成功...按任意键继续") page.show()
def do_findpass(page): """找回密码""" username = get_input(message="请输入用户名:", check_re=r"^\w{3,16}$") logger.debug(f"用户名:{username}") user = UserinfoDAO.select_one(username) if user is None: print("用户不存在...按任意键继续") page.show() print(f"验证码发送到尾号为({user.phone[-4:]})的手机上,请注意查收...") random_code = get_input(message="请输入验证码:", check_re=r"^\d{6}$") # 暂时随便输 logger.debug(f"用户输入验证码 {random_code}") # 检查验证码的正确性,这里暂不实现 # 输入新密码 f_password = get_input(message="请输入新密码(6-12位):", warning="密码不能为空", is_password=True) logger.debug(f"输入新密码 {f_password}") # 确认新密码 a_password = get_input(message="请确认新密码:", warning="两次输入的密码不一致,请重新输入", check_re=f"^{f_password}$", is_password=True) logger.debug(f"确认新密码 {a_password}") # 更新用户密码 user.password = f_password UserinfoDAO.update(user) # 为了辨识,user 为实参,p_user 为形参 input("修改密码成功...按任意键继续") page.show()
def do_readerlist(page): command = get_input(message="查询条件:用户名(u),手机号(p),读者详情(i),回车查全部:", check_re=r"^[upiUPI]$", empty=True) logger.debug(command) if not command or command.lower() != 'i': if command.lower() == 'u': username = get_input(message="请输入用户名:") if command.lower() == 'p': phone = get_input(message="请输入手机号:") print('-' * page.MESSAGE_MAX_LEN) print("{:4}|{:9}|{:10}|{}".format("编号", "用户名", "昵称", "手机号")) print('-' * page.MESSAGE_MAX_LEN) readers = UserinfoDAO.select(level="reader") count = 0 for reader in readers: if command.lower() == 'u' and reader.username != username: continue if command.lower() == 'p' and reader.phone != phone: continue nickname = reader.nickname nickname_size = 12 - (len(nickname.encode("gbk")) - len(nickname)) print(f"{reader.userid:<6}|{reader.username:12}|" f"{nickname:{nickname_size}}|{reader.phone:11}") count += 1 print('-' * page.MESSAGE_MAX_LEN) print(f"共 {count} 条数据") reader_id = get_input(message="查看用户详情,请输入用户编号:", check_re=r"^\d+$", empty=True) if reader_id: logger.debug(reader_id) reader = UserinfoDAO.select_one(p_userid=int(reader_id)) if reader is None: print(f"读者 {reader_id} 不存在...按任意键继续") page.show() else: # 进入读者详情页 ReaderinfoPage(user=page.user, reader=reader, home=page.home_page, prev=page).show() else: page.show()
def select(p_state=None, p_is_borrow=None, p_author=None): """ 查询多条图书数据 :param p_state: normal 未删除图书, default 全部 :param p_is_borrow: bool 借阅状态 :param p_author: 作者 :return: Bookinfo 列表 """ res = [] logger.debug("数据文件:%s" % os.path.abspath(BookinfoDAO.FILE_NAME)) if os.path.exists(BookinfoDAO.FILE_NAME): with open(BookinfoDAO.FILE_NAME, 'r', encoding=SuperModel.FILE_ENCODING) as rf: for line in rf.readlines(): fields = line.strip().split(SuperModel.FILE_SEPARATOR) book = Bookinfo() book.id = int(fields[0]) book.booktype = fields[1] book.bookname = fields[2] book.author = fields[3] book.state = fields[4] book.is_borrow = fields[5] book.createuser = fields[6] if p_state: if p_state == "normal": if book.state != Bookinfo.BOOK_STATUS["NORMAL"]: continue else: raise ValueError("state 参数错误") if p_is_borrow: if type(p_is_borrow) is not bool: raise ValueError("is_borrow 参数错误") if book.is_borrow != p_is_borrow: continue if p_author and book.author != p_author: continue res.append(book) return res
def update(p_borrow): if p_borrow is not None and p_borrow.id > 0: borrows = BorrowregDAO.select() for borrow in borrows: if borrow.id == p_borrow.id: logger.debug(borrow) logger.debug(p_borrow) borrow.borrow_user_id = p_borrow.borrow_user_id borrow.return_user_id = p_borrow.return_user_id borrow.book_id = p_borrow.book_id borrow.state = p_borrow.state logger.info("UPDATE borrow:") logger.info(borrow) break with open(BorrowregDAO.FILE_NAME, 'w', encoding=SuperModel.FILE_ENCODING) as wf: for borrow in borrows: wf.write(borrow.to_file_str()) else: raise ValueError("借阅信息错误.")
def select(p_state=None, p_borrow_user_id=None): """ 查询多条借阅数据 :param p_state: borrow:借阅 return:归还 默认:全部 :param p_borrow_user_id: int 类型 借阅人id,Userinfo.id :return: Borrowreg 列表 """ res = [] logger.debug("数据文件: %s" % os.path.abspath(BorrowregDAO.FILE_NAME)) if os.path.exists(BorrowregDAO.FILE_NAME): with open(BorrowregDAO.FILE_NAME, 'r', encoding=SuperModel.FILE_ENCODING) as rf: for line in rf.readlines(): fields = line.strip().split(SuperModel.FILE_SEPARATOR) borrow = Borrowreg() borrow.id = int(fields[0]) borrow.book_id = int(fields[1]) borrow.borrow_user_id = int(fields[2]) borrow.state = fields[3] borrow.return_user_id = int( fields[4]) if fields[4] != "None" else None if p_state: if p_state == "borrow": if borrow.state != Borrowreg.BORROW_STATUS[ 'BORROW']: continue else: raise ValueError("state 参数错误.") if p_borrow_user_id: if type(p_borrow_user_id) is not int: raise ValueError("p_borrow_user_id 参数类型错误.") if borrow.borrow_user_id != p_borrow_user_id: continue res.append(borrow) return res
def do_login(page): logger.info("用户登录检查") username = get_input(message="请输入用户名:", warning="用户名不能为空") logger.debug(f"用户名:{username}") password = get_input(message="请输入密码:", warning="密码不能为空", is_password=True) try: user = UserinfoDAO.select_one(username) logger.info(f"当前用户 {str(user)}") # 检查密码 if user is None or\ user.password != encrypt_password(password, username): raise PermissionError("用户名或密码错误") input("登录成功...按任意键继续") # 进入 homepage HomePage(user).show() except Exception as e: logger.exception(e) input(str(e) + "...按任意键重新输入") page.show()
def do_changepass(page): # 输入旧密码 o_password = get_input(message="请输入旧密码:", warning="密码不能为空", is_password=True) if page.user.password != encrypt_password(o_password, page.user.username): input("密码错误...按任意键继续") page.show() # 输入新密码 f_password = get_input(message="请输入新密码(6-12位):", warning="密码不能为空", is_password=True) # 确认新密码 a_password = get_input(message="请确认新密码:", warning="两次输入的密码不一致,请重新输入", check_re=f"^{f_password}$", is_password=True) logger.debug(f"确认密码:{a_password}") page.user.password = a_password UserinfoDAO.update(page.user) input("密码修改成功...按任意键继续") page.show()
def update(p_book): if p_book is not None and p_book.id > 0: books = BookinfoDAO.select() for book in books: if book.id == p_book.id: logger.debug(book) logger.debug(p_book) book.bookname = p_book.bookname book.booktype = p_book.booktype book.author = p_book.author book.state = p_book.state book.is_borrow = p_book.is_borrow book.createuser = p_book.createuser logger.info("UPDTE book:") logger.info(book) break with open(BookinfoDAO.FILE_NAME, 'w', encoding=SuperModel.FILE_ENCODING) as wf: for book in books: wf.write(book.to_file_str()) else: raise ValueError("图书信息错误")
def do_action(self): if len(self.PAGE_ACTIONS) == 0: logger.warning("页面未定义actions") return elif len(self.PAGE_ACTIONS) == 1: action_str = self.PAGE_ACTIONS[0]["ACTION"] else: command_line = "" for action in self.PAGE_ACTIONS: if "AUTH" in action and not self.user.is_admin: continue if command_line: command_line += ',' command_line += f'{action["DESC"]}({action["KEY"].upper()})' logger.debug(command_line) print("您可以选择如下操作:") if self.home_page: print("首页(H),", end='') if self.prev_page and self.prev_page is not self.home_page: print("上一页(B),", end='') print(command_line) actions = [d["KEY"] for d in self.PAGE_ACTIONS] + ['h', 'b'] while True: command = input("请输入:") if command.lower() in actions: logger.debug(f"用户输入的命令是:{command}") break else: print("输入有误,请重新输入...") # 回首页 if command.lower() == 'h': self.home_page.show() if command.lower() == 'b': self.prev_page.show() # 根据用户的指令获取 action 字符串 for action in self.PAGE_ACTIONS: if command.lower() == action["KEY"].lower(): action_str = action["ACTION"] logger.debug("找到指令:" + action_str) break # 反射调用 登录、注册、退出等方法 module, funcname = action_str.split('.') module = __import__("actions." + module, fromlist=True) if hasattr(module, funcname): logger.info(f">>> action: {action_str}") action_func = getattr(module, funcname) action_func(self) else: print(f"指令{command}的{funcname}方法未定义")
def do_register(page): """ @param: page: 当前页面,类似 self """ username = get_input(message="请输入用户名(3-16位):", check_re=r"^\w{3,16}$") logger.debug(f"用户名:{username}") nickname = get_input(message="请输入昵称(1-10位):", check_re=r"^\S{1,10}$") logger.debug(f"昵称:{nickname}") f_password = get_input(message="请输入密码(6-12位):", check_re=r"^\w{6,12}$", is_password=True) a_password = get_input(message="请确认密码:", warning="两次输入的密码不一致,请重新输入", check_re=f"^{f_password}$", is_password=True) logger.debug(f"密码:{a_password}") phone = get_input(message="请输入手机号:", check_re=r"^1[3-9]\d{9}$") logger.debug(f"手机号:{phone}") # 5. 输出信息 print('*' * 50) print("您输入的信息如下:") print("用户名:" + username) print("昵称:" + nickname) print("密码:" + '*' * len(a_password)) print("手机号:" + phone) print('*' * 50) # 6. 确认信息 confirm = get_input(message="确认注册(Y),取消注册(N),重新注册(R):", check_re=r"^[YyNnRr]$") logger.debug(f"确认结果:{confirm}") # 如果用户确认注册,将用户的个人信息保存到文件中 if confirm.lower() == 'y': # 计算用户 id try: user = Userinfo() user.username = username user.nickname = nickname user.password = a_password user.phone = phone UserinfoDAO.insert(user) logger.debug("完成注册,用户信息已保存") input("注册成功...按任意键继续") WelcomePage().show() except Exception as e: logger.exception(e) # 会将 trace 写入日志 input(str(e) + "...按任意键继续") page.show() elif confirm.lower() == 'n': # 放弃注册,回到欢迎页面 WelcomePage().show() elif confirm.lower() == 'r': # 重新输入的时候,再次展示 register 页面的 show() page.show() else: # 为了健壮性 print("您输入的指令不支持...请联系管理员") WelcomePage().show()
INFO logging.info() 20 動作情報表示用 DEBUG logging.debug() 10 詳細な情報表示用 NOTSET 0 全てを出力。基本的に設定用の値 フォーマット 概要 %(asctime)s 実行時刻 %(filename)s ファイル名 %(funcName)s 関数名 %(levelname)s DEBUG、INFO等のレベル名 %(lineno)d 行番号 %(name)s 呼びだしたログの定義名 %(module)s モジュール名 %(message)s ログメッセージ %(process)d プロセスID %(thread)d スレッドID Created by 0160929 on 2016/02/04 14:37 """ __version__ = '0.0' import os import sys from applogger import logger for i in range(10000): logger.error("Error!") # 40 重大な問題用 logger.warning("Warning..") # 30 実行機能で問題が発生した場合用 logger.info("Hello Logger :D") # 20 動作情報表示用 logger.debug("Hello Logger is run...") # 10 詳細な情報表示用