Skip to content
Myr

blog.py

This file

Path

examples/tutorial/flaskr/blog.py

119 lines · 3.1 KiB · Python

Last changed by

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

History of this file

Take it

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 1–119why, one step back
1from flask import (
2 Blueprint, flash, g, redirect, render_template, request, url_for3)4from werkzeug.exceptions import abort56from flaskr.auth import login_required7from flaskr.db import get_db89bp = Blueprint('blog', __name__)101112@bp.route('/')13def index():14 """Show all the posts, most recent first."""15 db = get_db()16 posts = db.execute(17 'SELECT p.id, title, body, created, author_id, username'18 ' FROM post p JOIN user u ON p.author_id = u.id'19 ' ORDER BY created DESC'20 ).fetchall()21 return render_template('blog/index.html', posts=posts)222324def get_post(id, check_author=True):25 """Get a post and its author by id.2627 Checks that the id exists and optionally that the current user is28 the author.2930 :param id: id of post to get31 :param check_author: require the current user to be the author32 :return: the post with author information33 :raise 404: if a post with the given id doesn't exist34 :raise 403: if the current user isn't the author35 """36 post = get_db().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 ).fetchone()4243 if post is None:44 abort(404, "Post id {0} doesn't exist.".format(id))4546 if check_author and post['author_id'] != g.user['id']:47 abort(403)4849 return post505152@bp.route('/create', methods=('GET', 'POST'))53@login_required54def create():55 """Create a new post for the current user."""56 if request.method == 'POST':57 title = request.form['title']58 body = request.form['body']59 error = None6061 if not title:62 error = 'Title is required.'6364 if error is not None:65 flash(error)66 else:67 db = get_db()68 db.execute(69 'INSERT INTO post (title, body, author_id)'70 ' VALUES (?, ?, ?)',71 (title, body, g.user['id'])72 )73 db.commit()74 return redirect(url_for('blog.index'))7576 return render_template('blog/create.html')777879@bp.route('/<int:id>/update', methods=('GET', 'POST'))80@login_required81def update(id):82 """Update a post if the current user is the author."""83 post = get_post(id)8485 if request.method == 'POST':86 title = request.form['title']87 body = request.form['body']88 error = None8990 if not title:91 error = 'Title is required.'9293 if error is not None:94 flash(error)95 else:96 db = get_db()97 db.execute(98 'UPDATE post SET title = ?, body = ? WHERE id = ?',99 (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'))