From a482fe670c480ba188b33360b2d361a2935aef61 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Tue, 20 Nov 2018 23:49:00 +0100 Subject: [PATCH] Chapter 11: Blog post editor (11h) --- app/main/views.py | 20 +++++++++++++++++++- app/templates/_posts.html | 9 +++++++++ app/templates/edit_post.html | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 app/templates/edit_post.html diff --git a/app/main/views.py b/app/main/views.py index b805a32..1ee95b6 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -1,5 +1,5 @@ from flask import render_template, redirect, url_for, flash, request, \ - current_app + current_app, abort from flask_login import login_required, current_user from . import main from .forms import EditProfileForm, EditProfileAdminForm, PostForm @@ -88,3 +88,21 @@ def edit_profile_admin(id): def post(id): post = Post.query.get_or_404(id) return render_template('post.html', posts=[post]) + + +@main.route('/edit/', methods=['GET', 'POST']) +@login_required +def edit(id): + post = Post.query.get_or_404(id) + if current_user != post.author and \ + not current_user.can(Permission.ADMIN): + abort(403) + form = PostForm() + if form.validate_on_submit(): + post.body = form.body.data + db.session.add(post) + db.session.commit() + flash('The post has been updated.') + return redirect(url_for('.post', id=post.id)) + form.body.data = post.body + return render_template('edit_post.html', form=form) diff --git a/app/templates/_posts.html b/app/templates/_posts.html index f6c149d..fc54bd8 100644 --- a/app/templates/_posts.html +++ b/app/templates/_posts.html @@ -17,6 +17,15 @@ {% endif %}