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
1from flask import Blueprint, flash, g, redirect, render_template, request, url_for2from werkzeug.exceptions import abort34from flaskr.auth import login_required5from flaskr.db import get_db67bp = Blueprint("blog", __name__)8910@bp.route("/")11def index():12 """Show all the posts, most recent first."""13 db = get_db()14 posts = db.execute(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"18 ).fetchall()19 return render_template("blog/index.html", posts=posts)202122def 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 """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 )4445 if post is None:46 abort(404, "Post id {0} doesn't exist.".format(id))4748 if check_author and post["author_id"] != g.user["id"]:49 abort(403)5051 return post525354@bp.route("/create", methods=("GET", "POST"))55@login_required56def create():57 """Create a new post for the current user."""58 if request.method == "POST":59 title = request.form["title"]60 body = request.form["body"]61 error = None6263 if not title:64 error = "Title is required."6566 if error is not None:67 flash(error)68 else:69 db = get_db()70 db.execute(71 "INSERT INTO post (title, body, author_id)" " VALUES (?, ?, ?)",72 (title, body, g.user["id"]),73 )74 db.commit()75 return redirect(url_for("blog.index"))7677 return render_template("blog/create.html")787980@bp.route("/<int:id>/update", methods=("GET", "POST"))81@login_required82def update(id):83 """Update a post if the current user is the author."""84 post = get_post(id)8586 if request.method == "POST":87 title = request.form["title"]88 body = request.form["body"]89 error = None9091 if not title:92 error = "Title is required."9394 if error is not None:95 flash(error)96 else:97 db = get_db()98 db.execute(99 "UPDATE post SET title = ?, body = ? WHERE id = ?", (title, body, id)100 )101 db.commit()102 return redirect(url_for("blog.index"))103104 return render_template("blog/update.html", post=post)105106107@bp.route("/<int:id>/delete", methods=("POST",))108@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()117 db.execute("DELETE FROM post WHERE id = ?", (id,))118 db.commit()119 return redirect(url_for("blog.index"))