This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
125 lines (100 loc) · 3.73 KB
/
main.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
# Lucas Bubner, 2022
from flask import Flask, render_template, redirect, request
from os import environ
from sqlite3 import connect
from functools import wraps
from waitress import serve
app = Flask(__name__)
sql = connect("posts.db", check_same_thread=False)
posts = sql.cursor()
logged_in = False
def login_required(f):
@wraps(f)
def check(*args, **kwargs):
if not logged_in:
print("> debug | login_required check failed.")
return redirect("/login")
return f(*args, **kwargs)
return check
@app.route('/')
def index():
rows = posts.execute("SELECT id, title, content, img, imgheight FROM posts").fetchall()
print("> debug | fetched sql tables containing post information.")
return render_template("index.html",
logged_in = logged_in,
posts = rows)
@app.route("/addpost", methods=['GET', 'POST'])
@login_required
def addpost():
if request.method == "POST":
data = [
request.form.get("title"),
request.form.get("content"),
request.form.get("image"),
request.form.get("imageheight")
]
posts.execute("INSERT INTO posts(title, content, img, imgheight) VALUES(?,?,?,?)", data)
print("> debug | updating db with new data...")
sql.commit()
return redirect("/")
else:
return render_template("add.html", logged_in = logged_in)
@app.route("/delete/<int:id>", methods=['GET', 'POST'])
@login_required
def delete(id):
if request.method == "POST":
posts.execute("DELETE FROM posts WHERE id = ?", (id,))
print("> debug | removing database id {}".format(id))
sql.commit()
return redirect("/")
@app.route("/edit/<int:id>", methods=['GET', 'POST'])
@login_required
def editpost(id):
if request.method == "POST":
post = posts.execute("SELECT id, title, content, img, imgheight FROM posts WHERE id =?", (id,)).fetchone()
if not (title := request.form.get("title")):
title = post[1]
if not (content := request.form.get("content")):
content = post[2]
if not (image := request.form.get("image")):
image = post[3]
if not (imageheight := request.form.get("imageheight")):
imageheight = post[4]
data = [
title,
content,
image,
imageheight,
id
]
posts.execute("UPDATE posts SET title = ?, content = ?, img = ?, imgheight = ? WHERE id = ?", data)
print("> debug | updating db with new data...")
sql.commit()
return redirect("/")
else:
toedit = posts.execute("SELECT id, title, content, img, imgheight FROM posts WHERE id = ?", (id,)).fetchone()
print("> debug | fetching edits for a post...")
return render_template("edit.html", logged_in = logged_in, post = toedit)
@app.route("/login", methods=['GET', 'POST'])
def login():
global logged_in
if request.method == 'POST':
password = request.form.get("password")
if password == environ['password']:
logged_in = True
print("> debug | logged in successfully")
return redirect("/")
print("> debug | failed login")
return render_template("login.html",
incorrect = True)
else:
return render_template("login.html")
@app.route("/logout")
def logout():
global logged_in
logged_in = False
print("> debug | logged out successfully")
return redirect("/")
if __name__ == '__main__':
print("> APP INIT | running on http://127.0.0.1:8080/")
serve(app, host='0.0.0.0', port=8080)