-
Notifications
You must be signed in to change notification settings - Fork 5
/
personal_website.py
executable file
·257 lines (212 loc) · 7.44 KB
/
personal_website.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# personal_website.py
#
# Copyright 2021 Thomas Castleman <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
"""This is my personal website"""
import flask
import os
import json
import wiki
APP = flask.Flask(__name__)
@APP.route("/")
def main():
"""Handle the root directory of the website"""
return flask.render_template("index.html")
@APP.route("/3d-printing")
def three_d():
"""3D printing stuffs"""
return flask.render_template("3d.html")
@APP.route("/anime")
def anime():
"""A Weeb's favorite pass time"""
with open("anime.json", "r") as file:
data = json.load(file)
watched = convert_to_html_list(data["Watched"])
watching = convert_to_html_list(data["Watching"])
on_hold = convert_to_html_list(data["On Hold"])
ptw = convert_to_html_list(data["To Watch"])
output = flask.render_template("anime.html", on_hold=on_hold, to_watch=ptw,
watched=watched, watching=watching)
output = output.replace("<", "<")
output = output.replace(">", ">")
return output
def convert_to_html_list(obj):
"""Convert a Python 1D list to an HTML unordered list"""
output = ""
for each in obj:
output = output + f"<li>{each}</li>\n"
return output
@APP.route("/software")
def software():
"""Cause I'm a nerd on multiple levels"""
return flask.render_template("software.html")
@APP.route("/assets/<path:path>")
def static_dir(path):
if ".." in path:
return flask.redirect(flask.url_for("forbidden"))
if path not in os.listdir("assets"):
return flask.redirect(flask.url_for("page_not_found"))
return flask.send_from_directory("assets", path)
@APP.errorhandler(404)
def error_404(e):
return page_not_found()
@APP.errorhandler(403)
def error_403(e):
return forbidden()
@APP.route("/404")
def page_not_found():
return flask.render_template('404.html'), 404
@APP.route("/403")
def forbidden():
return flask.render_template('403.html'), 403
@APP.route("/favicon.ico")
def favicon():
return static_dir("favicon.png")
@APP.route("/cook_book")
def wiki_homepage(show=None):
"""This is be the wiki homepage and search
if show is None, then wiki shows the 10 most recent posts
otherwise, show should be a list of post titles
if show is not None, a list
"""
posts_template = """<a href="/cook_book/{ title }"><h2>{ title }</h2></a>
<h4>Written { written } by { author }</h4>
<p>{ synopsis }</p>
</br>"""
tags_template = '<input type="checkbox" name="%s" value="1"> %s'
if show is None:
posts = wiki.list_posts()[:10]
else:
if isinstance(show, (list, tuple)):
posts = show
else:
raise TypeError(f"{ show } is not None, a list, or a tuple.")
tags = wiki.get_all_tags()
posts_parse_in = []
for each in posts:
post = wiki.get_post_metadata(each)
new = posts_template.replace("{ title }", each)
new = new.replace("{ written }", post["WRITTEN"])
new = new.replace("{ synopsis }", post["SYNOPSIS"])
if "EDITOR" in post:
new = new.replace("{ author }",
f"""{ ', '.join(post['AUTHOR']) }, Edited by { ', '.join(post['EDITOR']) }""")
else:
new = new.replace("{ author }", ", ".join(post["AUTHOR"]))
posts_parse_in.append(new)
# generate tags search GUI
tags_parse_in = []
for each in tags:
tags_parse_in.append(tags_template % (each, each))
# make each element
count = 0
row_width = 5
tags_gui = []
add = []
for each in tags_parse_in:
entry = f" <td> { each } </td> "
if count < row_width:
add.append(entry)
count += 1
else:
tags_gui.append(add)
add = []
add.append(entry)
count = 1
if add:
tags_gui.append(add)
# combine elements into rows
newline = "\n" # it is entirely ridiculous that I have to do this due to a SyntaxError
for each in enumerate(tags_gui):
tags_gui[each[0]] = f"<tr>{ newline.join(each[1]) }</tr>"
# combine rows into table
tags_gui = f"""<table>{ newline.join(tags_gui) }</table>"""
# parse post previews into page
if len(posts_parse_in) > 0:
posts_parse_in = "\n</br>\n".join(posts_parse_in)
page = flask.render_template("wiki-home.html")
page = page.replace("{ content }", posts_parse_in)
else:
page = flask.render_template("wiki-home-none.html")
# parse tag info into page
page = page.replace("{ tags }", tags_gui)
page = page.replace("{ tags_list }",
f"""<input type="hidden" id="tags_list" name="tags_list" value="{ ",".join(tags) }">""")
# send the page to the user
return page
@APP.route("/cook_book/search", methods=["POST"])
def wiki_search():
"""Search the wiki"""
tags_list = flask.request.form.get("tags_list").split(",")
if tags_list is not None:
tags = {}
for each in tags_list:
tags[each] = bool(flask.request.form.get(each))
for each in range(len(tags_list) - 1, -1, -1):
if not tags[tags_list[each]]:
del tags_list[each]
del tags
output = list(wiki.search_tags(tags_list).keys())
else:
output = wiki.list_posts()
search_text = flask.request.form.get("free_text").split(" ")
if search_text != [""]:
output = wiki.search_freetext(search_text, output)
tbs = get_toggle_button_values(flask.request.form)
for each in tbs:
if tbs[each] is not None:
output = wiki.search_flags(each, output, tbs[each])
return wiki_homepage(show=output)
def get_toggle_button_values(form):
"""Get value from toggle buttons for search"""
data = {"GLUTEN-FREE": None, "VEGAN": None, "VEGETARIAN": None,
"KOSHER": None}
# Gluten
if form.get("state-g") == "true":
data["GLUTEN-FREE"] = True
elif form.get("state-g") == "false":
data["GLUTEN-FREE"] = False
# Vegan
if form.get("state-d") == "true":
data["VEGAN"] = True
elif form.get("state-d") == "false":
data["VEGAN"] = False
# Vegetarian
if form.get("state-v") == "true":
data["VEGETARIAN"] = True
elif form.get("state-v") == "false":
data["VEGETARIAN"] = False
# Kosher
if form.get("state-k") == "true":
data["KOSHER"] = True
elif form.get("state-k") == "false":
data["KOSHER"] = False
return data
@APP.route("/cook_book/<title>")
def wiki_post(title):
"""Provide rendered wiki posts"""
try:
return wiki.get_post(title)
except FileNotFoundError:
return page_not_found()
if __name__ == "__main__":
APP.run(host="0.0.0.0", debug=False)