persistent cookies

This commit is contained in:
TriMill 2022-12-31 19:07:52 -05:00
parent e28f11e840
commit 6ed79fedc2

View file

@ -4,6 +4,7 @@ import markdown2
import datetime import datetime
import os import os
import ast import ast
import datetime
markdown_extras = ["fenced-code-blocks", "footnotes", "strike", "tables", "metadata"] markdown_extras = ["fenced-code-blocks", "footnotes", "strike", "tables", "metadata"]
@ -101,23 +102,26 @@ def create_app():
theme = request.cookies.get("theme") or "dark" theme = request.cookies.get("theme") or "dark"
return render_template("404.html", data=data, theme=theme), 404 return render_template("404.html", data=data, theme=theme), 404
def _load_page(url): def get_theme():
return request.args.get("theme") or request.cookies.get("theme") or "dark"
def set_theme(resp):
resp = make_response(resp)
if request.args.get("theme"):
resp.set_cookie("theme", request.args["theme"], expires=datetime.datetime.now() + datetime.timedelta(days=30))
return resp
def load_page(url):
if url.endswith(".html"): if url.endswith(".html"):
path = os.path.join(app.root_path, app.template_folder, url) path = os.path.join(app.root_path, app.template_folder, url)
if os.path.exists(path): if os.path.exists(path):
theme = request.args.get("theme") or request.cookies.get("theme") or "dark" theme = get_theme()
return render_template(url, data=data, theme=theme) return set_theme(render_template(url, data=data, theme=theme))
else: else:
return abort(404) return abort(404)
else: else:
return send_from_directory("templates", url) return send_from_directory("templates", url)
def load_page(url):
resp = make_response(_load_page(url))
if request.args.get("theme"):
resp.set_cookie("theme", request.args["theme"])
return resp
@app.route("/") @app.route("/")
@app.route("/index.html") @app.route("/index.html")
def home(): def home():
@ -135,11 +139,7 @@ def create_app():
@app.route("/blog/") @app.route("/blog/")
def blog_list(): def blog_list():
theme = request.args.get("theme") or request.cookies.get("theme") or "dark" return set_theme(render_template("blog.html", data=data, theme=theme))
resp = make_response(render_template("blog.html", data=data, theme=theme))
if request.args.get("theme"):
resp.set_cookie("theme", request.args["theme"])
return resp
@app.route("/blog/<int:y>/<int:m>/<int:d>/") @app.route("/blog/<int:y>/<int:m>/<int:d>/")
@app.route("/blog/<int:y>/<int:m>/<int:d>/<int:n>") @app.route("/blog/<int:y>/<int:m>/<int:d>/<int:n>")
@ -152,11 +152,8 @@ def create_app():
contents = f.read() contents = f.read()
content = markdown2.markdown(contents, extras=markdown_extras) content = markdown2.markdown(contents, extras=markdown_extras)
meta = content.metadata meta = content.metadata
theme = request.args.get("theme") or request.cookies.get("theme") or "dark" theme = get_theme()
resp = make_response(render_template("_blog.html", data=data, theme=theme, content=content, date=date, meta=meta)) return set_theme(render_template("_blog.html", data=data, theme=theme, content=content, date=date, meta=meta))
if request.args.get("theme"):
resp.set_cookie("theme", request.args["theme"])
return resp
else: else:
return abort(404) return abort(404)