def create_comment(post_id): ''' file: ./documentation/create_comment.yml ''' post = Post.query.filter_by(id=post_id).first() if post is not None: request_body = json.loads(request.data) # Code here checks for blank body requests / @beforerequests checks for None body requests if not request_body.get('text') == '' and not request_body.get( 'username') == '': comment = Comment(text=request_body.get('text'), username=request_body.get('username'), post_id=post_id) post.comments.append(comment) db.session.add(comment) db.session.commit() return json.dumps({ 'success': True, 'data': comment.serialize() }), 201 return json.dumps({ 'success': False, 'error': 'invalid body format' }), 412 return json.dumps({'success': False, 'error': 'Post not found!'}), 404
def create_comment(user_id, post_id, body): comment = Comment(timestamp=datetime.datetime.now(), content=body.get("content"), edited=False, user_id=user_id, post_id=post_id) db.session.add(comment) db.session.commit() return comment.serialize()
def create_comment(post_id): post = Post.query.filter_by(id=post_id).first() if post is None: return json.dumps({'success': False, 'error': 'Post not found!'}), 404 comment_body = json.loads(request.data) comment = Comment(text=comment_body.get('text'), username=comment_body.get('username'), post_id=post.id) post.comments.append(comment) db.session.add(comment) db.session.commit() return json.dumps({'success': True, 'data': comment.serialize()}), 201
def create_comment(userid, postid): post_body = json.loads(request.data) try: comment = Comment(text=post_body.get('text'), user_id=userid, post_id=postid) db.session.add(comment) db.session.commit() return json.dumps({'success': True, 'data': comment.serialize()}), 201 except KeyError as e: return json.dumps({'success': False, 'data': 'Invalid input'}), 404
def create_comment(post_id): post = Post.query.filter_by(id=post_id).first() if not post: return failure_response('Post not found') body = json.loads(request.data) name = body.get('name') description = body.get('description') if not body: return failure_response('Missing required field') new_comment = Comment(netid = body.get("netid"), name = name, description = description) post.comments.append(new_comment) db.session.add(new_comment) db.session.commit() return success_response(new_comment.serialize(), 201)
def post_com(post_id): post = Post.query.filter_by(id=post_id).first() if post is not None: com_body = json.loads(request.data) com = Comment( content=com_body.get("content"), username=com_body.get("username"), post_id=post_id, ) post.comments.append(com) db.session.add(com) db.session.commit() return json.dumps({"success": True, "data": com.serialize()}) return json.dumps({"success": False, "error": "Post not found"}), 404
def add_post_comment(post_id): post = Post.query.filter_by(id=post_id, active=True).first() if post is None: return nopost() comment_body = extract(request) if 'text' not in comment_body or 'token' not in comment_body: return missing() uid = token_to_uid(comment_body) if uid is None: return invalid_token() comment = Comment(text=comment_body.get('text'), uid=uid, post_id=post_id, creation_time=time.time()) activate(uid) post.comments.append(comment) db.session.add(comment) db.session.commit() return json.dumps({'success': True, 'data': comment.serialize()}), 201
def post_comment(post_id): post = Post.query.filter_by(id=post_id).first() if not post: return json.dumps({'success': False, 'error': 'Post not found'}), 404 post_body = json.loads(request.data) body_comment = post_body.get('body') username = post_body.get('username') user = User.query.filter_by(username=username).first() if not user: return json.dumps({'success': False, 'error': 'user not found'}), 404 user_id = user.id newComment = Comment(body_comment=body_comment, time_stamp=datetime.now(), date=date.today(), user_id=user_id, post_id=post_id) db.session.add(newComment) db.session.commit() return json.dumps({'success': True, 'data': newComment.serialize()}), 201
def create_comment(trail_id, review_id): trail = Trail.query.filter_by(id=trail_id, ).first() if trail is None: return failure_response('Trail not found!') review = Review.query.filter_by(id=review_id, trail=trail).first() if review is None: return failure_response('Review not found!') body = json.loads(request.data) new_comment = Comment(body=body.get('body'), username=body.get('username'), review_id=review_id, review=review) if new_comment.body is None or new_comment.username is None: return failure_response('Could not create comment!') review.comments.append(new_comment) db.session.add(new_comment) db.session.commit() return success_response(new_comment.serialize(), 201)