def forumdefshow(self, usertokenstr): # 帖子首页 session = DBSession() try: if session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertokenstr).count() > 0: lista = session.query(Forum.forum_id, Forum.forum_title, Forum.forum_publishtime, Forum.forum_userinfoid).order_by(desc(Forum.forum_publishtime)).all() listb = [] print(lista) for i in lista: item = {} forumpromulgator = session.query(User.user_name).filter_by(userinfo_id=i[4]).one() item["forum_id"] = i[0] item["forum_title"] = i[1] # 帖子标题 item["forum_publishtime"] = i[2] praisenum = session.query(Praise).filter_by(praise_forumid=i[0]).count() item["praise_number"] = praisenum collectnum = session.query(Comment).filter_by(comment_forumid=i[0]).count() item["comment_number"] = collectnum listb.append(item) return listb except Exception as a: print(a) return "False"
def tokendelete(self, usertoken): session = DBSession() try: session.query(Usertoken).filter_by( usertoken_str=usertoken).delete() session.commit() session.close() return True except Exception as a: print(a) return False
def delectforum(self,usertokenstr,forumid): session = DBSession() try: session.query(Forum).filter_by(forum_id=forumid).delete() session.commit() session.close() return True except Exception as a: print(a) return False
def changepassword(self, userid, usernewpassword): session = DBSession() try: session.query(User).filter_by(user_id=userid).update( {'user_password': usernewpassword}) session.commit() session.close() return True except Exception as a: print(a) return False
def cancleattention(self,usertokenstr,userid): #取消关注 session = DBSession() try: userid1 = session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertokenstr).one() session.query(Attention).filter(Attention.attention_userid==userid,Attention.attention_fansid==userid1[0]).delete() session.commit() session.close() return True except Exception as a: print(a) return False
def modifypassword(self, usertokenstr, usernewpassword): session = DBSession() a = Userorml() try: userid = a.getidbytoken(usertokenstr) session.query(User).filter_by(user_id=userid).update( {'user_password': usernewpassword}) session.commit() session.close() return True except Exception as a: print(a) return "False"
def publishforum(self,usertoken,forumtitle,forumdesp): session = DBSession() try: userid = session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertoken).one() userinfoid = session.query(Userinfo.userinfo_id).filter_by(userinfo_userid=userid[0]).one() nowtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) session.add(Forum(forum_userinfoid=userinfoid[0], forum_title=forumtitle, forum_publishtime=nowtime, forum_desp=forumdesp)) session.commit() session.close() return True except Exception as a: print(a) return "False"
def forumcommentpublish(self,usertoken,forumid,commentcontent):#发布帖子的评论 session = DBSession() try: userid = session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertoken).one() userinfoid = session.query(Userinfo.userinfo_id).filter_by(userinfo_userid=userid[0]).one() nowtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) session.add(Comment(comment_forumid=forumid, comment_userinfoid=userinfoid[0], comment_content=commentcontent, comment_time=nowtime)) session.commit() session.close() return True except Exception as a: print(a) return "False"
def adduser(self, username, userpassword, userquestion, useranswer): session = DBSession() try: if username != "" and not self.checkuserexist(username): session.add( User(user_name=username, user_password=userpassword, user_question=userquestion, user_answer=useranswer)) session.commit() session.close() # 数据库表连接后,操作后,要关闭 session = DBSession() userid = session.query( User.user_id).filter_by(user_name=username).one() session.add( Userinfo(userinfo_userid=userid[0])) # 通过userid链接两个表 session.commit() session.close() return True else: return False except Exception as a: print(a)
def getidbytoken(self, usertokenstr): # 重要 session = DBSession() try: userid = session.query(Usertoken.usertoken_userid).filter_by( usertoken_str=usertokenstr).one() return userid[0] except Exception as a: print(a)
def getidbyname(self, username): session = DBSession() try: userid = session.query( User.user_id).filter_by(user_name=username).one() return userid[0] except Exception as a: print(a)
def modifyforum(self,usertoken,forumid,forumtitle,forumdesp): session = DBSession() #bookclass表 try: #userid = session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertoken).one()会出现访问不到的错误 #print("$$$$$$$$$$$$$$$",userid) #usercollege = session.query(Userinfo.userinfo_college).filter_by(userinfo_userid=userid[0]).one() #print("&&&&&&&&&&&&&&&",usercollege[0]) nowtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) session.query(Forum).filter_by(forum_id=forumid).update({"forum_title":forumtitle, "forum_desp":forumdesp, "forum_publishtime":nowtime, }) session.commit() session.close() return True except Exception as a: print(a)
def judgeanswer(self, username, userquestion1, useranswer1): session = DBSession() try: print("$$$$$$$$$$$$$$$$$$$$$$4", userquestion1) print("%%%%%%%%%%%%%%%%%5", useranswer1) if session.query(User).filter_by( user_name=username, user_question=userquestion1).count() > 0: print("$$$$$$$$$$$$$$$$$$$$$$4", useranswer1) if session.query(User).filter_by( user_name=username, user_answer=useranswer1).count() > 0: return 3 return 1 return 2 except Exception as a: print(a) return "False"
def checkuserexist(self, username): session = DBSession() try: if session.query(User).filter_by(user_name=username).count() > 0: return True except Exception as a: print(a) return "False" else: return False
def modifyquestion(self, usertokenstr, usernewquestion, usernewanswer): session = DBSession() a = Userorml() try: userid = a.getidbytoken(usertokenstr) print(2222222222222) print(userid) session.query(User).filter_by(user_id=userid).update({ 'user_question': usernewquestion, 'user_answer': usernewanswer }) session.commit() session.close() return True except Exception as a: print(a) return "False"
def addattention(self,usertokenstr,userid): #增加关注 session = DBSession() try: userid1 = session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertokenstr).one() session.add(Attention(attention_userid=userid,attention_fansid=userid1[0])) session.commit() session.close() return True except Exception as a: print(a) return False
def getuserid(self, username): session = DBSession() try: userid = session.query( User.user_id).filter_by(user_name=username).all() item = {} item["user_id"] = userid[0] # .decode("utf-8") return item except Exception as a: print(a) return False
def userlogin(self, username, userpassword): # 通过账号密码登录 session = DBSession() try: if session.query(User).filter_by( user_name=username, user_password=userpassword).count() > 0: return True except Exception as a: print(a) return "False" else: return False
def ownattention(self,usertokenstr):#查看我的关注 session = DBSession() try: if session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertokenstr).count() > 0: userid = session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertokenstr).one() attentionid = session.query(Attention.attention_userid).filter_by(attention_fansid=userid[0]).all()#查找出所有的关注用户id listb = [] for j in attentionid: lista = session.query(Userinfo.userinfo_img,User.user_name,).filter_by(userinfo_userid=j[0]).all() print(lista) for i in lista: item = {} item["userinfo_img"] = i[0] item["userinfo_name"] = i[1] listb.append(item) return listb except Exception as a: print(a) return "False"
def Forumshowme(self,usertokenstr): #显示我的帖子 session = DBSession() try: if session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertokenstr).count() > 0: userid = session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertokenstr).one() userinfoid =session.query(Userinfo.userinfo_id).filter_by(userinfo_userid=userid[0]).one() lista = session.query(Forum.forum_id, Forum.forum_title, Forum.forum_publishtime,Forum.forum_userinfoid).filter_by(forum_userinfoid=userinfoid[0]).all() listb = [] print(lista) for i in lista: item = {} forumpromulgator = session.query(User.user_name).filter_by(userinfo_id=i[4]).one() item["forum_promulgator"] = forumpromulgator[0] item["forum_id"] = i[0] item["forum_title"] = i[1] item["forum_publishtime"] = i[2] praisenum = session.query(Praise).filter_by(praise_forumid=i[0]).count() item["praise_number"] = praisenum listb.append(item) listb.sort(key = lambda x:x["forum_publishtime"], reverse = True) return listb except Exception as a: print(a) return "False"
def forumpraise(self,usertoken,forumid,praisestatus): session = DBSession() try: userid = session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertoken).one() if int(praisestatus) == 1 and session.query(Forum).filter_by(forum_id = forumid).count()>0: session.query(Praise).filter_by(praise_forumid = forumid, praise_userid = userid[0]).delete() session.commit() session.close() return "0" elif int(praisestatus) == 0: if session.query(Praise).filter_by(praise_forumid =forumid,praise_userid = userid[0]).count()>0: return "False" else: nowtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) session.add(Praise(praise_forumid = forumid, praise_userid = userid[0])) session.commit() session.close() return "1" else: return "False" except Exception as a: print(a) return "False"
def showotherinfo(self,usertokenstr,userid): session = DBSession()#显示他人信息 try: if session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertokenstr).count() > 0: lista = session.query(Userinfo.userinfo_img,User.user_name,).filter_by(userinfo_userid=userid).all() listb = [] print(lista) for i in lista: item = {} item["userinfo_img"] = i[0] item["userinfo_name"] = i[1] attentionnum = session.query(Attention).filter_by(attention_fansid=userid).count() item["userinfo_attentionnum"] = attentionnum fansnum = session.query(Attention).filter_by(attention_userid=userid).count() item["userinfo_fansnum"] = fansnum listb.append(item) return listb except Exception as a: print(a) return "False"
def personalcencer(self,usertokenstr): session = DBSession() try: if session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertokenstr).count() > 0: userid = session.query(Usertoken.usertoken_userid).filter_by(usertoken_str=usertokenstr).one() lista = session.query(Userinfo.userinfo_img,).filter_by(userinfo_userid=userid[0]).all() listb = [] for i in lista: item = {} item["userinfo_img"] = i[0] item["userinfo_name"] = i[1] attentionnum = session.query(Attention).filter_by(attention_fansid=userid[0]).count() item["userinfo_attentionnum"] = attentionnum fansnum = session.query(Attention).filter_by(attention_userid=userid[0]).count() item["userinfo_fansnum"] = fansnum listb.append(item) return listb except Exception as a: print(a) return "False"
def forumallshow(self,usertoken,forumid): session = DBSession() if session.query(Usertoken.usertoken_userid).filter_by(usertoken_str = usertoken).count()>0: try: userinfoid = session.query(Forum.forum_userinfoid).filter_by(forum_id=forumid).one() userinfoimg = session.query(Userinfo.userinfo_img).filter_by(userinfo_id = userinfoid[0]).one() userinfoname = session.query(User.user_name).filter_by(userinfo_id = userinfoid[0]).one() item = {} lista = [] lista = session.query(Forum.forum_id,Forum.forum_title,Forum.forum_publishtime,Forum.forum_desp).filter_by(forum_id=forumid).all() for i in lista: item["forum_id"] = i[0] item["forum_userimg"] = userinfoimg[0] item["forum_username"] = userinfoname[0] item["forum_title"] = i[1] item["forum_publishtime"] = i[2] item["forum_desp"] = i[3] praisenum = session.query(Praise).filter_by(praise_forumid=forumid).count() item["praise_number"] = praisenum listc = session.query(Comment.comment_id,Comment.comment_content,Comment.comment_time,Comment.comment_userinfoid).filter_by(comment_forumid = forumid).all() commentlist = [] for j in listc: comer = {} comer["comment_id"] = j[0] name = session.query(User.user_name).filter_by(userinfo_id=j[3]).one() comer["comment_name"] = name[0] img = session.query(Userinfo.userinfo_img).filter_by(userinfo_id=j[3]).one() comer["comment_userimg"] = img[0] comer["comment_content"] = j[1] comer["comment_time"] = j[2] commentlist.append(comer) commentlist.sort(key = lambda x : x['comment_time'],reverse=False) # item["book_comments"] = commentlist # return item except Exception as a: print(a) return "False"
from orml.dbbase import DBSession from dbmodels.usermodel import User from dbmodels.usertokenmodel import Usertoken from dbmodels.userinfomodel import Userinfo from dbmodels.picturemodel import Picture from dbmodels.praisemodel import Praise from dbmodels.commentmodel import Comment from dbmodels.forummodel import Forum from dbmodels.attentionmodel import Attention from dbmodels.fansmodel import Fans session = DBSession() if session.query(User).count() == 0: userlist = [ User(user_id=1, user_name="小红", user_password="******", user_question="我多少岁", user_answer="18"), User(user_id=2, user_name="小明", user_password="******", user_question="我来自哪里", user_answer="贵阳"), User(user_id=3, user_name="小蓝", user_password="******", user_question="我读哪个大学", user_answer="贵大"), User(user_id=4,