Exemplo n.º 1
0
def set_comment_like():
    """评论点赞"""
    if not g.user:
        return jsonify(errno=RET.SESSIONERR, errmsg="用户未登陆")

    # 获取参数
    comment_id = request.json.get("comment_id")
    news_id = request.json.get("news_id")
    action = request.json.get("action")

    # 判断参数
    if not all([comment_id, news_id, action]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")

    # 查询评论数据
    try:
        comment = Comment.query.get(comment_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据查询失败")

    if not comment:
        return jsonify(errno=RET.NODATA, errmsg="评论数据不存在")

    if action == "add":
        comment_like = CommentLike.query.filter(
            CommentLike.comment_id == comment_id,
            CommentLike.user_id == g.user.id).first()
        if not comment_like:
            comment_like = CommentLike()
            comment_like.comment_id = comment_id
            comment_like.user_id = g.user.id
            db.session.add(comment_like)
            # 增加点赞条数
            comment.like_count += 1
    else:
        # 删除点赞数据
        comment_like = CommentLike.query.filter(
            CommentLike.comment_id == comment_id,
            CommentLike.user_id == g.user.id).first()
        if comment_like:
            db.session.delete(comment_like)
            # 减少点赞条数
            comment.like_count -= 1

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="数据更新失败")

    return jsonify(errno=RET.OK, errmsg="操作成功")
Exemplo n.º 2
0
def like_comment(comment_id):
    commentLike = CommentLike(UserID=current_user.UserID, CommentID=comment_id)
    if db.session.add(commentLike):
        db.session.commit()
        return "success"
    else:
        return "fail"
Exemplo n.º 3
0
def generateData(numUser,numCategory,numBlog):
    User.fake(numUser)
    
    Follow.fake(numUser)
        
    Category.fake(numCategory)
    
    Blog.fake(numBlog,numUser)
    
    Comment.fake(numBlog,numUser)
    
    BlogCategory.fake(numBlog,numCategory*5)
    
    BlogLabel.fake(numBlog)
    
    BlogLike.fake(numBlog*20,numUser,numBlog)
    
    CommentLike.fake(numBlog*30,numUser,numBlog*8)
    
    Collection.fake(numBlog*10,numUser,numBlog)
Exemplo n.º 4
0
def unlike_comment(comment_id):
    if len(
            CommentLike.query.filter_by(UserID=current_user.UserID,
                                        CommentID=comment_id)) == 0:
        return "fail"
    commentLike = CommentLike(UserID=current_user.UserID, CommentID=comment_id)
    if db.session.delete(commentLike):
        db.session.commit()
        return "success"
    else:
        return "fail"
Exemplo n.º 5
0
def commentLike():
    likes = CommentLike.query.all()
    newLike = request.get_json()
    userId = newLike['userId']
    commentId = newLike['commentId']
    for like in likes:
        if int(like.userId) == int(userId) and int(
                like.commentId) == int(commentId):
            db.session.delete(like)
            db.session.commit()
            return ({"userId": like.userId, "commentId": like.commentId})

    new_commentLike = CommentLike(userId=userId, commentId=commentId)
    db.session.add(new_commentLike)
    db.session.commit()
    return (newLike)
Exemplo n.º 6
0
def seed_commentLikes():

    commentLike1 = CommentLike(userId=2, commentId=1)
    commentLike2 = CommentLike(userId=3, commentId=1)
    commentLike3 = CommentLike(userId=4, commentId=1)
    commentLike4 = CommentLike(userId=1, commentId=2)
    commentLike5 = CommentLike(userId=2, commentId=3)
    commentLike6 = CommentLike(userId=3, commentId=4)
    commentLike7 = CommentLike(userId=4, commentId=2)
    commentLike8 = CommentLike(userId=5, commentId=6)

    db.session.add(commentLike1)
    db.session.add(commentLike2)
    db.session.add(commentLike3)
    db.session.add(commentLike4)
    db.session.add(commentLike5)
    db.session.add(commentLike6)
    db.session.add(commentLike7)
    db.session.add(commentLike8)

    db.session.commit()
Exemplo n.º 7
0
def seed_commentLikes():

    first = CommentLike(
        userId = 1,
        commentId = 1
    )
    second = CommentLike(
        userId = 1,
        commentId = 2
    )
    third = CommentLike(
        userId = 3,
        commentId = 1
    )
    fourth = CommentLike(
        userId = 1,
        commentId = 2
    )
    fifth = CommentLike(
        userId = 2,
        commentId = 2
    )
    sixth = CommentLike(
        userId = 1,
        commentId = 1
    )
    seventh = CommentLike(
        userId = 1,
        commentId = 3
    )
    db.session.add(first)
    db.session.add(second)
    db.session.add(third)
    db.session.add(fourth)
    db.session.add(fifth)
    db.session.add(sixth)
    db.session.add(seventh)
    db.session.commit()
Exemplo n.º 8
0
def like_comment(commentId):
    commentLike = CommentLike(commentId=commentId, userId=current_user.id)
    db.session.add(commentLike)
    db.session.commit()
    return {'message': "Success"}
Exemplo n.º 9
0
def thumbs_up():
    from app.models import User, Comment, News, CommentLike, Category
    from app import db

    user_id = session.get("user_id")
    user = User.query.get(user_id)

    if not user_id:
        return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")

    # 获取参数
    comment_id = request.json.get("comment_id")
    # 触发事件
    action_things = request.json.get("action")

    # 判断
    if not all([comment_id, action_things]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有为空")
    # 取消和点赞触发事件
    if action_things not in ["add", "remove"]:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    try:
        comment_id = int(comment_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    # 查询被点赞的评论
    try:
        comment = Comment.query.get(comment_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库查询错误")

    # 判断
    if not comment:
        return jsonify(errno=RET.NODATA, errmsg="没有评论数据")

    if action_things == "add":
        # 选出评论模型
        comment_like_model = CommentLike.query.filter(
            CommentLike.user_id == user.id,
            CommentLike.comment_id == comment.id).first()

        if not comment_like_model:
            comment_like_model = CommentLike()
            comment_like_model.user_id = user.id
            comment_like_model.comment_id = comment.id
            # 往session中添加数据
            db.session.add(comment_like_model)
            comment.like_count += 1
    else:
        # 再次筛选评论模型
        comment_like_model = CommentLike.query.filter(
            CommentLike.user_id == user.id,
            CommentLike.comment_id == comment.id).first()

        if comment_like_model:
            # 从session中清除数据
            db.session.delete(comment_like_model)

            comment.like_count -= 1

    try:
        # 插入提交数据
        db.session.commit()
    except Exception as e:
        # 数据库回滚
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库操作有误")

    return jsonify(errno=RET.OK, errmsg="OK")