Skip to content
Myr

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 file

Take 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 back
1from flask import Blueprint
2from flask import flash3from flask import g4from flask import redirect5from flask import render_template6from flask import request7from flask import url_for

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 8–12why, one step back
8from werkzeug.exceptions import abort
910from flaskr.auth import login_required11from flaskr.db import get_db12

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 13why, one step back
13bp = Blueprint("blog", __name__)

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 14–15why, one step back
14
15

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 16why, one step back
16@bp.route("/")

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 17–20why, one step back
17def 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 back
21 "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 back
24 ).fetchall()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 25why, one step back
25 return render_template("blog/index.html", posts=posts)

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 26–39why, one step back
26
2728def 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 back
40 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 back
50
51 if post is None:

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

line 52why, one step back
52 abort(404, f"Post id {id} doesn't exist.")

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

line 53why, one step back
53

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 54why, one step back
54 if check_author and post["author_id"] != g.user["id"]:

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 55–59why, one step back
55 abort(403)
5657 return post5859

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 60why, one step back
60@bp.route("/create", methods=("GET", "POST"))

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 61–63why, one step back
61@login_required
62def 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 back
64 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 back
67 error = None
6869 if not title:

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 70why, one step back
70 error = "Title is required."

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 71–76why, one step back
71
72 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 back
77 "INSERT INTO post (title, body, author_id) VALUES (?, ?, ?)",

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 78why, one step back
78 (title, body, g.user["id"]),

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 79–80why, one step back
79 )
80 db.commit()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 81why, one step back
81 return redirect(url_for("blog.index"))

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

line 82why, one step back
82

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 83why, one step back
83 return render_template("blog/create.html")

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 84–85why, one step back
84
85

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 86why, one step back
86@bp.route("/<int:id>/update", methods=("GET", "POST"))

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 87–91why, one step back
87@login_required
88def update(id):89 """Update a post if the current user is the author."""90 post = get_post(id)91

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 92–94why, one step back
92 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 back
95 error = None
9697 if not title:

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 98why, one step back
98 error = "Title is required."

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 99–104why, one step back
99
100 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 back
105 "UPDATE post SET title = ?, body = ? WHERE id = ?", (title, body, id)

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 106–107why, one step back
106 )
107 db.commit()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 108why, one step back
108 return redirect(url_for("blog.index"))

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

line 109why, one step back
109

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 110why, one step back
110 return render_template("blog/update.html", post=post)

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 111–112why, one step back
111
112

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 113why, one step back
113@bp.route("/<int:id>/delete", methods=("POST",))

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

lines 114–122why, one step back
114@login_required
115def 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 back
123 db.execute("DELETE FROM post WHERE id = ?", (id,))

Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc

line 124why, one step back
124 db.commit()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 125why, one step back
125 return redirect(url_for("blog.index"))