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})
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))
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})
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))
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})
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})