示例#1
0
def like_action_comment(post_id, comment_id, action):
    ''' Adds or removes a like/dislike to or from a comment '''

    post = Posts.query.get_or_404(post_id)
    comment = Comment.query.get_or_404(comment_id)

    if action == 'like':
        if current_user.has_disliked_comment:
            current_user.undislike_post(post=None, comment=comment)
        current_user.like_post(post=None, comment=comment)
        db.session.commit()
    if action == 'unlike':
        current_user.unlike_post(post=None, comment=comment)
        db.session.commit()

    if action == 'dislike':
        if current_user.has_liked_comment:
            current_user.unlike_post(post=None, comment=comment)
        current_user.dislike_post(post=None, comment=comment)
        db.session.commit()
    if action == 'undislike':
        current_user.undislike_post(post=None, comment=comment)
        db.session.commit()

    return redirect(
        url_for('boards.selected_comment',
                topic_address=post.topic,
                post_id=post_id,
                comment_id=comment_id))
示例#2
0
def like_action(post_id, action):
    post = Post.query.filter_by(id=post_id).first_or_404()
    if request.method == 'POST':
        if action == 'like':
            if current_user.has_disliked_post(post):
                current_user.undislike_post(post)
                db.session.commit()

            if current_user.has_liked_post(post):
                current_user.unlike_post(post)
                db.session.commit()

            else:
                print('like')
                current_user.like_post(post)
                db.session.commit()

            return succesful_response({'message': 'Great Success!'})

        if action == 'dislike':
            if current_user.has_liked_post(post):
                current_user.unlike_post(post)
                db.session.commit()

            if current_user.has_disliked_post(post):
                current_user.undislike_post(post)
                db.session.commit()

            else:
                current_user.dislike_post(post)
                db.session.commit()

            return succesful_response({'message': 'Great Success!'})

    if request.method == 'GET':
        if current_user.has_liked_post(post):
            liked = True
            disliked = False
        elif current_user.has_disliked_post(post):
            liked = False
            disliked = True
        else:
            liked = False
            disliked = False

        data = {
            'likes': post.likes.count(),
            'dislikes': post.dislikes.count(),
            'liked': liked,
            'disliked': disliked
        }
        return data


# @app.route('/profile/<', methods=['GET', 'POST'])
# @login_required
# def like_action(post_id, action):
示例#3
0
def dislike_action(post_id, action):
    post = Post.query.filter_by(id=post_id).first_or_404()
    if action == 'dislike':
        current_user.dislike_post(post)
        db.session.commit()
    if action == 'undislike':
        current_user.undislike_post(post)
        db.session.commit()
    return redirect(request.referrer)
示例#4
0
def like_action(post_id, action):
    post = Post.query.get_or_404(post_id)
    if action == 'like':
        current_user.like_post(post)
        current_user.undislike_post(post)
        db.session.commit()
    if action == 'unlike':
        current_user.unlike_post(post)
        db.session.commit()
    return redirect(request.referrer)
示例#5
0
文件: routes.py 项目: kmstage/Qtalk
def dislike_action(post_id, action):
    post = Post.query.filter_by(id=post_id).first_or_404()
    if action == 'dislike':
        current_user.dislike_post(post)
        db.session.commit()
        likes = post.likes.count()
        dislikes = post.dislikes.count()
        result = f"{likes} لایک و {dislikes} دیسلایک"
        return jsonify(post_id=post_id, state='dislike', result=result)
    if action == 'undislike':
        current_user.undislike_post(post)
        db.session.commit()
        likes = post.likes.count()
        dislikes = post.dislikes.count()
        result = f"{likes} لایک و {dislikes} دیسلایک"
        return jsonify(post_id=post_id, state='undislike', result=result)
示例#6
0
def like_action(post_id, action):
    post = Post.query.filter_by(id=post_id).first_or_404()
    if action == 'like':
        if current_user.has_disliked_post(post):
            current_user.undislike_post(post)
        current_user.like_post(post)
        db.session.commit()
    if action == 'unlike':
        current_user.unlike_post(post)
        db.session.commit()
    if action == 'dislike':
        if current_user.has_liked_post(post):
            current_user.unlike_post(post)
        current_user.dislike_post(post)
        db.session.commit()
    if action == 'undislike':
        current_user.undislike_post(post)
        db.session.commit()
    return redirect(url_for('blog'))