Example #1
0
def like(postid):
    if request.method == 'POST':
        user = current_user
        post = UserPosts.query.filter_by(id=postid).first()

        def check_if_user_liked_already(user, post):
            likes = UserLikes.query.all()
            for like in likes:
                if like.user_id == user.id and like.post_id == post.id:
                    return True
            return False

        if post is not None:
            if check_if_user_liked_already(user, post):
                er = True
                msg = "{} already likes this post".format(user.user_name)
                return jsonify(error=er, message=msg), 403
            new_like = UserLikes(user.id, post.id)
            db.session.add(new_like)
            db.session.commit()

            er = None
            msg = "{} liked this post".format(user.user_name)
            return jsonify(error=er, message=msg), 202
        else:
            er = True
            msg = "Invalid post id"
            return jsonify(error=er, message=msg), 404
    else:
        abort(405)
Example #2
0
def like(post_id):
    """ set a like on the current post by the logged in user"""
    if request.method == 'POST':
        user_id = int(request.form['user_id'])
        post = int(request.form['post_id'])
        like = UserLikes(user_id, post)
        db.session.add(like)
        db.session.commit()
        total_likes = len(UserLikes.query.filter_by(post_id=post).all())
        return jsonify(response=[{
            'message': 'You liked a user post',
            'likes': total_likes
        }])
    return jsonify(error=[{'error': 'unable to create link'}])
def like(postid):
    if request.method == 'POST':
        user = current_user
        post = UserPosts.query.filter_by(id = postid).first()
        if post is not None:
            new_like = UserLikes(user.id,post.id)
            db.session.add(new_like)
            db.session.commit()
        
            er=None
            msg ="{} liked this post".format(user.user_name)
            return jsonify(error=er,message=msg)
        else:
            er=True
            msg ="Invalid post id"
            return jsonify(error=er,message=msg)
    else:
        abort(405)