def createLikes():

    try:
        if not request.json:
            abort(400)
        else:
            likes = Likes(request.json['feeds_id'],
                          request.json['no_of_likes'])
            session.add(likes)
            session.commit()
            return jsonify(likes.serialize())

    except Exception as e:
        return (str(e))
Beispiel #2
0
def like_unlike_post(message_id):
    """Add/Delete liked messages"""
    
    message = Message.query.get(message_id)
    if message not in g.user.likes:
        like = Likes(user_id=message.user_id,message_id=message_id)
        g.user.likes.append(message)
        db.session.commit()
        serialized = like.serialize()
        like = Likes.query.filter(Likes.message_id == message_id).first()
        return jsonify(like=serialized), 201
    else:
        like = Likes.query.filter(Likes.message_id == message_id).first()
        db.session.delete(like)
        db.session.commit()
        return jsonify(message="Deleted")
    return render_template('home.html')
Beispiel #3
0
def like_unlike_post(message_id):
    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    message = Message.query.get(message_id)
    if message not in g.user.likes:
        like = Likes(user_id=message.user_id, message_id=message_id)
        g.user.likes.append(message)
        db.session.commit()
        serialized = like.serialize()
        like = Likes.query.filter(Likes.message_id == message_id).first()
        return jsonify(like=serialized), 201
    else:
        like = Likes.query.filter(Likes.message_id == message_id).first()
        db.session.delete(like)
        db.session.commit()
        return jsonify(message="Deleted")
    return render_template('home.html')
Beispiel #4
0
def create_like():
    """
    Create a like association to message based on JSON data;
    return created like in JSON.
    """
    data = request.json
    likes_data = {
        field: data.get(field)
        for field in Likes.__table__.columns.keys() if data.get(field)
    }
    try:
        likes = Likes(**likes_data)
        db.session.add(likes)
        db.session.commit()
    except IntegrityError as e:
        resp = jsonify({"message": e.orig.pgerror})
        return (resp, 400)
    except:
        resp = jsonify({"message": "Failed to create a like association"})
        return (resp, 400)

    resp = jsonify({"likes": likes.serialize()})
    return (resp, 201)