blog.py
This file
Path
examples/tutorial/flaskr/blog.py
125 lines · 3.2 KiB · Python
Last changed by
Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
History of this fileTake it
The walk stopped after 200 commits: lines older than c5c8bbc point at it.
Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
lines 1–7why, one step back1from flask import Blueprint2from flask import flash3from flask import g4from flask import redirect5from flask import render_template6from flask import request7from flask import url_forMerge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 8–12why, one step back8from werkzeug.exceptions import abort910from flaskr.auth import login_required11from flaskr.db import get_db12Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 13why, one step back13bp = Blueprint("blog", __name__)Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 14–15why, one step back1415Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 16why, one step back16@bp.route("/")Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 17–20why, one step back17def index():18 """Show all the posts, most recent first."""19 db = get_db()20 posts = db.execute(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 21–23why, one step back21 "SELECT p.id, title, body, created, author_id, username"22 " FROM post p JOIN user u ON p.author_id = u.id"23 " ORDER BY created DESC"Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
line 24why, one step back24 ).fetchall()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 25why, one step back25 return render_template("blog/index.html", posts=posts)Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 26–39why, one step back262728def get_post(id, check_author=True):29 """Get a post and its author by id.3031 Checks that the id exists and optionally that the current user is32 the author.3334 :param id: id of post to get35 :param check_author: require the current user to be the author36 :return: the post with author information37 :raise 404: if a post with the given id doesn't exist38 :raise 403: if the current user isn't the author39 """Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 40–49why, one step back40 post = (41 get_db()42 .execute(43 "SELECT p.id, title, body, created, author_id, username"44 " FROM post p JOIN user u ON p.author_id = u.id"45 " WHERE p.id = ?",46 (id,),47 )48 .fetchone()49 )Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 50–51why, one step back5051 if post is None:Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 52why, one step back52 abort(404, f"Post id {id} doesn't exist.")Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
line 53why, one step back53Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 54why, one step back54 if check_author and post["author_id"] != g.user["id"]:Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 55–59why, one step back55 abort(403)5657 return post5859Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 60why, one step back60@bp.route("/create", methods=("GET", "POST"))Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 61–63why, one step back61@login_required62def create():63 """Create a new post for the current user."""Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 64–66why, one step back64 if request.method == "POST":65 title = request.form["title"]66 body = request.form["body"]Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 67–69why, one step back67 error = None6869 if not title:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 70why, one step back70 error = "Title is required."Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 71–76why, one step back7172 if error is not None:73 flash(error)74 else:75 db = get_db()76 db.execute(Merge pull request #3247 from pallets/join-stringsDavid Lordbefore adoptiond4b688b
line 77why, one step back77 "INSERT INTO post (title, body, author_id) VALUES (?, ?, ?)",Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 78why, one step back78 (title, body, g.user["id"]),Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 79–80why, one step back79 )80 db.commit()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 81why, one step back81 return redirect(url_for("blog.index"))Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
line 82why, one step back82Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 83why, one step back83 return render_template("blog/create.html")Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 84–85why, one step back8485Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 86why, one step back86@bp.route("/<int:id>/update", methods=("GET", "POST"))Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 87–91why, one step back87@login_required88def update(id):89 """Update a post if the current user is the author."""90 post = get_post(id)91Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 92–94why, one step back92 if request.method == "POST":93 title = request.form["title"]94 body = request.form["body"]Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 95–97why, one step back95 error = None9697 if not title:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 98why, one step back98 error = "Title is required."Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 99–104why, one step back99100 if error is not None:101 flash(error)102 else:103 db = get_db()104 db.execute(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 105why, one step back105 "UPDATE post SET title = ?, body = ? WHERE id = ?", (title, body, id)Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 106–107why, one step back106 )107 db.commit()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 108why, one step back108 return redirect(url_for("blog.index"))Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
line 109why, one step back109Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 110why, one step back110 return render_template("blog/update.html", post=post)Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 111–112why, one step back111112Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 113why, one step back113@bp.route("/<int:id>/delete", methods=("POST",))Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 114–122why, one step back114@login_required115def delete(id):116 """Delete a post.117118 Ensures that the post exists and that the logged in user is the119 author of the post.120 """121 get_post(id)122 db = get_db()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 123why, one step back123 db.execute("DELETE FROM post WHERE id = ?", (id,))Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
line 124why, one step back124 db.commit()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 125why, one step back125 return redirect(url_for("blog.index"))