def update(id): """Update a post if the current user is the author.""" post = check_post(id) if not post: return Response("post not found", status=404) if request.method == "POST": error = None json = request.get_json() if check_attrs_in_json(json, 'title', 'body'): title = json['title'] body = json['body'] db = get_db() update_post(db, title, body, id) return Response('Post was updated', status=200) else: error = 'incorrect json' flash(error) return Response('%s' % error, status=400) return Response("need POST method", status=405)
def post(id): post = dict(check_post(id)) error = None # post with id does not exist if not post: error = json.dumps({"error": "Post id {0} not available.".format(id)}) if error: return Response( error, status=404, mimetype="application/json" ) # get post by id if request.method == "GET": post['created'] = post['created'].strftime("%d-%b-%Y (%H:%M:%S)") data = json.dumps(dict(post)) return Response( data, status=200, mimetype="application/json" ) # update post by id elif request.method == "PUT": 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() update_post(db, title, body, id) message = json.dumps({'message': 'Post id {0} update successfully'.format(id)}) return Response( message, status=200, mimetype="application/json" ) # delete post by id elif request.method == "DELETE": db = get_db() delete_post(db, id) message = json.dumps({'message': 'Post id {0} delete successfully'.format(id)}) return Response( message, status=200, mimetype="application/json" )
def update(post_id): """Update a post if the current user is the author.""" check_post(post_id) json = request.get_json() if json.get('title') and json.get('body'): title, body = json['title'], json['body'] update_post(get_db(), title, body, post_id) return Response("Success: post was updated", 200) abort(400, description='Error: Title and body is required')
def update(id): """Update a post if the current user is the author.""" post = check_post(id) if request.method == "POST": error = None db = get_db() json_data = request.get_json() title = json_data['title'] body = json_data['body'] update_post(db, title, body, id) return Response("Updated!", status=200) return Response("Method is not POST", status=40)
def update(id): """Update a post if the current user is the author.""" post = check_post(id) if request.method == "POST": error = None # TODO: достать title и body из формы title = None body = None # TODO: title обязательное поле. Если его нет записать ошибку if error is not None: flash(error) else: db = get_db() update_post(db, title, body, id) return redirect(url_for("blog.index")) return render_template("blog/update.html", post=post)
def upd_post(id): post = check_post(id) if not request.json: abort(400) if 'title' in request.json and not isinstance(request.json['title'], str): abort(400) if 'body' in request.json and not isinstance(request.json['body'], str): abort(400) title = request.json.get('title', post['title']) body = request.json.get('body', post['body']) db = get_db() update_post(db, title, body, id) post = dict(get_post(db, id)) return jsonify({'post': post})