Skip to content
Myr

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 file

Take it

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

line 1why, one step back
1from flask import Blueprint, flash, g, redirect, render_template, request, url_for

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 2–6why, one step back
2from werkzeug.exceptions import abort
34from flaskr.auth import login_required5from flaskr.db import get_db6

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 8–9why, one step back
8
9

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 11–14why, one step back
11def 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 back
15 "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 back
18 ).fetchall()

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 20–33why, one step back
20
2122def 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 back
34 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 back
44
45 if post is None:46 abort(404, "Post id {0} doesn't exist.".format(id))47

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 49–53why, one step back
49 abort(403)
5051 return post5253

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 55–57why, one step back
55@login_required
56def 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 back
58 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 back
61 error = None
6263 if not title:

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 65–70why, one step back
65
66 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 back
71 "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 back
73 )
74 db.commit()

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

line 76why, one step back
76

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 78–79why, one step back
78
79

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 81–85why, one step back
81@login_required
82def update(id):83 """Update a post if the current user is the author."""84 post = get_post(id)85

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

lines 86–88why, one step back
86 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 back
89 error = None
9091 if not title:

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 100–101why, one step back
100 )
101 db.commit()

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

line 103why, one step back
103

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 105–106why, one step back
105
106

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

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

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 108–116why, one step back
108@login_required
109def 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 back
117 db.execute("DELETE FROM post WHERE id = ?", (id,))

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

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

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

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