-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
240 lines (195 loc) · 7.58 KB
/
app.py
File metadata and controls
240 lines (195 loc) · 7.58 KB
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
238
239
240
from flask import Flask, redirect, render_template, request, session, abort, flash, make_response, jsonify, g
import db, users, config
import markupsafe
import posts
import math, secrets
import sqlite3
import time
app = Flask(__name__)
app.secret_key = config.secret_key
@app.template_filter()
def show_lines(content):
content = str(markupsafe.escape(content))
content = content.replace("\n", "<br />")
return markupsafe.Markup(content)
@app.template_filter()
def filter_empty_days(content_days):
return [day for day in content_days if day.strip()]
def require_login():
if "user_id" not in session:
abort(403)
def check_csrf():
if request.form["csrf_token"] != session["csrf_token"]:
abort(403)
@app.route('/')
def index():
offset = int(request.args.get('offset', 0))
limit = int(request.args.get('limit', 10))
paginated_posts = posts.fetch_posts(offset, limit)
return render_template('index.html', posts=paginated_posts, offset=offset, limit=limit)
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "GET":
return render_template("register.html", filled={})
if request.method == "POST":
username = request.form["username"]
if not username or len(username) > 16:
abort(403)
password1 = request.form["password1"]
password2 = request.form["password2"]
if password1 != password2:
flash("Passwords do not match")
filled = {"username": username}
return render_template("register.html", filled=filled)
try:
users.create_user(username, password1)
flash("Registering successful. You can now log in")
return redirect("/")
except sqlite3.IntegrityError:
flash("Username taken")
filled = {"username": username}
return render_template("register.html", filled=filled)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "GET":
return render_template("login.html")
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
user_id = users.check_login(username, password)
if user_id:
session["user_id"] = user_id
session["csrf_token"] = secrets.token_hex(16)
return redirect("/")
else:
flash("Wrong username or password")
return render_template("login.html")
@app.route("/logout")
def logout():
require_login()
del session["user_id"]
return redirect("/")
@app.route("/new_post", methods=["GET", "POST"])
def new_post():
if request.method == "GET":
require_login()
return render_template("new_post.html")
if request.method == "POST":
check_csrf()
require_login()
title = request.form["title"]
tag = request.form["tag"]
content_days = [request.form.get(f'content_day{i}', '') for i in range(1, 8)]
if not title or len(title) > 100:
flash("Title must be between 1 and 100 characters")
return render_template("new_post.html")
for i, content in enumerate(content_days, 1):
if len(content) > 50:
flash(f"Day {i} content must not exceed 50 characters")
return render_template("new_post.html")
user_id = session["user_id"]
post_id = posts.add_post(title, content_days, tag, user_id)
return redirect("/")
@app.route('/posts')
def get_posts():
offset = int(request.args.get('offset', 0))
limit = int(request.args.get('limit', 10))
paginated_posts = posts.fetch_posts(offset, limit)
return jsonify(paginated_posts)
@app.route("/image/<int:post_id>")
def show_image(post_id):
image = posts.get_image(post_id)
if not image:
return redirect("/static/default.jpg")
response = make_response(bytes(image))
response.headers.set("Content-Type", "image/jpeg")
return response
@app.route("/post/<int:post_id>/delete", methods=["POST"])
def delete_post(post_id):
require_login()
check_csrf()
post = posts.fetch_post(post_id)
if not post or post["user_id"] != session["user_id"]:
abort(403)
posts.remove_post(post["id"])
return redirect("/")
@app.route("/post/<int:post_id>/edit", methods=["GET", "POST"])
def edit_post(post_id):
if request.method == "GET":
require_login()
post = posts.fetch_post(post_id)
if not post or post["user_id"] != session["user_id"]:
abort(403)
filled = {
"title": post["title"],
"tag": post["tag"],
"content_day1": post["content_day1"],
"content_day2": post["content_day2"],
"content_day3": post["content_day3"],
"content_day4": post["content_day4"],
"content_day5": post["content_day5"],
"content_day6": post["content_day6"],
"content_day7": post["content_day7"]
}
return render_template("edit.html", filled=filled, post_id=post_id)
if request.method == "POST":
require_login()
check_csrf()
post = posts.fetch_post(post_id)
if not post or post["user_id"] != session["user_id"]:
abort(403)
title = request.form["title"]
tag = request.form["tag"]
content_days = [request.form.get(f'content_day{i}', '') for i in range(1, 8)]
if not title or len(title) > 100:
abort(403)
user_id = session["user_id"]
posts.update_post(title, tag, content_days, user_id, post_id)
return redirect(f"/post/{post_id}")
@app.route("/post/<int:post_id>")
def show_post(post_id):
post = posts.fetch_post(post_id)
user_id = session.get("user_id")
if not post:
abort(404)
total_votes = posts.get_post_votes(post_id)
return render_template("post.html", post=post, user_id=user_id, total_votes=total_votes)
@app.route("/search")
def search():
query = request.args.get("query")
offset = int(request.args.get("offset", 0))
limit = int(request.args.get("limit", 10))
results_paginated = posts.search(query, offset, limit) if query else []
return render_template("search.html", query=query, results=results_paginated, offset=offset, limit=limit)
@app.route("/user/<int:user_id>")
def profile(user_id):
offset = int(request.args.get("offset", 0))
limit = int(request.args.get("limit", 10))
user_posts = posts.user_posts(user_id, offset, limit)
user = users.get_user(user_id)
total_posts = posts.total_posts_count(user_id)
received_upvotes = posts.get_received_upvotes(user_id)
sent_votes = posts.get_sent_votes(user_id)
return render_template("profile.html", posts=user_posts, user=user, offset=offset, limit=limit, total_posts=total_posts, received_upvotes=received_upvotes, sent_votes=sent_votes)
@app.route("/post/<int:post_id>/vote", methods=["POST"])
def vote(post_id):
require_login()
check_csrf()
user_id = session.get("user_id")
vote = request.form.get("vote")
if user_id is None:
abort(400)
existing_vote = posts.get_user_vote(user_id, post_id)
if existing_vote is not None:
flash("You have already voted on this post.")
return redirect(f"/post/{post_id}")
posts.vote(user_id, post_id, vote)
return redirect(f"/post/{post_id}")
@app.before_request
def before_request():
g.start_time = time.time()
@app.after_request
def after_request(response):
elapsed_time = round(time.time() - g.start_time, 5)
print("elapsed time:", elapsed_time, "s")
return response