-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroar.py
237 lines (194 loc) · 6.67 KB
/
roar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from flask import Blueprint
from flask import flash
from flask import g
from flask import redirect
from flask import render_template
from flask import request
from flask import url_for
from werkzeug.exceptions import abort
import random
from auth import login_required
from db import get_db
bp = Blueprint("roar", __name__)
@bp.route("/")
def index():
"""Show all the posts, most recent first."""
db = get_db()
posts = db.execute(
"""
select
p.id,
roar,
p.created,
author_id,
(select count(*) from applaud a WHERE a.post_id = p.id) applauds,
username
from post p join user u on p.author_id = u.id
order by p.created desc;
"""
).fetchall()
applauds = [] # dic with {post_number: True/False} whether the post has been liked by the person.
if g.user is not None: # if the person is signed in.
applauds_list = db.execute(
"SELECT post_id FROM applaud WHERE user_id = ?",
(g.user["id"],),
).fetchall()
[applauds.append(a["post_id"]) for a in applauds_list] # make a list of only the ID numbers.
return render_template("roar/index.html", posts=posts, applauds=applauds)
def get_post(id, check_author=True):
"""Get a post and its author by id.
Checks that the id exists and optionally that the current user is
the author.
:param id: id of post to get
:param check_author: require the current user to be the author
:return: the post with author information
:raise 404: if a post with the given id doesn't exist
:raise 403: if the current user isn't the author
"""
post = (
get_db()
.execute(
"SELECT p.id, roar, p.created, author_id, username"
" FROM post p JOIN user u ON p.author_id = u.id"
" WHERE p.id = ?",
(id,),
)
.fetchone()
)
if post is None:
abort(404, f"Post id {id} doesn't exist.")
if check_author and post["author_id"] != g.user["id"]:
abort(403)
return post
@bp.route("/create", methods=("GET", "POST"))
@login_required
def create():
"""Create a new post for the current user."""
if request.method == "POST":
roar = request.form["roar"]
error = None
if not roar:
error = "Put some VOLUME in your ROAR."
if error is not None:
flash(error)
else:
db = get_db()
db.execute(
"INSERT INTO post (roar, author_id) VALUES (?, ?)",
(roar, g.user["id"]),
)
db.commit()
return redirect(url_for("roar.index"))
return render_template("roar/create.html")
@bp.route("/<int:id>/update", methods=("GET", "POST"))
@login_required
def update(id):
"""Update a post if the current user is the author."""
post = get_post(id)
if request.method == "POST":
roar = request.form["roar"]
error = None
if not roar:
error = "Put some SOUND in your ROAR."
if error is not None:
flash(error)
else:
db = get_db()
db.execute(
"UPDATE post SET roar = ? WHERE id = ?", (roar, id)
)
db.commit()
return redirect(url_for("roar.index"))
return render_template("roar/update.html", post=post)
@bp.route("/<int:id>/delete", methods=("POST",))
@login_required
def delete(id):
"""Delete a post.
Ensures that the post exists and that the logged in user is the
author of the post.
"""
get_post(id)
db = get_db()
db.execute("DELETE FROM post WHERE id = ?", (id,))
db.commit()
return redirect(url_for("roar.index"))
@bp.route("/applaud", methods=("GET",))
@login_required
def applaud():
post_id = request.args.get('post_id', 0, type=int)
user_id = g.user["id"]
previous_applauds = get_db().execute(
"SELECT * FROM applaud WHERE post_id = ? AND user_id = ?",
(post_id, user_id),
).fetchall()
db = get_db()
if len(previous_applauds) == 0:
# not previously liked.
# add the person to the posts likers
db.execute(
"INSERT INTO applaud (user_id, post_id) VALUES (?, ?)",
(user_id, post_id),
)
db.commit()
else:
# UNlike this post for the person
# delete this person from the post's likes
db.execute("DELETE FROM applaud WHERE user_id = ? AND post_id = ?", (user_id, post_id))
db.commit()
return {}
@bp.route("/~<string:username>/applauds", methods=("GET", "POST"))
def show_account_applauds(username):
"""Show all the tweets from a username."""
db = get_db()
posts = db.execute(
"""
select
p.id,
roar,
p.created,
author_id,
(select count(*) from applaud a WHERE a.post_id = p.id) applauds,
username
from
applaud a
join post p on p.id = a.post_id
join user u on u.id = a.user_id
where username = ?;
""",
(username,)).fetchall()
if len(posts) == 0:
print('no liked posts')
# return abort(404)
applauds = [] # dic with {post_number: True/False} whether the post has been liked by the person.
if g.user is not None: # if the person is signed in.
applauds_list = db.execute(
"SELECT post_id FROM applaud WHERE user_id = ?",
(g.user["id"],),
).fetchall()
[applauds.append(a["post_id"]) for a in applauds_list] # make a list of only the ID numbers.
return render_template("roar/show_account/base.html", applauds=applauds, username=username, posts=posts)
@bp.route("/~<string:username>", methods=("GET", "POST"))
def show_account(username):
"""Show all the tweets from a username."""
db = get_db()
posts = db.execute(
"""
select
p.id,
roar,
p.created,
author_id,
(select count(*) from applaud a WHERE a.post_id = p.id) applauds,
username
from post p join user u on p.author_id = u.id
where username = ?
order by p.created desc;
""",
(username,)).fetchall()
if posts is None:
return abort(404)
return render_template("roar/show_account/base.html", username=username, posts=posts)
@bp.errorhandler(404)
def page_not_found(e):
# note that we set the 404 status explicitly
return "<h1>404</h1><p>Chap, you made a mistake typing that URL. Here's a link <a href='/'>home</a>.</p>", 404