Skip to content

Commit f32d955

Browse files
Chapter 11: Blog post editor (11h)
1 parent 3a6aa64 commit f32d955

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

app/main/views.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,20 @@ def edit_profile_admin(id):
8686
def post(id):
8787
post = Post.query.get_or_404(id)
8888
return render_template('post.html', posts=[post])
89+
90+
91+
@main.route('/edit/<int:id>', methods=['GET', 'POST'])
92+
@login_required
93+
def edit(id):
94+
post = Post.query.get_or_404(id)
95+
if current_user != post.author and \
96+
not current_user.can(Permission.ADMINISTER):
97+
abort(403)
98+
form = PostForm()
99+
if form.validate_on_submit():
100+
post.body = form.body.data
101+
db.session.add(post)
102+
flash('The post has been updated.')
103+
return redirect(url_for('.post', id=post.id))
104+
form.body.data = post.body
105+
return render_template('edit_post.html', form=form)

app/templates/_posts.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
{% endif %}
1818
</div>
1919
<div class="post-footer">
20+
{% if current_user == post.author %}
21+
<a href="{{ url_for('.edit', id=post.id) }}">
22+
<span class="label label-primary">Edit</span>
23+
</a>
24+
{% elif current_user.is_administrator() %}
25+
<a href="{{ url_for('.edit', id=post.id) }}">
26+
<span class="label label-danger">Edit [Admin]</span>
27+
</a>
28+
{% endif %}
2029
<a href="{{ url_for('.post', id=post.id) }}">
2130
<span class="label label-default">Permalink</span>
2231
</a>

app/templates/edit_post.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% extends "base.html" %}
2+
{% import "bootstrap/wtf.html" as wtf %}
3+
4+
{% block title %}Flasky - Edit Post{% endblock %}
5+
6+
{% block page_content %}
7+
<div class="page-header">
8+
<h1>Edit Post</h1>
9+
</div>
10+
<div>
11+
{{ wtf.quick_form(form) }}
12+
</div>
13+
{% endblock %}
14+
15+
{% block scripts %}
16+
{{ super() }}
17+
{{ pagedown.include_pagedown() }}
18+
{% endblock %}

0 commit comments

Comments
 (0)