-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
200 lines (174 loc) · 5.49 KB
/
app.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
from flask import (
Flask,
render_template,
request,
redirect,
make_response,
)
import os
from dotenv import load_dotenv
from flask_sitemap import Sitemap
from common.auth import check_token
from common.artist import Artist
import upgrade_db
import mimetypes
import subprocess
from common.db import get_db_connection
import blueprints.admin
import blueprints.artists
import blueprints.auth
import blueprints.comics
import blueprints.series
import blueprints.create
upgrade_db.upgrade_if_needed()
load_dotenv()
SITEMAP_URLS = (
"index",
"auth.login",
"artists.list",
"series.list",
"rss",
"auth.create_account",
"tos",
)
maxlogintime = os.getenv("MAX_LOGIN_TIME")
if maxlogintime is not None:
if not maxlogintime.isnumeric():
print("invalid config!")
exit()
else:
print("invalid config!")
exit()
if os.getenv("SECRET_KEY") is None:
print("invalid config!")
exit()
if os.getenv("SERVER_ADDRESS") is None:
print("invalid config!")
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
app.config["UPLOAD_FOLDER"] = "static/comics"
app.config["MAX_LOGIN_TIME"] = int(maxlogintime)
app.config["ALLOWED_EXTENSIONS"] = ("png", "jpg", "jpeg", "gif", "tiff", "webp")
app.config["SERVER_ADDRESS"] = os.getenv("SERVER_ADDRESS")
ext = Sitemap(app=app)
@app.context_processor
def inject_version_info():
return dict(
COMMIT_ID=subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"]
).decode(),
VERSION=subprocess.check_output(
["git", "describe", "--tags", "--abbrev=0"]
).decode(),
)
app.register_blueprint(blueprints.admin.bp)
app.register_blueprint(blueprints.artists.bp)
app.register_blueprint(blueprints.auth.bp)
app.register_blueprint(blueprints.comics.bp)
app.register_blueprint(blueprints.create.bp)
app.register_blueprint(blueprints.series.bp)
@app.route("/")
@check_token(app)
def index(login_artist: Artist | None):
conn = get_db_connection()
page = request.args.get("page", type=int)
if page is None:
page = 0
else:
page -= 1
# pages are 50 items long
offset = page * 50
# request one more comic than we need, purely to see if it exists
comics = conn.execute(
"SELECT * FROM comics ORDER BY created DESC LIMIT 51 OFFSET ?",
(offset,),
).fetchall()
# if there are more comics, allow going to the next page
# and clean up the extra comic
if len(comics) > 50:
comics.pop()
allownext = True
else:
# if not, do not allow going to the next page
allownext = False
artists = conn.execute("SELECT id, username FROM artists").fetchall()
# convert the artist to a dict for easy searching
artistdict = {}
for other_artist in artists:
artistdict.update({other_artist[0]: other_artist[1]})
series_db = conn.execute("SELECT id, name FROM series").fetchall()
seriesdict = {}
# convert the series to a dict for easy searching
for series in series_db:
seriesdict.update({series[0]: series[1]})
conn.close()
return render_template(
"index.jinja",
comics=comics,
artists=artistdict,
series=seriesdict,
page=page + 1,
allownext=allownext,
login_artist=login_artist,
)
@app.route("/feed")
def indexrssfeed():
conn = get_db_connection()
comics = conn.execute("SELECT * FROM comics ORDER BY created DESC").fetchall()
series_db = conn.execute("SELECT id, name FROM series").fetchall()
seriesdict = {}
# convert the series to a dict for easy searching
for series in series_db:
seriesdict.update({series[0]: series[1]})
artists = conn.execute("SELECT id, username FROM artists").fetchall()
# convert the artist to a dict for easy searching
artistdict = {}
for other_artist in artists:
artistdict.update({other_artist[0]: other_artist[1]})
conn.close()
response = make_response(
render_template(
"rss/index.jinja",
comics=comics,
series=seriesdict,
artists=artistdict,
types_map=mimetypes.types_map,
)
)
response.headers["Content-Type"] = "application/xml"
return response
@app.route("/rss")
@check_token(app)
def rss(login_artist: Artist):
return render_template("rss.jinja", login_artist=login_artist)
@app.route("/terms-of-service")
@check_token(app)
def tos(login_artist: Artist):
return render_template("terms-of-service.jinja", login_artist=login_artist)
@app.route("/toggletheme")
def toggletheme():
if request.referrer is not None:
resp = make_response(redirect(request.referrer))
else:
resp = make_response(redirect("/"))
if request.cookies.get("darkmode", default=None) is None:
resp.set_cookie("darkmode", "true")
else:
resp.delete_cookie("darkmode")
return resp
@ext.register_generator
def sitemap():
for page in SITEMAP_URLS:
yield page, {}
conn = get_db_connection()
comics = conn.execute("SELECT id FROM comics ORDER BY created DESC").fetchall()
for comic in comics:
yield "comics.comic", {"comic_id": comic[0]}
artists = conn.execute(
"SELECT username FROM artists ORDER BY created DESC"
).fetchall()
for artist in artists:
yield "artists.artist", {"artist": artist[0]}
series_db = conn.execute("SELECT name FROM series").fetchall()
for series in series_db:
yield "series.series", {"seriesName": series[0]}