Ejemplo n.º 1
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):
Ejemplo n.º 2
0
def like_post():
    id = request.args.get('id')
    post = Post.query.filter_by(id=id).first()
    result = {'like': True}
    if current_user.has_liked_post(post):
        current_user.unlike_post(post)
        result = {'like': False}
        return jsonify(result)
    else:
        current_user.like_post(post)
        return jsonify(result)
Ejemplo n.º 3
0
def like_post(post_id, action):
    post = Post.query.filter_by(id=post_id).first()
    if current_user.username == 'admin' and current_user.email == '*****@*****.**':
        session['logged_in'] = True
        return redirect('/admin')
    if action == 'like' and current_user.has_liked_post(post):
        flash('Already liked post')
    if action == 'like':
        new_like = Like(liker=current_user, liked=post)
        db.session.add(new_like)
        db.session.commit()

    if action == 'unlike':
        like = Like.query.filter_by(liker=current_user, liked=post).delete()
        db.session.commit()

    return jsonify({
        "result": "success",
        "total_likes": post.likes.count(),
        "liked": current_user.has_liked_post(post)
    })
Ejemplo n.º 4
0
def like_unlike(post_id=None, action=None):
    post = database.models.Post.query.filter_by(id=post_id).first()
    if action == 'like':
        postlike = database.models.Post.query.filter_by(
            user_id=current_user.id).all()
        print(current_user.has_liked_post(post))
        current_user.like_post(post)
        db.session.commit()
    if action == 'unlike':
        current_user.unlike_post(post)
        db.session.commit()
    return redirect(request.referrer)
Ejemplo n.º 5
0
def like_action(id, action):
    post = Post.query.filter_by(id=id).first_or_404()
    if action == 'like':
        current_user.like_post(post)
        current_user.add_notification(
            current_user.username + ' just liked your post',
            current_user.has_liked_post(post))
        db.session.commit()
    if action == 'unlike':
        current_user.unlike_post(post)
        db.session.commit()
    return redirect(request.referrer)
Ejemplo n.º 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_liked_post(post):
            current_user.unlike_post(post)
            post.like -= 1
            db.session.commit()
        else:
            current_user.like_post(post)
            post.like += 1
            db.session.commit()
        return redirect(url_for('posts.post', post_id=post.id))
Ejemplo n.º 7
0
def unlike_action(post_id):
    ''' Like route, it is used to unlike a post '''

    if request.method == 'POST':

        post = Post.query.filter_by(id=post_id).first()

        if post is None:
            return jsonify({'message': 'Post not found!'})
        if current_user.has_liked_post(post) == False:
            return jsonify({'message': 'You never liked this post'})
        else:
            current_user.unlike_post(post)
            db.session.commit()
            return jsonify({'message': 'You unlike the post'})
Ejemplo n.º 8
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'))
Ejemplo n.º 9
0
def like_action():
    blog_post = BlogPost.query.filter_by(id=request.form['id']).first_or_404()
    if request.method == "POST":
        if current_user.has_liked_post(blog_post):
            current_user.unlike_post(blog_post)
            db.session.commit()
            blog_post = BlogPost.query.filter_by(
                id=request.form['id']).first_or_404()
            return jsonify({
                'condition': 'like',
                'count': blog_post.likes.count()
            })
        else:
            current_user.like_post(blog_post)
            db.session.commit()
            blog_post = BlogPost.query.filter_by(
                id=request.form['id']).first_or_404()

            return jsonify({
                'condition': 'unlike',
                'count': blog_post.likes.count()
            })