def show_question(question_id): question = data_manager.select_sql('question', clause='WHERE', condition=['id', '=', question_id]) answers = data_manager.select_sql( 'answer', clause='WHERE', condition=['question_id', '=', question_id]) comments = data_manager.select_sql( 'comment', clause='WHERE', condition=['question_id', '=', question_id]) comment_head = data_manager.get_table_head('comment') if not answers: answers = [{'Answers': 'This question doesn\'t have any answer yet.'}] if not comments: comment_head = ['Comments'] comments = [{'Comments': 'This answer doesn\'t have any comment yet.'}] question[0]['view_number'] += 1 data_manager.update_sql(table='question', column='view_number', update_value=question[0]['view_number'], update_condition=f'id={question_id}') return render_template('display_question.html', question=question, answers=answers, comments=comments, question_id=question_id, comment_head=comment_head)
def edit_comment(comment_id): table_head = data_manager.get_table_head('comment') comment = data_manager.select_sql(table='comment', clause='WHERE', condition=['id', '=', comment_id]) if comment[0]['answer_id'] is None: redirect_table = 'question' redirect_id = comment[0]['question_id'] else: redirect_table = 'answer' redirect_id = comment[0]['answer_id'] print(comment) if request.method == 'POST': for column_name, element in request.form.items(): data_manager.update_sql('comment', column_name, element, update_condition=f'id={comment_id}') data_manager.update_sql('comment', 'edited_count', comment[0]['edited_count'] + 1, update_condition=f'id={comment_id}') return redirect(f'/{redirect_table}/{redirect_id}') return render_template('update_comment.html', table_head=table_head, comment=comment, redirect_id=redirect_id, redirect_table=redirect_table)
def edit_answer(answer_id): table_head = data_manager.get_table_head('answer') answer = data_manager.select_sql('answer', clause='WHERE', condition=['id', '=', answer_id]) if request.method == 'POST': for column_name, element in request.form.items(): data_manager.update_sql('answer', column_name, element, update_condition=f'id={answer_id}') return redirect(f'/answer/{answer_id}') return render_template('update_answer.html', table_head=table_head, answer=answer, answer_id=answer_id)
def edit_question(question_id): table_head = data_manager.get_table_head('question') question = data_manager.select_sql('question', clause='WHERE', condition=['id', '=', question_id]) if request.method == 'POST': for column_name, element in request.form.items(): data_manager.update_sql('question', column_name, element, update_condition=f'id={question_id}') return redirect(f'/question/{question_id}') return render_template('update_question.html', table_head=table_head, question=question, question_id=question_id)
def vote(id_, vote_direction, table): table_data = data_manager.select_sql(table, clause='WHERE', condition=['id', '=', id_]) if vote_direction == 'up': table_data[0]['vote_number'] += 1 elif vote_direction == 'down': table_data[0]['vote_number'] -= 1 data_manager.update_sql(table=table, column='vote_number', update_value=table_data[0]['vote_number'], update_condition=f'id={id_}') if table == 'answer': id_ = table_data[0]['question_id'] question = data_manager.select_sql('question', clause='WHERE', condition=['id', '=', id_]) question[0]['view_number'] -= 1 data_manager.update_sql(table='question', column='view_number', update_value=question[0]['view_number'], update_condition=f'id={id_}') return redirect(f'/question/{id_}')
def admin(password_admin): """ 管理员模块 """ global books_df print(border1) password = getpass.getpass(prompt="请输入【管理员密码】!退出请按【0】\n密码:") while password != "0" and password != password_admin: print(Fore.RED + "【密码错误!】") password = getpass.getpass(prompt="请输入密码!退出请按【0】\n密码:") if password == "0": return instruction = ("\n【管理员】请按指示进行相关操作:" "\n单本录入图书请按【1】" "\n批量录入图书请按【2】" "\n自动生成借书号请按【3】" "\n修改读者权限请按【4】" "\n设置还书期限请按【5】" "\n重置密码及登录信息请按【6】" "\n查看统计信息请按【7】" "\n查询书目完整信息请按【8】" "\n查询读者完整信息请按【9】" "\n恢复备份文件请按【10】" "\n退出请按【0】\n") choice = input_request(border1 + instruction) while choice != "0": if choice == "1": logger.info("单本录入图书") print(border1) # 备份数据 dm.update_sql() # 要求用户输入ISBN码,可以手动输入,也可以扫码枪输入 isbn = input_request("输入ISBN,按0退出\n") while isbn != "0": # 调用单本录入函数 book_info = book_info_entry_single(isbn) # 如果有返回信息,说明用户输入信息格式正确 if book_info: book_schema = [ "isbn", "title", "author", "publisher", "publish_date", "page_number", "price", "subject", "total_number", "call_no", "summary", "location" ] book_info_tuple = (book_info[col] for col in book_schema) # 如果ISBN已经存在,并且用户同意合并信息,则只更改已存在条目中书籍总数 if book_info["isbn"] in set(books_df["isbn"]): books_df.loc[ books_df[books_df["isbn"] == isbn].index, "total_number"] = book_info["total_number"] else: # 如果ISBN不存在,则插入新的信息 print(border2) if book_info["status"]: print( Fore.GREEN + "联网数据获得成功,数据源:{}".format(book_info["source"])) else: # 无论书籍信息是否获取成功,这一条信息都会被插入到excel中 print(Fore.RED + "联网数据获得失败,请打开“图书馆信息.xlsx”文件手动添加书籍信息。") record = pd.DataFrame([book_info_tuple], columns=book_schema) books_df = pd.concat([books_df, record], ignore_index=True, sort=False) # 更新excel dm.update_excel_library() print(border2) # 更新内存中的object book_to_obj(isbn, force=True).print_info() print(border2) isbn = input_request("输入ISBN,按0退出\n") elif choice == "2": # 逻辑比较难实现,暂时搁置 logger.info("批量录入图书") print(border1) dm.update_sql() book_info_entry_batch() elif choice == "3": logger.info("自动生成借书号") print(border1) dm.update_sql() reader_id_generater() print(Fore.GREEN + "【读者借书号成功生成!】") return True elif choice == "4": logger.info("修改读者权限") dm.update_sql() reader, _ = retrieve_reader_book(True, False) if reader: request = input_request(("\n开通读者借书权限请按【1】" "\n暂停读者借书权限请按【2】" "\n退出请按【0】\n")) while request not in ["1", "2", "0"]: request = input_request(("\n开通读者借书权限请按【1】" "\n暂停读者借书权限请按【2】" "\n退出请按【0】\n")) if request == "1": reader.reader_access_revise("开通") print(border2) reader.print_info(5) print(Fore.GREEN + "【读者权限修改成功!】") input("请按回车键返回。") elif request == "2": reader.reader_access_revise("暂停") print(border2) reader.print_info(5) print(Fore.GREEN + "【读者权限修改成功!】") input("请按回车键返回。") elif choice == "5": logger.info("设置还书期限") print(border1) student_days = input("【学生】借书期限(天):") teacher_days = input("【教师】借书期限(天):") confirm = "999" while confirm != "1" and confirm != "0": confirm = input_request(("该操作将设置读者借书期限,但【不会】影响书籍信息、读者信息和借阅记录。" "\n确认请按【1】,取消请按【0】")) if confirm == "1": meta_data["student_days"] = student_days # TODO meta_data["teacher_days"] = teacher_days meta_data.to_sql("Meta", dm.get_connection(), if_exists="replace", index=False) print(Fore.GREEN + "【读者借书期限设置成功!】") elif choice == "6": logger.info("重置密码及登录信息") print(border1) confirm = "999" while confirm != "1" and confirm != "0": confirm = input_request( ("该操作将重置软件登录信息及密码,但【不会】影响书籍信息、读者信息和借阅记录。" "\n确认请按【1】,取消请按【0】")) if confirm == "1": print(border2) meta_data["status"] = "0" meta_data.to_sql("Meta", dm.get_connection(), if_exists="replace", index=False) print(Fore.GREEN + "【密码及登录信息重置成功!】") return True elif choice == "7": logger.info("查看统计信息") print(border1) info_summary() input("请按回车键返回。") elif choice == "8": logger.info("查询书目完整信息") retrieve_reader_book(False, True, None) input("请按回车键返回。") elif choice == "9": logger.info("查询读者完整信息") retrieve_reader_book(True, False, None) input("请按回车键返回。") elif choice == "10": logger.info("恢复备份文件") print(border1) print("正在恢复文件中,请稍后...") dm.sql_to_excel() print(Fore.GREEN + "【文件恢复完成!】") print(Fore.GREEN + "【请返回到软件所在目录的“备份恢复”文件夹,用恢复所得文件覆盖原文件。】") return True else: print(Fore.RED + "【错误代码,请重新输!】") choice = input_request(border1 + instruction)