def getpublicinfo(cur, args): id = args.get('id') token = args.get('token') getid = args.get('getid') #参数完整性验证 if all([id, token, getid]) == False: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #连接数据库,获取信息 cur.execute( sql.SQL('SELECT * FROM userinfo ' 'WHERE {ID}=%s').format(ID=sql.Identifier("ID")), (getid, )) row = cur.fetchone() #如果用户存在 if row != None: data = { "Id": row[0], "Avatar": row[5], "Username": row[6], "Sex": row[7], "City": row[11], "Streak": row[26] } return returnmsg.success(data) #如果用户不存在 return returnmsg.error('用户不存在', 400)
def thumbup(cur, conn, args): id = args.get('id') token = args.get('token') momentid = args.get('momentid') #参数完整性验证 if all([id, token, momentid]) == False: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #数据库找到momentid, thumb+1 cur.execute( sql.SQL('UPDATE moments ' 'SET {Thumbs} = {Thumbs} + 1 ' 'WHERE {MomentID} = %s ' 'RETURNING {Thumbs}').format( Thumbs=sql.Identifier("Thumbs"), MomentID=sql.Identifier("MomentID")), (momentid, )) conn.commit() row = cur.fetchone() if row == None: return returnmsg.error("点赞的动态不存在", 400) return returnmsg.success({"Thumbs": row[0]})
def registerlogin(cur, conn, args): phone_num = args.get('phonenum') key = args.get('key') #参数完整性验证 if all([phone_num, key]) == False: return returnmsg.error('参数不完整', 400) #验证key if aescrypt.verify_key(phone_num, key) == False: return returnmsg.error('Key不合法', 400) #生成随机token并写入数据库,从数据库返回id,将id和token一并return id, token, needinfo = userToken.genToken(cur, conn, phone_num, None) data = {'Id': id, 'Token': token, 'Needinfo': needinfo} return returnmsg.success(data)
def wrapper(*args, **kwargs): try: return f(*args, **kwargs) except (DataError, InternalError): g.conn.rollback() return returnmsg.error("Database Error", 403) #尝试SQL注入或数据不规范时会引发数据库异常,返回异常信息。
def postmoment(cur, conn, json): try: id = json['Id'] token = json['Token'] text = json['Text'] pic = json['Pic'] #参数完整性验证 except KeyError: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #获取时间 #服务器为北京时间 now = time.strftime("%Y-%m-%d %H:%M:%S+08", time.localtime()) #数据库里新增一个moment cur.execute( sql.SQL('INSERT INTO moments ' '({ID}, {Time}, {Text}, {Pic}) ' 'VALUES ' '(%s, %s, %s, %s) ' 'RETURNING {MomentID}').format( ID=sql.Identifier("ID"), Time=sql.Identifier("Time"), Text=sql.Identifier("Text"), Pic=sql.Identifier("Pic"), MomentID=sql.Identifier("MomentID")), (id, now, text, pic)) conn.commit() momentid = cur.fetchone()[0] return returnmsg.success({"MomentID": momentid})
def postcomment(cur, conn, json): try: id = json['Id'] token = json['Token'] momentid = json['MomentID'] text = json['Text'] #参数完整性验证 except KeyError: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #获取时间 #服务器为北京时间 now = time.strftime("%Y-%m-%d %H:%M:%S+08", time.localtime()) #先检查是否存在动态 cur.execute( sql.SQL('SELECT {MomentID} FROM moments ' 'WHERE {MomentID} = %s').format( MomentID=sql.Identifier("MomentID")), (momentid, )) if cur.fetchone == None: return returnmsg.error("评论的动态不存在", 400) #连接数据库,插入评论 cur.execute( sql.SQL('INSERT INTO comments ' '({MomentID}, {ID}, {Time}, {Text}) ' 'VALUES ' '(%s, %s, %s, %s) ' 'RETURNING {CommentID}').format( MomentID=sql.Identifier("MomentID"), ID=sql.Identifier("ID"), Time=sql.Identifier("Time"), Text=sql.Identifier("Text"), CommentID=sql.Identifier("CommentID")), (momentid, id, now, text)) conn.commit() commentid = cur.fetchone()[0] return returnmsg.success({"CommentID": commentid})
def dailyscore(cur, args): id = args.get('id') token = args.get('token') #参数完整性验证 if all([id, token]) == False: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #获取日期 #服务器是北京时间 today = time.strftime("%Y-%m-%d", time.localtime()) #数据库部分 cur.execute( sql.SQL('SELECT i.{ID}, {Username}, {Avatar}, {HealthyScore} ' 'FROM userinfo as i, userdata as d ' 'WHERE i.{ID} = d.{ID} ' 'AND {City} = ( ' 'SELECT {City} FROM userinfo ' 'WHERE {ID} = %s) ' 'AND {Date} = %s ' 'ORDER BY {HealthyScore} DESC ' 'LIMIT 50').format(ID=sql.Identifier("ID"), Username=sql.Identifier("Username"), Avatar=sql.Identifier("Avatar"), HealthyScore=sql.Identifier("HealthyScore"), City=sql.Identifier("City"), Date=sql.Identifier("Date")), (id, today)) rows = cur.fetchall() if len(rows) == 0: return returnmsg.empty("今日还没有排行榜数据") ids = [] usernames = [] avatars = [] healthyscores = [] for row in rows: ids.append(row[0]) usernames.append(row[1]) avatars.append(row[2]) healthyscores.append(row[3]) return returnmsg.success({ "Ids": ids, "Usernames": usernames, "Avatars": avatars, "HealthyScores": healthyscores })
def updatetoken(cur, conn, args): id = args.get('id') token = args.get('token') #参数完整性验证 if all([id, token]) == False: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() id, token, needinfo = userToken.genToken(cur, conn, None, id) data = {'Token': token} return returnmsg.success(data)
def streak(cur, args): id = args.get('id') token = args.get('token') #参数完整性验证 if all([id, token]) == False: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #数据库部分 cur.execute( sql.SQL('SELECT {ID}, {Username}, {Avatar}, {Streak} ' 'FROM userinfo ' 'WHERE {City} = ( ' 'SELECT {City} FROM userinfo ' 'WHERE {ID} = %s) ' 'ORDER BY {Streak} DESC ' 'LIMIT 50').format(ID=sql.Identifier("ID"), Username=sql.Identifier("Username"), Avatar=sql.Identifier("Avatar"), Streak=sql.Identifier("Streak"), City=sql.Identifier("City")), (id, )) rows = cur.fetchall() if len(rows) == 0: return returnmsg.empty("今日还没有排行榜数据") ids = [] usernames = [] avatars = [] streak = [] for row in rows: ids.append(row[0]) usernames.append(row[1]) avatars.append(row[2]) streak.append(row[3]) return returnmsg.success({ "Ids": ids, "Usernames": usernames, "Avatars": avatars, "Streak": streak })
def getselfinfo(cur, args): id = args.get('id') token = args.get('token') #参数完整性验证 if all([id, token]) == False: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() cur.execute( sql.SQL('SELECT * FROM userinfo ' 'WHERE {ID}=%s').format(ID=sql.Identifier("ID")), (id, )) row = cur.fetchone() #组装数据 data = { "PhoneNum": row[4], "Avatar": row[5], "Username": row[6], "Sex": row[7], "Height": row[8], "Weight": row[9], "Birth": row[10].strftime("%Y-%m-%d"), "City": row[11], "SkinType": row[12], "HeatQuantityDemand": row[13], "ProteinDemand": row[14], "CarbohydratesDemand": row[15], "FatDemand": row[16], "VitaminADemand": row[17], "VitaminB1Demand": row[18], "VitaminB2Demand": row[19], "VitaminB6Demand": row[20], "VitaminB12Demand": row[21], "VitaminCDemand": row[22], "VitaminDDemand": row[23], "VitaminEDemand": row[24], "VitaminKDemand": row[25], "Streak": row[26] } return returnmsg.success(data)
def getmoment(cur, args): id = args.get('id') token = args.get('token') momentid = args.get('momentid') #参数完整性验证 if all([id, token, momentid]) == False: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #从数据库中获取momentid的数据 cur.execute( sql.SQL( 'SELECT m.{ID}, {MomentID}, {Time}, {Text}, {Pic}, ' '{Thumbs}, {Username}, {Avatar}, {Streak} ' 'FROM moments as m, userinfo as i ' 'WHERE m.{ID} = i.{ID} and {MomentID} = %s' ).format( ID=sql.Identifier("ID"), MomentID=sql.Identifier("MomentID"), Time=sql.Identifier("Time"), Text=sql.Identifier("Text"), Pic=sql.Identifier("Pic"), Thumbs=sql.Identifier("Thumbs"), Username=sql.Identifier("Username"), Avatar=sql.Identifier("Avatar"), Streak=sql.Identifier("Streak") ),(momentid,) ) info_row = cur.fetchone() if info_row == None: return returnmsg.empty("无动态数据") #查询评论数据 cur.execute( sql.SQL( 'SELECT {MomentID}, {CommentID}, c.{ID}, ' '{Time}, {Text}, {Username} ' 'FROM comments as c, userinfo as i ' 'WHERE c.{ID} = i.{ID} and {MomentID} = %s' ).format( MomentID=sql.Identifier("MomentID"), CommentID=sql.Identifier("CommentID"), ID=sql.Identifier("ID"), Time=sql.Identifier("Time"), Text=sql.Identifier("Text"), Username=sql.Identifier("Username"), ),(momentid,) ) comment_rows = cur.fetchall() #comments集 comments = {} for row in comment_rows: comments.update( { row[1]: { "Id": row[2], "Username": row[5], "Text": row[4], "Time": row[3] } } ) return returnmsg.success( { "MomentID": info_row[1], "Id": info_row[0], "Time": info_row[2], "Text": info_row[3], "Pic": info_row[4], "Thumbs": info_row[5], "Username": info_row[6], "Avatar": info_row[7], "Streak": info_row[8], "Comments": comments } )
def getstatistic(cur, args): id = args.get('id') token = args.get('token') getall = args.get('getall') date = args.get('date') #参数完整性验证 if all([id, token, getall, date]) == False: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #根据getall的情况来获取数据 if getall == "true": cur.execute( sql.SQL('SELECT * FROM userdata ' 'WHERE {ID} = %s').format(ID=sql.Identifier("ID")), (id, )) elif getall == "false": cur.execute( sql.SQL('SELECT * FROM userdata ' 'WHERE {ID} = %s ' 'AND {Date} = %s').format(ID=sql.Identifier("ID"), Date=sql.Identifier("Date")), (id, date)) else: return returnmsg.error("getall值异常", 400) rows = cur.fetchall() #如果是空数据 if len(rows) == 0: return returnmsg.empty("无数据") data = {} for row in rows: data_day = { "HealthyState": row[2], "HealthyScore": row[3], "HeatQuantity": row[4], "HeatQuantityDiff": row[5], "Protein": row[6], "ProteinDiff": row[7], "Carbohydrates": row[8], "CarbohydratesDiff": row[9], "Fat": row[10], "FatDiff": row[11], "VitaminA": row[12], "VitaminADiff": row[13], "VitaminB1": row[14], "VitaminB1Diff": row[15], "VitaminB2": row[16], "VitaminB2Diff": row[17], "VitaminB6": row[18], "VitaminB6Diff": row[19], "VitaminB12": row[20], "VitaminB12Diff": row[21], "VitaminC": row[22], "VitaminCDiff": row[23], "VitaminD": row[24], "VitaminDDiff": row[25], "VitaminE": row[26], "VitaminEDiff": row[27], "VitaminK": row[28], "VitaminKDiff": row[29] } data.update({row[1].strftime("%Y-%m-%d"): data_day}) return returnmsg.success(data)
def poststatistic(cur, conn, json): #先解析json try: id = json['Id'] token = json['Token'] user_statistic = json['UserStatistic'] #参数完整性验证 except KeyError: return returnmsg.error('参数不完整', 400) #验证Token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #user_staticstic完整性验证 try: date = user_statistic['Date'] healthystate = user_statistic['HealthyState'] healthyscore = user_statistic['HealthyScore'] heatquantity = user_statistic['HeatQuantity'] heatquantitydiff = user_statistic['HeatQuantityDiff'] protein = user_statistic['Protein'] proteindiff = user_statistic['ProteinDiff'] carbohydrates = user_statistic['Carbohydrates'] carbohydratesdiff = user_statistic['CarbohydratesDiff'] fat = user_statistic['Fat'] fatdiff = user_statistic['FatDiff'] vitamina = user_statistic['VitaminA'] vitaminadiff = user_statistic['VitaminADiff'] vitaminb1 = user_statistic['VitaminB1'] vitaminb1diff = user_statistic['VitaminB1Diff'] vitaminb2 = user_statistic['VitaminB2'] vitaminb2diff = user_statistic['VitaminB2Diff'] vitaminb6 = user_statistic['VitaminB6'] vitaminb6diff = user_statistic['VitaminB6Diff'] vitaminb12 = user_statistic['VitaminB12'] vitaminb12diff = user_statistic['VitaminB12Diff'] vitaminc = user_statistic['VitaminC'] vitamincdiff = user_statistic['VitaminCDiff'] vitamind = user_statistic['VitaminD'] vitaminddiff = user_statistic['VitaminDDiff'] vitamine = user_statistic['VitaminE'] vitaminediff = user_statistic['VitaminEDiff'] vitamink = user_statistic['VitaminK'] vitaminkdiff = user_statistic['VitaminKDiff'] except KeyError: return returnmsg.error('UserStatistic参数不完整', 400) #将数据更新到表里 #判断日期是否存在,存在则更新,不存在则新建 cur.execute( sql.SQL( 'SELECT {ID} FROM userdata ' 'WHERE {ID} = %s ' 'AND {Date} = %s' ).format( ID=sql.Identifier("ID"), Date=sql.Identifier("Date") ),(id, date) ) #数据不存在 if cur.fetchone() == None: cur.execute( sql.SQL( 'INSERT INTO userdata ' '({ID}, {Date}, {HealthyState}, {HealthyScore}, {HeatQuantity}, {HeatQuantityDiff}, {Protein}, {ProteinDiff}, {Carbohydrates}, {CarbohydratesDiff}, {Fat}, {FatDiff}, {VitaminA}, {VitaminADiff}, {VitaminB1}, {VitaminB1Diff}, {VitaminB2}, {VitaminB2Diff}, {VitaminB6}, {VitaminB6Diff}, {VitaminB12}, {VitaminB12Diff}, {VitaminC}, {VitaminCDiff}, {VitaminD}, {VitaminDDiff}, {VitaminE}, {VitaminEDiff}, {VitaminK}, {VitaminKDiff}) ' 'VALUES ' '(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' ).format( ID=sql.Identifier("ID"), Date=sql.Identifier("Date"), HealthyState=sql.Identifier("HealthyState"), HealthyScore=sql.Identifier("HealthyScore"), HeatQuantity=sql.Identifier("HeatQuantity"), HeatQuantityDiff=sql.Identifier("HeatQuantityDiff"), Protein=sql.Identifier("Protein"), ProteinDiff=sql.Identifier("ProteinDiff"), Carbohydrates=sql.Identifier("Carbohydrates"), CarbohydratesDiff=sql.Identifier("CarbohydratesDiff"), Fat=sql.Identifier("Fat"), FatDiff=sql.Identifier("FatDiff"), VitaminA=sql.Identifier("VitaminA"), VitaminADiff=sql.Identifier("VitaminADiff"), VitaminB1=sql.Identifier("VitaminB1"), VitaminB1Diff=sql.Identifier("VitaminB1Diff"), VitaminB2=sql.Identifier("VitaminB2"), VitaminB2Diff=sql.Identifier("VitaminB2Diff"), VitaminB6=sql.Identifier("VitaminB6"), VitaminB6Diff=sql.Identifier("VitaminB6Diff"), VitaminB12=sql.Identifier("VitaminB12"), VitaminB12Diff=sql.Identifier("VitaminB12Diff"), VitaminC=sql.Identifier("VitaminC"), VitaminCDiff=sql.Identifier("VitaminCDiff"), VitaminD=sql.Identifier("VitaminD"), VitaminDDiff=sql.Identifier("VitaminDDiff"), VitaminE=sql.Identifier("VitaminE"), VitaminEDiff=sql.Identifier("VitaminEDiff"), VitaminK=sql.Identifier("VitaminK"), VitaminKDiff=sql.Identifier("VitaminKDiff") ),(id, date, healthystate, healthyscore, heatquantity, heatquantitydiff, protein, proteindiff, carbohydrates, carbohydratesdiff, fat, fatdiff, vitamina, vitaminadiff, vitaminb1, vitaminb1diff, vitaminb2, vitaminb2diff, vitaminb6, vitaminb6diff, vitaminb12, vitaminb12diff, vitaminc, vitamincdiff, vitamind, vitaminddiff, vitamine, vitaminediff, vitamink, vitaminkdiff) ) #数据存在,更新数据 else: cur.execute( sql.SQL( 'UPDATE userdata ' 'SET {HealthyState} = %s, ' '{HealthyScore} = %s, ' '{HeatQuantity} = %s, ' '{HeatQuantityDiff} = %s, ' '{Protein} = %s, ' '{ProteinDiff} = %s, ' '{Carbohydrates} = %s, ' '{CarbohydratesDiff} = %s, ' '{Fat} = %s, ' '{FatDiff} = %s, ' '{VitaminA} = %s, ' '{VitaminADiff} = %s, ' '{VitaminB1} = %s, ' '{VitaminB1Diff} = %s, ' '{VitaminB2} = %s, ' '{VitaminB2Diff} = %s, ' '{VitaminB6} = %s, ' '{VitaminB6Diff} = %s, ' '{VitaminB12} = %s, ' '{VitaminB12Diff} = %s, ' '{VitaminC} = %s, ' '{VitaminCDiff} = %s, ' '{VitaminD} = %s, ' '{VitaminDDiff} = %s, ' '{VitaminE} = %s, ' '{VitaminEDiff} = %s, ' '{VitaminK} = %s, ' '{VitaminKDiff} = %s ' 'WHERE {ID} = %s ' 'AND {Date} = %s' ).format( HealthyState=sql.Identifier("HealthyState"), HealthyScore=sql.Identifier("HealthyScore"), HeatQuantity=sql.Identifier("HeatQuantity"), HeatQuantityDiff=sql.Identifier("HeatQuantityDiff"), Protein=sql.Identifier("Protein"), ProteinDiff=sql.Identifier("ProteinDiff"), Carbohydrates=sql.Identifier("Carbohydrates"), CarbohydratesDiff=sql.Identifier("CarbohydratesDiff"), Fat=sql.Identifier("Fat"), FatDiff=sql.Identifier("FatDiff"), VitaminA=sql.Identifier("VitaminA"), VitaminADiff=sql.Identifier("VitaminADiff"), VitaminB1=sql.Identifier("VitaminB1"), VitaminB1Diff=sql.Identifier("VitaminB1Diff"), VitaminB2=sql.Identifier("VitaminB2"), VitaminB2Diff=sql.Identifier("VitaminB2Diff"), VitaminB6=sql.Identifier("VitaminB6"), VitaminB6Diff=sql.Identifier("VitaminB6Diff"), VitaminB12=sql.Identifier("VitaminB12"), VitaminB12Diff=sql.Identifier("VitaminB12Diff"), VitaminC=sql.Identifier("VitaminC"), VitaminCDiff=sql.Identifier("VitaminCDiff"), VitaminD=sql.Identifier("VitaminD"), VitaminDDiff=sql.Identifier("VitaminDDiff"), VitaminE=sql.Identifier("VitaminE"), VitaminEDiff=sql.Identifier("VitaminEDiff"), VitaminK=sql.Identifier("VitaminK"), VitaminKDiff=sql.Identifier("VitaminKDiff"), ID=sql.Identifier("ID"), Date=sql.Identifier("Date") ),(healthystate, healthyscore, heatquantity, heatquantitydiff, protein, proteindiff, carbohydrates, carbohydratesdiff, fat, fatdiff, vitamina, vitaminadiff, vitaminb1, vitaminb1diff, vitaminb2, vitaminb2diff, vitaminb6, vitaminb6diff, vitaminb12, vitaminb12diff, vitaminc, vitamincdiff, vitamind, vitaminddiff, vitamine, vitaminediff, vitamink, vitaminkdiff, id, date) ) conn.commit() return returnmsg.success({})
def updatedata(cur, conn, json): #先解析json try: id = json['Id'] token = json['Token'] user_data = json['UserData'] #参数完整性验证 except KeyError: return returnmsg.error('参数不完整', 400) #验证Token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #user_data完整性验证 try: avatar = user_data['Avatar'] username = user_data['Username'] sex = user_data['Sex'] height = user_data['Height'] weight = user_data['Weight'] birth = user_data['Birth'] city = user_data['City'] skintype = user_data['SkinType'] heatquantitydemand = user_data['HeatQuantityDemand'] proteindemand = user_data['ProteinDemand'] carbohydratesdemand = user_data['CarbohydratesDemand'] fatdemand = user_data['FatDemand'] vitaminademand = user_data['VitaminADemand'] vitaminb1demand = user_data['VitaminB1Demand'] vitaminb2demand = user_data['VitaminB2Demand'] vitaminb6demand = user_data['VitaminB6Demand'] vitaminb12demand = user_data['VitaminB12Demand'] vitamincdemand = user_data['VitaminCDemand'] vitaminddemand = user_data['VitaminDDemand'] vitaminedemand = user_data['VitaminEDemand'] vitaminkdemand = user_data['VitaminKDemand'] streak = user_data['Streak'] except KeyError: return returnmsg.error('UserData参数不完整', 400) #将数据更新到表里 cur.execute( sql.SQL('UPDATE userinfo ' 'SET {Avatar} = %s, ' '{Username} = %s, ' '{Sex} = %s, ' '{Height} = %s, ' '{Weight} = %s, ' '{Birth} = %s, ' '{City} = %s, ' '{SkinType} = %s, ' '{HeatQuantityDemand} = %s, ' '{ProteinDemand} = %s, ' '{CarbohydratesDemand} = %s, ' '{FatDemand} = %s, ' '{VitaminADemand} = %s, ' '{VitaminB1Demand} = %s, ' '{VitaminB2Demand} = %s, ' '{VitaminB6Demand} = %s, ' '{VitaminB12Demand} = %s, ' '{VitaminCDemand} = %s, ' '{VitaminDDemand} = %s, ' '{VitaminEDemand} = %s, ' '{VitaminKDemand} = %s, ' '{Streak} = %s ' 'WHERE {ID} = %s').format( Avatar=sql.Identifier("Avatar"), Username=sql.Identifier("Username"), Sex=sql.Identifier("Sex"), Height=sql.Identifier("Height"), Weight=sql.Identifier("Weight"), Birth=sql.Identifier("Birth"), City=sql.Identifier("City"), SkinType=sql.Identifier("SkinType"), HeatQuantityDemand=sql.Identifier("HeatQuantityDemand"), ProteinDemand=sql.Identifier("ProteinDemand"), CarbohydratesDemand=sql.Identifier("CarbohydratesDemand"), FatDemand=sql.Identifier("FatDemand"), VitaminADemand=sql.Identifier("VitaminADemand"), VitaminB1Demand=sql.Identifier("VitaminB1Demand"), VitaminB2Demand=sql.Identifier("VitaminB2Demand"), VitaminB6Demand=sql.Identifier("VitaminB6Demand"), VitaminB12Demand=sql.Identifier("VitaminB12Demand"), VitaminCDemand=sql.Identifier("VitaminCDemand"), VitaminDDemand=sql.Identifier("VitaminDDemand"), VitaminEDemand=sql.Identifier("VitaminEDemand"), VitaminKDemand=sql.Identifier("VitaminKDemand"), Streak=sql.Identifier("Streak"), ID=sql.Identifier("ID")), (avatar, username, sex, height, weight, birth, city, skintype, heatquantitydemand, proteindemand, carbohydratesdemand, fatdemand, vitaminademand, vitaminb1demand, vitaminb2demand, vitaminb6demand, vitaminb12demand, vitamincdemand, vitaminddemand, vitaminedemand, vitaminkdemand, streak, id)) conn.commit() return returnmsg.success({})
def getmomentsid(cur, args): id = args.get('id') token = args.get('token') getall = args.get('getall') getid = args.get('getid') #参数完整性验证 if all([id, token, getall, getid]) == False: return returnmsg.error('参数不完整', 400) #先验证token是否合法 if userToken.testToken(cur, id, token) == False: return returnmsg.tokeninvalid() #根据getall的情况来获取数据 #选择单用户的所有moments if getall == "false": #先查询用户是否存在 cur.execute( sql.SQL( 'SELECT {ID} FROM userinfo ' 'WHERE {ID} = %s' ).format( ID=sql.Identifier("ID") ),(getid,) ) if cur.fetchone() == None: return returnmsg.error("此用户不存在", 400) cur.execute( sql.SQL( 'SELECT {MomentID} FROM moments ' 'WHERE {ID} = %s ' 'ORDER BY {Time} DESC' ).format( MomentID=sql.Identifier("MomentID"), ID=sql.Identifier("ID"), Time=sql.Identifier("Time") ),(getid,) ) #所有用户的moments elif getall == "true": cur.execute( sql.SQL( 'SELECT {MomentID} FROM moments ' 'ORDER BY {Time} DESC' ).format( MomentID=sql.Identifier("MomentID"), Time=sql.Identifier("Time") ) ) else: return returnmsg.error("gerall值异常", 400) rows = cur.fetchall() #没有动态 if len(rows) == 0: return returnmsg.empty('无动态数据') momentsids = [] for row in rows: momentsids.append(row[0]) return returnmsg.success({"MomentsIDs": momentsids})