无法实现审批新闻
我的代码无法实现审批新闻,我粘贴了app.py news_dao.py以及news_service.py的代码, 请老师帮忙测试下,看看问题出在哪 app.py from colorama import Fore,Style,init init() from getpass import getpass from service.user_service import UserService from service.news_service import NewsService from service.role_service import RoleService from service.type_service import TypeService import os import sys import time __user_service=UserService() __news_service=NewsService() __role_service=RoleService() __type_service=TypeService() while True: os.system("cls") print(Fore.LIGHTBLUE_EX,"\n\t======================") print(Fore.LIGHTBLUE_EX, "\n\t欢迎使用新闻管理系统") print(Fore.LIGHTBLUE_EX, "\n\t======================") print(Fore.LIGHTGREEN_EX, "\n\t1.登入系统") print(Fore.LIGHTGREEN_EX, "\n\t2.退出系统") print(Style.RESET_ALL) opt=input("\n\t输入操作编号:") if opt=="1": username=input("\n\t用户名:") password=getpass("\n\t密码:") result=__user_service.login(username,password) #登入成功 if result==True: #查询角色 role=__user_service.search_user_role(username) while True: os.system("cls") if role=="新闻编辑": print(Fore.LIGHTGREEN_EX, "\n\t1.发表新闻") print(Fore.LIGHTGREEN_EX, "\n\t2.编辑新闻") print(Fore.LIGHTRED_EX, "\n\tback.退出登入") print(Fore.LIGHTRED_EX, "\n\texit.退出系统") print(Style.RESET_ALL) opt = input("\n\t输入操作编号:") if opt=="1": os.system("cls") title=input("\n\t新闻标题") userid=__user_service.search_user_id(username) result=__type_service.search_list() for index in range(len(result)): one = result[index] print(Fore.LIGHTBLUE_EX, "\n\t%d.%s" % (index + 1, one[1])) print(Style.RESET_ALL) opt = input("\n\t类型编号:") type_id = result[int(opt) - 1][0] # TODO 新闻正文内容 content_id=100 is_top=input("\n\t置顶级别(0-5):") is_commit=input("\n\t是否提交(Y/N):") if is_commit=="Y" or is_commit=="y": __news_service.insert(title,userid,type_id,content_id,is_top) print("\n\t保存(3秒自动返回)") time.sleep(3) elif opt=="2": page = 1 while True: os.system("cls") count_page = __news_service.search_count_page() result = __news_service.search_list(page) for index in range(len(result)): one = result[index] print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s\t%s" % (index + 1, one[1], one[2], one[3])) print(Fore.LIGHTBLUE_EX_EX, "\n\t---------------------") print(Fore.LIGHTBLUE_EX, "\n\t%d/%d" % (page, count_page)) print(Fore.LIGHTBLUE_EX_EX, "\n\t---------------------") print(Fore.LIGHTRED_EX, "\n\tback.返回上一层") print(Fore.LIGHTRED_EX, "\n\tprev.上一页") print(Fore.LIGHTRED_EX, "\n\tnext.下一页") print(Style.RESET_ALL) opt = input("\n\t输入操作编号:") if opt == "back": break elif opt == "prev" and page > 1: page -= 1 elif opt == "next" and page < count_page: page += 1 elif int(opt) >= 1 and int(opt) <= 10: news_id = result[int(opt) - 1][0] result=__news_service.search_by_id(news_id) title=result[0] type=result[1] is_top=result[2] print("\n\t新闻原标题:%s" %(title)) new_title=input("\n\t新标题:") print("\n\t新闻原类型:%s" %(type)) result = __type_service.search_list() for index in range(len(result)): one = result[index] print(Fore.LIGHTBLUE_EX, "\n\t%d.%s" % (index + 1, one[1])) print(Style.RESET_ALL) opt = input("\n\t类型编号:") type_id = result[int(opt) - 1][0] #TODO 输入新闻内容 content_id=100 print("\n\t新置顶级别:%s" % (is_top)) new_is_top=input("\n\t置顶级别(0-5):") is_commit=input("\n\t是否提交?(Y/N):") if is_commit=="Y" or is_commit=="y": __news_service.update(news_id,new_title,type_id,content_id,new_is_top) print("\n\t保存成功(3秒自动返回)") time.sleep(3) elif opt == "back": break elif opt == "exit": sys.exit(0) elif role == "管理员": print(Fore.LIGHTGREEN_EX,"\n\t1.新闻管理") print(Fore.LIGHTGREEN_EX,"\n\t2.用户管理") print(Fore.LIGHTRED_EX,"\n\tback.退出登入") print(Fore.LIGHTRED_EX,"\n\texit.退出系统") print(Style.RESET_ALL) opt = input("\n\t输入操作编号:") if opt=="1": while True: os.system("cls") print(Fore.LIGHTGREEN_EX, "\n\t1.审批新闻") print(Fore.LIGHTGREEN_EX, "\n\t2.删除新闻") print(Fore.LIGHTRED_EX, "\n\tback.返回上一层") print(Style.RESET_ALL) opt = input("\n\t输入操作编号:") if opt=="1": page=1 while True: os.system("cls") count_page=__news_service.search_unreview_count_page() result=__news_service.search_unreview_list(page) for index in range(len(result)): one=result[index] print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s\t%s"%(index+1,one[1],one[2],one[3])) print(Fore.LIGHTBLUE_EX, "\n\t---------------------") print(Fore.LIGHTBLUE_EX,"\n\t%d/%d"%(page,count_page)) print(Fore.LIGHTBLUE_EX, "\n\t---------------------") print(Fore.LIGHTRED_EX, "\n\tback.返回上一层") print(Fore.LIGHTRED_EX, "\n\tprev.上一页") print(Fore.LIGHTRED_EX, "\n\tnext.下一页") print(Style.RESET_ALL) opt=input("\n\t输入操作编号:") if opt=="back": break elif opt=="prev" and page>1: page-=1 elif opt=="next" and page<count_page: page+=1 elif int(opt)>=1 and int(opt)<=10: news_id=result[int(opt)-1] __news_service.update_unreview_news(news_id) result = __news_service.search_cache(news_id) title = result[0] username = result[1] type = result[2] content_id = result[3] #TODO 查找新闻正文 content = "100" is_top = result[4] create_time = str(result[5]) __news_service.cache_news(news_id,title,username,type,content,is_top,create_time) elif opt=="back": break elif opt == "2": page = 1 while True: os.system("cls") count_page = __news_service.search_count_page() result = __news_service.search_list(page) for index in range(len(result)): one = result[index] print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s\t%s" % (index + 1, one[1], one[2], one[3])) print(Fore.LIGHTBLUE_EX_EX, "\n\t---------------------") print(Fore.LIGHTBLUE_EX, "\n\t%d/%d" % (page, count_page)) print(Fore.LIGHTBLUE_EX_EX, "\n\t---------------------") print(Fore.LIGHTRED_EX, "\n\tback.返回上一层") print(Fore.LIGHTRED_EX, "\n\tprev.上一页") print(Fore.LIGHTRED_EX, "\n\tnext.下一页") print(Style.RESET_ALL) opt = input("\n\t输入操作编号:") if opt == "back": break elif opt == "prev" and page > 1: page -= 1 elif opt == "next" and page < count_page: page += 1 elif int(opt) >= 1 and int(opt) <= 10: news_id = result[int(opt) - 1][0] __news_service.delete_by_id(news_id) __news_service.delete_cache(news_id) elif opt=="2": while True: os.system("cls") print(Fore.LIGHTGREEN_EX, "\n\t1.添加用户") print(Fore.LIGHTGREEN_EX, "\n\t2.修改用户") print(Fore.LIGHTGREEN_EX, "\n\t3.删除用户") print(Fore.LIGHTRED_EX, "\n\tback.返回上一层") print(Style.RESET_ALL) opt = input("\n\t输入操作编号:") if opt=="back": break elif opt=="1": os.system("cls") username = input("\n\t用户名:") password = getpass("\n\t密码:") repassword = getpass("\n\t重复密码:") if password != repassword: print("\n\t两次密码不一致(3s自动返回)") time.sleep(3) continue email=input("\n\t邮箱:") result=__role_service.search_list() for index in range(len(result)): one = result[index] print(Fore.LIGHTBLUE_EX,"\n\t%d.%s"%(index+1,one[1])) print(Style.RESET_ALL) opt=input("\n\t角色编号:") role_id=result[int(opt)-1][0] __user_service.insert(username,password,email,role_id) print("\n\t保存成功(3s自动返回)") time.sleep(3) elif opt=="2": page = 1 while True: os.system("cls") count_page = __user_service.search_count_page() result = __user_service.search_list(page) for index in range(len(result)): one = result[index] print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s" % (index + 1, one[1], one[2])) print(Fore.LIGHTBLUE_EX, "\n\t---------------------") print(Fore.LIGHTBLUE_EX, "\n\t%d/%d" % (page, count_page)) print(Fore.LIGHTBLUE_EX, "\n\t---------------------") print(Fore.LIGHTRED_EX, "\n\tback.返回上一层") print(Fore.LIGHTRED_EX, "\n\tprev.上一页") print(Fore.LIGHTRED_EX, "\n\tnext.下一页") print(Style.RESET_ALL) opt = input("\n\t输入操作编号:") if opt == "back": break elif opt == "prev" and page > 1: page -= 1 elif opt == "next" and page < count_page: page += 1 elif int(opt) >= 1 and int(opt) <= 10: os.system("cls") user_id=result[int(opt)-1][0] username = input("\n\t新用户名:") password = getpass("\n\t新密码:") repassword = getpass("\n\t再次输入密码:") if password != repassword: print(Fore.LIGHTRED_EX,"\n\t两次密码不一致(3s自动返回)") print(Style.RESET_ALL) time.sleep(3) break email = input("\n\t新邮箱:") result = __role_service.search_list() for index in range(len(result)): one = result[index] print(Fore.LIGHTBLUE_EX, "\n\t%d.%s" % (index + 1, one[1])) print(Style.RESET_ALL) opt = input("\n\t角色编号:") role_id = result[int(opt) - 1][0] opt=input("\n\t是否保存(Y/N)") if opt == "Y" or opt == "y": __user_service.update(user_id,username,password,email,role_id) print("\n\t保存成功(3s自动返回)") time.sleep(3) elif opt=="3": page = 1 while True: os.system("cls") count_page = __user_service.search_count_page() result = __user_service.search_list(page) for index in range(len(result)): one = result[index] print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s" % (index + 1, one[1], one[2])) print(Fore.LIGHTBLUE_EX, "\n\t---------------------") print(Fore.LIGHTBLUE_EX, "\n\t%d/%d" % (page, count_page)) print(Fore.LIGHTBLUE_EX, "\n\t---------------------") print(Fore.LIGHTRED_EX, "\n\tback.返回上一层") print(Fore.LIGHTRED_EX, "\n\tprev.上一页") print(Fore.LIGHTRED_EX, "\n\tnext.下一页") print(Style.RESET_ALL) opt = input("\n\t输入操作编号:") if opt == "back": break elif opt == "prev" and page > 1: page -= 1 elif opt == "next" and page < count_page: page += 1 elif int(opt) >= 1 and int(opt) <= 10: os.system("cls") user_id=result[int(opt)-1][0] __user_service.delete_by_id(user_id) print("\n\t删除成功(1s自动返回)") time.sleep(1) if opt=="back": break elif opt=="exit": sys.exit(0) else: print("\n\t登入失败(3秒自动返回)") time.sleep(3) elif opt=="2": # 安全退出系统 sys.exit(0) news_dao.py from db.mysql_db import pool class NewsDao: # 查询待审批新闻列表 def search_unreview_list(self,page): try: con=pool.get_connection() cursor=con.cursor() sql="SELECT n.id,n.title,t.type,u.username "\ "FROM t_news n JOIN t_type t ON n.type_id=t.id "\ "JOIN t_user u ON n.editor_id=u.id "\ "WHERE n.state=%s "\ "ORDER BY n.create_time DESC "\ "LIMIT %s,%s" cursor.execute(sql,("待审批",(page-1)*10, 10)) result=cursor.fetchall() # result两个元组组成的列表 return result except Exception as e: print(e) finally: if "con" in dir(): con.close() # 查询待审批新闻总页数 def search_unreview_count_page(self): try: con=pool.get_connection() cursor=con.cursor() sql="SELECT CEIL(COUNT(*)/10) FROM t_news WHERE state=%s" cursor.execute(sql,["待审批"]) count_page=cursor.fetchone()[0] return count_page except Exception as e: print(e) finally: if "con" in dir(): con.close() # 审批新闻 def update_unreview_news(self,id): try: con=pool.get_connection() con.start_transaction() cursor=con.cursor() sql="UPDATE t_news SET state=%S WHERE id=%s" cursor.execute(sql,["已审批",id]) con.commit() except Exception as e: if "con" in dir(): con.rollback() print(e) finally: if "con" in dir(): con.close() # 查询新闻列表 def search_list(self,page): try: con=pool.get_connection() cursor=con.cursor() sql="SELECT "\ "FROM t_news n JOIN t_type t ON n.type_id=t.id "\ "JOIN t_user u ON n.editor_id=u.id "\ "ORDER BY n.create_time DESC "\ "LIMIT %s,%s" cursor.execute(sql,((page-1)*10,10)) result=cursor.fetchall() return result except Exception as e: print(e) finally: if "con" in dir(): con.close() # 查询新闻总页数 def search_count_page(self): try: con = pool.get_connection() cursor = con.cursor() sql = "SELECT CEIL(COUNT(*)/10) FROM t_news" cursor.execute(sql) count_page = cursor.fetchone()[0] return count_page except Exception as e: print(e) finally: if "con" in dir(): con.close() # 删除新闻 def delete_by_id(self,id): try: con=pool.get_connection() con.start_transaction() cursor=con.cursor() sql="DELETE FROM t_news WHERE id=%S" cursor.execute(sql,[id]) con.commit() except Exception as e: if "con" in dir(): con.rollback() print(e) finally: if "con" in dir(): con.close() # 添加新闻 def insert(self,title,editor_id,type_id,content_id,is_top): try: con = pool.get_connection() con.start_transaction() cursor = con.cursor() sql = "INSERT INTO t_news(title,editor_id,type_id,content_id,is_top,state) " \ "VALUES(%s,%s,%s,%s,%s,%s)" cursor.execute(sql, [title,editor_id,type_id,content_id,is_top,"待审批"]) con.commit() except Exception as e: if "con" in dir(): con.rollback() print(e) finally: if "con" in dir(): con.close() # 查找用于缓存的记录 def search_cache(self,id): try: con=pool.get_connection() cursor=con.cursor() sql="SELECT n.title,u.username,t.type,n.content_id, " \ "n.is_top,n.create_time " \ "FROM t_news n " \ "JOIN t_type t ON n.type_id=t.id " \ "JOIN t_user u ON n.editor_id=u.id " \ "WHERE n.id=%s" cursor.execute(sql,[id]) result=cursor.fetchone() return result except Exception as e: print(e) finally: if "con" in dir(): con.close() # 根据id查找新闻 def search_by_id(self,id): try: con = pool.get_connection() cursor = con.cursor() sql = "SELECT n.title,t.type,n.is_top" \ "FROM t_news n " \ "JOIN t_type t ON n.type_id=t.id " \ "WHERE n.id=%s" cursor.execute(sql, [id]) result = cursor.fetchone() return result except Exception as e: print(e) finally: if "con" in dir(): con.close() # 更改新闻 def update(self,id,title,type_id,content_id,is_top): try: con = pool.get_connection() con.start_transaction() cursor = con.cursor() sql = "UPDATE t_news SET title=%s,type_id=%s,content_id=%s, " \ "is_top=%s,state=%s,update_time=NOW() WHERE id=%s" cursor.execute(sql, (title,type_id,content_id,is_top,"待审批",id)) con.commit() except Exception as e: if "con" in dir(): con.rollback() print(e) finally: if "con" in dir(): con.close() news_service.py from db.news_dao import NewsDao from db.redis_news_dao import RedisNewsDao class NewsService: __news_dao=NewsDao() __redis_news_dao=RedisNewsDao() # 查询待审批新闻列表 def search_unreview_list(self,page): result=self.__news_dao.search_unreview_list(page) return result # 查询待审批新闻列表 def search_unreview_count_page(self): count_page=self.__news_dao.search_unreview_count_page() return count_page # 审批新闻 def update_unreview_news(self, id): self.__news_dao.update_unreview_news(id) # 查询新闻列表 def search_list(self,page): result=self.__news_dao.search_list(page) return result # 查询新闻总页数 def search_count_page(self): count_page=self.__news_dao.search_count_page() return count_page # 删除新闻 def delete_by_id(self, id): self.__news_dao.delete_by_id(id) # 添加新闻 def insert(self, title, editor_id, type_id, content_id, is_top): self.__news_dao.insert(title,editor_id,type_id,content_id,is_top) # 查找用于缓存的记录 def search_cache(self,id): result = self.__news_dao.search_cache(id) return result # 向redis保存缓存的新闻 def cache_news(self,id,title,username,type,content,is_top,create_time): self.__redis_news_dao.insert(id,title,username,type,content,is_top,create_time) #删除缓存的新闻 def delete_cache(self,id): self.__redis_news_dao.delete_cache(id) # 根据id查找新闻 def search_by_id(self,id): result=self.__news_dao.search_by_id(id) return result # 更改新闻 def update(self,id,title,type_id,content_id,is_top): self.__news_dao.update(id,title,type_id,content_id,is_top) self.delete_cache(id)
11
收起
正在回答 回答被采纳积分+1
1回答
2.Python操作三大数据库
- 参与学习 人
- 提交作业 625 份
- 解答问题 2669 个
数据库作为企业储存和管理数据的根本,掌握数据库是每个开发工程师必备的技能,本阶段带你学会用Python操作MySQL、Redis和MongoDB三大主流数据库。夯实数据库基础。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星