Esempio n. 1
0
def update_comment(id):
    body = request.get_json(force=True)

    update = CommentSchema().load(body)
    updated = db.query(Comment).filter(
        Comment.id == id, Comment.author_id == g.user.id).update(update)
    db.commit()

    return jsonify({'success': True if updated else False})
Esempio n. 2
0
def comment_on_post(id):
    body = request.get_json(force=True)

    data = CommentSchema(only=['body']).load(body)
    comment = Comment(**data, author_id=g.user.id, post_id=id)
    db.add(comment)
    db.commit()

    return jsonify(CommentSchema().dump(comment))
Esempio n. 3
0
def update_post(id):
    body = request.get_json(force=True)

    update = PostSchema(only=['title', 'body']).load(body)
    updated = db.query(Post).filter(Post.id == id,
                                    Post.author_id == g.user.id).update(update)
    db.commit()

    return jsonify({'success': True if updated else False})
Esempio n. 4
0
def create_post():
    body = request.get_json(force=True)

    data = PostSchema(only=['post', 'title']).load(body)
    post = Post(**data, author_id=g.user.id)
    db.add(post)
    db.commit()

    return jsonify(PostSchema().dump(post))
Esempio n. 5
0
def register():
    body = request.get_json(force=True)
    data = UserSchema().load(body)

    username_taken = db.query(User).filter(
        User.username == data['username']).first()
    if username_taken:
        return jsonify({'errors': {'username': '******'}})

    user = User(**data)
    db.add(user)
    db.commit()
    token = jwt.encode({'user_id': user.id}, JWT_SECRET).decode('utf-8')
    return jsonify({'token': token})
Esempio n. 6
0
def delete_comment(id):
    deleted = db.query(Comment).filter(
        Comment.id == id, Comment.author_id == g.user.id).delete()
    db.commit()

    return jsonify({'success': True if deleted else False})