blog.py
This file
Path
examples/tutorial/flaskr/blog.py
119 lines · 3.2 KiB · Python
Last changed by
Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
History of this fileTake it
Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 1why, one step back1from flask import Blueprint, flash, g, redirect, render_template, request, url_forMerge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 2–6why, one step back2from werkzeug.exceptions import abort34from flaskr.auth import login_required5from flaskr.db import get_db6Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 7why, one step back7bp = Blueprint("blog", __name__)Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 8–9why, one step back89Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 10why, one step back10@bp.route("/")Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 11–14why, one step back11def index():12 """Show all the posts, most recent first."""13 db = get_db()14 posts = db.execute(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 15–17why, one step back15 "SELECT p.id, title, body, created, author_id, username"16 " FROM post p JOIN user u ON p.author_id = u.id"17 " ORDER BY created DESC"Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 18why, one step back18 ).fetchall()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 19why, one step back19 return render_template("blog/index.html", posts=posts)Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 20–33why, one step back202122def get_post(id, check_author=True):23 """Get a post and its author by id.2425 Checks that the id exists and optionally that the current user is26 the author.2728 :param id: id of post to get29 :param check_author: require the current user to be the author30 :return: the post with author information31 :raise 404: if a post with the given id doesn't exist32 :raise 403: if the current user isn't the author33 """Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 34–43why, one step back34 post = (35 get_db()36 .execute(37 "SELECT p.id, title, body, created, author_id, username"38 " FROM post p JOIN user u ON p.author_id = u.id"39 " WHERE p.id = ?",40 (id,),41 )42 .fetchone()43 )Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 44–47why, one step back4445 if post is None:46 abort(404, "Post id {0} doesn't exist.".format(id))47Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 48why, one step back48 if check_author and post["author_id"] != g.user["id"]:Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 49–53why, one step back49 abort(403)5051 return post5253Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 54why, one step back54@bp.route("/create", methods=("GET", "POST"))Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 55–57why, one step back55@login_required56def create():57 """Create a new post for the current user."""Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 58–60why, one step back58 if request.method == "POST":59 title = request.form["title"]60 body = request.form["body"]Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 61–63why, one step back61 error = None6263 if not title:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 64why, one step back64 error = "Title is required."Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 65–70why, one step back6566 if error is not None:67 flash(error)68 else:69 db = get_db()70 db.execute(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 71–72why, one step back71 "INSERT INTO post (title, body, author_id)" " VALUES (?, ?, ?)",72 (title, body, g.user["id"]),Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 73–74why, one step back73 )74 db.commit()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 75why, one step back75 return redirect(url_for("blog.index"))Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 76why, one step back76Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 77why, one step back77 return render_template("blog/create.html")Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 78–79why, one step back7879Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 80why, one step back80@bp.route("/<int:id>/update", methods=("GET", "POST"))Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 81–85why, one step back81@login_required82def update(id):83 """Update a post if the current user is the author."""84 post = get_post(id)85Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 86–88why, one step back86 if request.method == "POST":87 title = request.form["title"]88 body = request.form["body"]Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 89–91why, one step back89 error = None9091 if not title:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 92why, one step back92 error = "Title is required."Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 93–98why, one step back9394 if error is not None:95 flash(error)96 else:97 db = get_db()98 db.execute(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 99why, one step back99 "UPDATE post SET title = ?, body = ? WHERE id = ?", (title, body, id)Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 100–101why, one step back100 )101 db.commit()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 102why, one step back102 return redirect(url_for("blog.index"))Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 103why, one step back103Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 104why, one step back104 return render_template("blog/update.html", post=post)Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 105–106why, one step back105106Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 107why, one step back107@bp.route("/<int:id>/delete", methods=("POST",))Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 108–116why, one step back108@login_required109def delete(id):110 """Delete a post.111112 Ensures that the post exists and that the logged in user is the113 author of the post.114 """115 get_post(id)116 db = get_db()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 117why, one step back117 db.execute("DELETE FROM post WHERE id = ?", (id,))Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 118why, one step back118 db.commit()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 119why, one step back119 return redirect(url_for("blog.index"))