def userLogin(): username = request.json.get('username') password = request.json.get('password') if username is None or password is None: # 信息不完整 return dataIncomplete() if User.query.filter_by(username=username).count() == 0: # 账号不存在 return noneObject("账号不存在") user = User.query.filter_by(username=username).first() if user.checkPassword(password): token = generate_token(username) user.token = token db.session.commit() return jsonify( { 'status': 0, 'data': { 'username': username, 'userid': user.id, 'token': token } } ) else: # 密码错误 invalidToken("密码错误")
def deleteSubComment(): # 验证token token = request.json.get('token') user = User.query.filter_by(token=token).first() if user is None or certify_token(user.username, token) is False: # token无效 return invalidToken() subCommentId = request.json.get('subcommentid') subcomment = subComment.query.get(subCommentId) db.session.delete(subcomment) # 生成新token token = generate_token(user.username) user.token = token db.session.commit() return jsonify( { 'status':0, 'data': { 'token': token } } )
def deleteComment(): # 验证token token = request.json.get('token') user = User.query.filter_by(token=token).first() if user is None or certify_token(user.username, token) is False: # token无效 return invalidToken() commentId = request.json.get('commentid') comment = Comment.query.get(commentId) if comment.userId != user.id: # 没有权限 return permissionDenied() subComment.query.filter_by(parentCommentId=comment.id).delete() db.session.delete(comment) # 生成新token token = generate_token(user.username) user.token = token db.session.commit() return jsonify( { 'status':0, 'data': { 'token': token } } )
def userRegister(): username = request.json.get('username') password = request.json.get('password') if username is None or password is None: # 信息不完整 return dataIncomplete() if User.query.filter_by(username=username).count() != 0: # 用户名存在 return duplicateData() token = generate_token(username) user = User( username=username, token = token ) password = user.makePassword(password) user.password = password db.session.add(user) db.session.commit() return jsonify({ 'status':0, 'data': { 'username': username, 'token': token } })
def starSubComment(): # 验证token token = request.json.get('token') user = User.query.filter_by(token=token).first() if user is None or certify_token(user.username, token) is False: # token无效 return invalidToken() subCommentId = request.json.get('subcommentid') subcomment = Comment.query.get(subCommentId) if subcomment is None: # 没有该评论 return noneObject() userstars = userStar.query.filter_by(userId=user.id).all() check = 0 for userstar in userstars: if userstar.subCommentId == subcomment.id: check = 1 break if check: # 已经点赞了 permissionDenied("不能重复点赞") subcomment.star += 1 userstar = userStar( userId=user.id, commentType=1, subCommentId=subcomment.id ) db.session.add(userstar) # 生成新token token = generate_token(user.username) user.token = token db.session.commit() return jsonify( { 'status':0, 'data': { 'token': token } } )
def makeSubComment(): # 验证token token = request.json.get('token') user = User.query.filter_by(token=token).first() if user is None or certify_token(user.username, token) is False: # token无效 return invalidToken() text = request.json.get('text') parentId = request.json.get('parentid') if text is None or parentId is None: # 重要参数不能为空 return dataIncomplete() if Comment.query.get(parentId) is None: # 评论不存在 noneObject() subcmt = subComment( text=text, userId=user.id, uploadTime=datetime.now(), parentCommentId=parentId ) db.session.add(subcmt) # 生成新token token = generate_token(user.username) user.token = token db.session.commit() return jsonify( { 'status': 0, 'data': { 'id': subcmt.id, 'username': user.username, 'token': token, 'text': text, 'uploadTime': subcmt.uploadTime } } )
def makeComment(): # 验证token token = request.json.get('token') user = User.query.filter_by(token=token).first() print(token, user.username) if user is None or certify_token(user.username, token) is False: # token无效 return invalidToken() text = request.json.get('text') if text is None: # 重要参数不能为空 return dataIncomplete() # 提交评论 comment = Comment( text=text, userId=user.id, uploadTime=datetime.now() ) db.session.add(comment) # 生成新token token = generate_token(user.username) user.token=token db.session.commit() return jsonify( { 'status': 0, 'data': { 'id': comment.id, 'username': user.username, 'token': token, 'text': text, 'uploadTime': comment.uploadTime } } )