Browse Source

Chapter 11: Blog post editor (11h)

master
T. Meissner 6 years ago
parent
commit
a482fe670c
3 changed files with 46 additions and 1 deletions
  1. +19
    -1
      app/main/views.py
  2. +9
    -0
      app/templates/_posts.html
  3. +18
    -0
      app/templates/edit_post.html

+ 19
- 1
app/main/views.py View File

@ -1,5 +1,5 @@
from flask import render_template, redirect, url_for, flash, request, \ from flask import render_template, redirect, url_for, flash, request, \
current_app
current_app, abort
from flask_login import login_required, current_user from flask_login import login_required, current_user
from . import main from . import main
from .forms import EditProfileForm, EditProfileAdminForm, PostForm from .forms import EditProfileForm, EditProfileAdminForm, PostForm
@ -88,3 +88,21 @@ def edit_profile_admin(id):
def post(id): def post(id):
post = Post.query.get_or_404(id) post = Post.query.get_or_404(id)
return render_template('post.html', posts=[post]) return render_template('post.html', posts=[post])
@main.route('/edit/<int:id>', 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)

+ 9
- 0
app/templates/_posts.html View File

@ -17,6 +17,15 @@
{% endif %} {% endif %}
</div> </div>
<div class="post-footer"> <div class="post-footer">
{% if current_user == post.author %}
<a href="{{ url_for('.edit', id=post.id) }}">
<span class="label label-primary">Edit</span>
</a>
{% elif current_user.is_administrator() %}
<a href="{{ url_for('.edit', id=post.id) }}">
<span class="label label-danger">Edit [Admin]</span>
</a>
{% endif %}
<a href="{{ url_for('.post', id=post.id) }}"> <a href="{{ url_for('.post', id=post.id) }}">
<span class="label label-default">Permalink</span> <span class="label label-default">Permalink</span>
</a> </a>


+ 18
- 0
app/templates/edit_post.html View File

@ -0,0 +1,18 @@
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Flasky - Edit Post{% endblock %}
{% block page_content %}
<div class="page-header">
<h1>Edit Post</h1>
</div>
<div>
{{ wtf.quick_form(form) }}
</div>
{% endblock %}
{% block scripts %}
{{ super() }}
{{ pagedown.include_pagedown() }}
{% endblock %}

Loading…
Cancel
Save