def check_comment(id, check_author=True): """Get a comment, its author and post by id. Checks that the id exists and optionally that the current user is the author. :param id: id of comment to get :param check_author: require the current user to be the author :return: the comment with author and post information :raise 404: if a comment with the given id doesn't exist :raise 403: if the current user isn't the author """ comment = get_comment(get_db(), id) if comment is None: abort(404, "Comment id {0} doesn't exist.".format(id)) username = auth.username() user = get_user_by_username(get_db(), username) if not user: abort(403) if check_author and comment["author_id"] != user["id"]: abort(403) return comment
def check_comment(id, check_author=True): comment = get_comment(get_db(), id) if comment is None: abort(404, description=f"Comment id {id} doesn't exist.") user_id = get_user_by_username(get_db(), auth.username())['id'] if check_author and comment["author_id"] != user_id: abort(403) return comment
def create(): """Create a new post for the current user.""" json = request.get_json() if json.get('title') and json.get('body'): title, body = json['title'], json['body'] user_id = get_user_by_username(get_db(), auth.username())['id'] create_post(get_db(), title, body, user_id) return Response("Success: post was created", 200) abort(400, description='Error: Title and body is required')
def create(post_id): """Create a new comment""" json = request.get_json() if json.get('text'): text = json['text'] user_id = get_user_by_username(get_db(), auth.username())['id'] create_comment(get_db(), post_id, user_id, text) return Response("Success: comment was created", 200) abort(400, description='Error: Text and body is required')
def check_post(id, check_author=True): """Get a post and its author by id. Checks that the id exists and optionally that the current user is the author. :param id: id of post to get :param check_author: require the current user to be the author :return: the post with author information :raise 404: if a post with the given id doesn't exist :raise 403: if the current user isn't the author """ post = get_post(get_db(), id) if post is None: abort(404, description=f"Post id {id} doesn't exist.") user_id = get_user_by_username(get_db(), auth.username())['id'] if check_author and post["author_id"] != user_id: abort(403) return post
def post_list(): db = get_db() error = None # get all posts if request.method == "GET": posts = list(posts_list(db)) if not posts: error = json.dumps({"error": "No posts available."}) if error: return Response( error, status=404, mimetype="application/json" ) data = list() for post in posts: post = dict(post) post['created'] = post['created'].strftime("%d-%b-%Y (%H:%M:%S)") data.append(post) data = json.dumps(data) return Response( data, status=200, mimetype="application/json" ) # create new post elif request.method == "POST": data = request.get_json() title = data.get('title', '') body = data.get('body', '') if not title: error = json.dumps({"error": "Title is required"}) if error: return Response( error, status=400, mimetype="application/json" ) db = get_db() username = auth.username() user = get_user_by_username(username) create_post(db, title, body, user["id"]) data = json.dumps({'title': title,'body': body, 'user_id': user["id"]}) return Response( data, status=201, mimetype="application/json" ) error = json.dumps({"error": 'Unknown method'}) return Response( error, stauts=405, mimetype="application/json" )
def comment_list(post_id): db = get_db() error = None # get all comments if request.method == "GET": comments = list(comments_list(db)) if not comments: error = json.dumps({"error": "No comments available."}) if error: return Response( error, status=404, mimetype="application/json" ) data = list() for comment in comments: comment = dict(comment) comment['created'] = comment['created'].strftime("%d-%b-%Y (%H:%M:%S)") data.append(comment) data = json.dumps(data) return Response( data, status=200, mimetype="application/json" ) # create new comment elif request.method == "POST": data = request.get_json() body = data.get('body', '') if not body: error = json.dumps({"error": "Body is required"}) if error: return Response( error, status=400, mimetype="application/json" ) db = get_db() username = auth.username() user = get_user_by_username(username) create_comment(db, body, user["id"], post_id) data = json.dumps({'body': body, 'post_id': post_id, 'author_id': user["id"]}) return Response( data, status=201, mimetype="application/json" ) error = json.dumps({"error": 'Unknown method'}) return Response( error, stauts=405, mimetype="application/json" )