Browse Source

Adds blog and template

Jose Reyes 5 years ago
parent
commit
446bd2d442

+ 97
- 0
flaskr/blog.py View File

@@ -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
+

+ 16
- 0
flaskr/templates/blog/create.html View File

@@ -0,0 +1,16 @@
1
+{% extends 'base.html' %}
2
+
3
+{% block header %}
4
+    <h1>{% block title %}New Post{% endblock %}</h1>
5
+{% endblock %}
6
+
7
+{% block content %}
8
+    <form method="post">
9
+        <label for="title">Title</label>
10
+        <input name="title" id="title" value="{{ request.form['title'] }}" required>
11
+        <label for="body">Body</label>
12
+        <textarea name="body" id="body">{{ request.form['body'] }}</textarea>
13
+        <input type="submit" value="Save">
14
+    </form>
15
+{% endblock %}
16
+

+ 29
- 0
flaskr/templates/blog/index.html View File

@@ -0,0 +1,29 @@
1
+{% extends 'base.html' %}
2
+
3
+{% block header %}
4
+    <h1>{% block title %}Posts{% endblock %}</h1>
5
+    {% if g.user %}
6
+        <a class="action" href="{{ url_for('blog.create') }}">New</a>
7
+    {% endif %}
8
+{% endblock %}
9
+
10
+{% block content %}
11
+    {% for post in posts %}
12
+        <article class="post">
13
+            <header>
14
+                <div>
15
+                    <h1>{{ post['title'] }} </h1>
16
+                    <div class="about">by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}</div>
17
+                </div>
18
+                {% if g.user['id'] == post['author_id'] %}
19
+                    <a class="action" href="{{ url_for('blog.update', id=post['id']) }}">Edit</a>
20
+                {% endif %}
21
+            </header>
22
+            <p class="body">{{ post['body'] }}</p>
23
+        </article>
24
+        {% if not loop.last %}
25
+        <hr>
26
+        {% endif %}
27
+    {% endfor %}
28
+{% endblock %}
29
+

+ 21
- 0
flaskr/templates/blog/update.html View File

@@ -0,0 +1,21 @@
1
+{% extends 'base.html' %}
2
+
3
+{% block header %}
4
+	<h1>{% block title %}Edit "{{ post['title'] }}"{% endblock %}</h1>
5
+{% endblock %}
6
+
7
+{% block content %}
8
+	<form method="post">
9
+		<label for="title">Title</label>
10
+		<input name="title" id="title"
11
+		  value="{{ request.form['title'] or post['title'] }}" required>
12
+		<label for="body">Body</label>
13
+		<textarea name="body" id="body">{{ request.form['body'] or post['body'] }}</textarea>
14
+		<input type="submit" value="Save">
15
+	</form>
16
+	<hr>
17
+	<form action="{{ url_for('blog.delete', id=post['id']) }}" method="post">
18
+		<input class="danger" type="submit" value="Delete" onclick="return confirm('Are you sure?');">
19
+	</form>
20
+{% endblock %}
21
+