def edit_post(id): form=PostEditForm(request.form) post = Post.query.filter_by(id = id).first() choices=[] if request.method=="GET" or request.method=="POST": tags=Tag.query.all() for tag in tags: choices.append((tag.tagname,tag.tagname)) form.tags.choices=choices if form.validate_on_submit()==True: delete_post=form.delete.data if delete_post==True: database.session.delete(post) database.session.commit() return redirect(url_for("index")) post.title=form.title.data post.body=form.body.data taglist=form.create_taglist() post.tags=taglist database.session.add(post) database.session.commit() return render_template('message.html',post = post) else: form.title.data=post.title form.body.data=post.body tagstring=post.create_tagstring() form.tags.data=tagstring return render_template('edit_post.html',title="Edit Post",form=form,post=post)
def admin_post_edit(post_id=None): if post_id: post = Post.get_post_by_id(post_id) success_message = app.config['POST_EDIT_SUCCEED'] else: post = Post() success_message = app.config['POST_ADD_SUCCEED'] form = PostEditForm(request.form, post) if request.form: if form.validate(): if Post.update_post(post, form): flash(success_message, 'success') return redirect(url_for(config.END_POINT_ADMIN_POST_EDIT, post_id=post.id)) else: flash(app.config['FORM_ERROR'], 'error') available_tags = list(app.config['post_tags'].keys()) return render_template('admin/post_edit.html', form=form, tags=available_tags)
def create_post(): """Creates a new post with a new id""" if not request.json: abort(400) form = PostEditForm.from_json(request.json, skip_unkown_keys=False) if form.validate(): post = Post(json=form.data) objId = mongo.db.posts.insert(post.to_bson()) post.id = str(objId) return jsonify(**post.to_dict()), 201 else: abort(400)
def edit_post(post_id): """Replaces post behind id with submitted one""" post_d = mongo.db.posts.find_one(post_id) if post_d is None: abort(404) form = PostEditForm.from_json(request.json, skip_unknown_keys=False) if form.validate(): post = Post(bson=post_d) post.update(form.data) query = {'_id': post_id} mongo.db.posts.update(query, post.to_bson()) # needed for chaining multiple edits together token = generate_csrf_token() return jsonify(_csrf=token, **post.to_dict()) else: print form.errors #TODO abort(400)
def show_editor(post_id): d = mongo.db.posts.find_one(post_id) post = Post(bson=d) form = PostEditForm() return render_template('editor.html', post=post, form=form)