|
@@ -0,0 +1,97 @@
|
|
1
|
+from flask import (
|
|
2
|
+ Blueprint, flash, g, redirect, render_template, request, url_for
|
|
3
|
+)
|
|
4
|
+from werkzeug.exceptions import abort
|
|
5
|
+
|
|
6
|
+from flaskr.auth import login_required
|
|
7
|
+from flaskr.db import get_db
|
|
8
|
+
|
|
9
|
+bp = Blueprint('blog', __name__)
|
|
10
|
+
|
|
11
|
+@bp.route('/')
|
|
12
|
+def index():
|
|
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)
|
|
20
|
+
|
|
21
|
+@bp.route('/create', methods=('GET', 'POST'))
|
|
22
|
+@login_required
|
|
23
|
+def create():
|
|
24
|
+ if request.method == 'POST':
|
|
25
|
+ title = request.form['title']
|
|
26
|
+ body = request.form['body']
|
|
27
|
+ error = None
|
|
28
|
+
|
|
29
|
+ if not title:
|
|
30
|
+ error = 'Title is required.'
|
|
31
|
+
|
|
32
|
+ if error is not None:
|
|
33
|
+ flash(error)
|
|
34
|
+ else:
|
|
35
|
+ db = get_db()
|
|
36
|
+ db.execute(
|
|
37
|
+ 'INSERT INTO post (title, body, author_id)'
|
|
38
|
+ ' VALUES (?, ?, ?)',
|
|
39
|
+ (title, body, g.user['id'])
|
|
40
|
+ )
|
|
41
|
+ db.commit()
|
|
42
|
+ return redirect(url_for('blog.index'))
|
|
43
|
+
|
|
44
|
+ return render_template('blog/create.html')
|
|
45
|
+
|
|
46
|
+def get_post(id, check_author=True):
|
|
47
|
+ post = get_db().execute(
|
|
48
|
+ 'SELECT p.id, title, body, created, author_id, username'
|
|
49
|
+ ' FROM post p JOIN user u ON p.author_id = u.id'
|
|
50
|
+ ' WHERE p.id = ?',
|
|
51
|
+ (id,)
|
|
52
|
+ ).fetchone()
|
|
53
|
+
|
|
54
|
+ if post is None:
|
|
55
|
+ abort(404, "Post id {0} doesn't exist.".format(id))
|
|
56
|
+
|
|
57
|
+ if check_author and post['author_id'] != g.user['id']:
|
|
58
|
+ abort(403)
|
|
59
|
+
|
|
60
|
+ return post
|
|
61
|
+
|
|
62
|
+@bp.route('/<int:id>/update', methods=('GET', 'POST'))
|
|
63
|
+@login_required
|
|
64
|
+def update(id):
|
|
65
|
+ post = get_post(id)
|
|
66
|
+
|
|
67
|
+ if request.method == 'POST':
|
|
68
|
+ title = request.form['title']
|
|
69
|
+ body = request.form['body']
|
|
70
|
+ error = None
|
|
71
|
+
|
|
72
|
+ if not title:
|
|
73
|
+ error = 'Title is required.'
|
|
74
|
+
|
|
75
|
+ if error is not None:
|
|
76
|
+ flash(error)
|
|
77
|
+ else:
|
|
78
|
+ db = get_db()
|
|
79
|
+ db.execute(
|
|
80
|
+ 'UPDATE post SET title = ?, body = ?'
|
|
81
|
+ ' WHERE id = ?',
|
|
82
|
+ (title, body, id)
|
|
83
|
+ )
|
|
84
|
+ db.commit()
|
|
85
|
+ return redirect(url_for('blog.index'))
|
|
86
|
+
|
|
87
|
+ return render_template('blog/update.html', post=post)
|
|
88
|
+
|
|
89
|
+@bp.route('/<int:id>/delete', methods=('POST',))
|
|
90
|
+@login_required
|
|
91
|
+def delete(id):
|
|
92
|
+ get_post(id)
|
|
93
|
+ db = get_db()
|
|
94
|
+ db.execute('DELETE FROM post WHERE id = ?', (id,))
|
|
95
|
+ db.commit()
|
|
96
|
+ return redirect(url_for('blog.index'))
|
|
97
|
+
|