moved config to a json, which makes adding more variables easier, but perhaps otherwise adds complexity

This commit is contained in:
Ville Rantanen
2023-07-28 13:08:54 +03:00
parent 80af9c321c
commit f1c453d3d4
18 changed files with 258 additions and 182 deletions

View File

@@ -1,9 +1,8 @@
from version import VERSION
# TODO put in config table
THREADS_PER_PAGE = 50
from flask import Flask, render_template, session, request, redirect, url_for, flash, g
from db.sqlite import DB
from db.config import Config
import os, sys, subprocess
import passlib.hash, secrets
import time
@@ -13,33 +12,33 @@ import captcha, password, minimd
app = Flask(__name__)
db = DB(os.getenv("DB"))
config = Config(os.getenv("CONF"))
# This defaults to None, which allows CSRF attacks in FireFox
# and older versions of Chrome.
# 'Lax' is sufficient to prevent malicious POST requests.
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
app.config["SECRET_KEY"] = config.secret_key
class Config:
pass
config = Config()
(
config.version,
config.server_name,
config.server_description,
app.config["SECRET_KEY"],
config.captcha_key,
config.registration_enabled,
config.login_required
) = db.get_config()
config.user_css = os.path.exists(os.path.join(app.static_folder, 'user.css'))
# ~ class Config:
# ~ pass
# ~ config = Config()
# ~ (
# ~ config.version,
# ~ config.server_name,
# ~ config.server_description,
# ~ app.config["SECRET_KEY"],
# ~ config.captcha_key,
# ~ config.registration_enabled,
# ~ config.login_required
# ~ ) = db.get_config()
# ~ app.config['user_css'] = os.path.exists(os.path.join(app.static_folder, 'user.css'))
# ~ config.threads_per_page = 50
if config.version != VERSION:
print(f"Incompatible version {config.version} (expected {VERSION})")
sys.exit(1)
class Role:
USER = 0
MODERATOR = 1
@@ -50,11 +49,10 @@ class Role:
def before_request():
if config.login_required:
user_id = session.get("user_id", -1)
if user_id == -1 and request.endpoint not in ("login","static"):
if user_id == -1 and request.endpoint not in ("login", "static"):
return redirect(url_for("login"))
@app.after_request
def after_request(response):
# This forbids other sites from embedding this site in an iframe,
@@ -80,10 +78,10 @@ def forum(forum_id):
title, description = db.get_forum(forum_id)
offset = int(request.args.get("p", 0))
user_id = session.get("user_id", -1)
threads = [*db.get_threads(forum_id, offset, THREADS_PER_PAGE + 1, user_id)]
if len(threads) == THREADS_PER_PAGE + 1:
threads = [*db.get_threads(forum_id, offset, config.threads_per_page + 1, user_id)]
if len(threads) == config.threads_per_page + 1:
threads.pop()
next_page = offset + THREADS_PER_PAGE
next_page = offset + config.threads_per_page
else:
next_page = None
return render_template(
@@ -95,7 +93,7 @@ def forum(forum_id):
description=description,
threads=threads,
next_page=next_page,
prev_page=max(offset - THREADS_PER_PAGE, 0) if offset > 0 else None,
prev_page=max(offset - config.threads_per_page, 0) if offset > 0 else None,
)
@@ -111,7 +109,7 @@ def thread(thread_id):
modify_time,
comments,
hidden,
forum_id
forum_id,
) = db.get_thread(thread_id)
forum_title, _ = db.get_forum(forum_id)
@@ -155,7 +153,7 @@ def comment(comment_id):
parent_id=parent_id,
thread_id=thread_id,
forum_id=forum_id,
forum_title=forum_title
forum_title=forum_title,
)
@@ -532,11 +530,14 @@ def admin_edit_config():
return user
try:
db.set_config(
request.form["server_name"],
trim_text(request.form["server_description"]),
"registration_enabled" in request.form,
"login_required" in request.form,
# db.set_config(
config.set_config(
server_name=request.form["server_name"],
server_description=trim_text(request.form["server_description"]),
registration_enabled="registration_enabled" in request.form,
login_required="login_required" in request.form,
threads_per_page=int(request.form["threads_per_page"]),
user_css=request.form["user_css"],
)
flash("Updated config. Refresh the page to see the changes.", "success")
restart()
@@ -554,7 +555,8 @@ def admin_new_secrets():
secret_key = secrets.token_urlsafe(30)
captcha_key = secrets.token_urlsafe(30)
try:
db.set_config_secrets(secret_key, captcha_key)
# ~ db.set_config_secrets(secret_key, captcha_key)
config.set_config_secrets(secret_key, captcha_key)
flash("Changed secrets. You will be logged out.", "success")
restart()
except Exception as e:
@@ -776,7 +778,7 @@ def create_comment_tree(comments, user):
# Sort each comment based on create time
def sort_time(l):
l.sort(key=lambda c: c.modify_time, reverse=True)
l.sort(key=lambda c: c.modify_time, reverse=False)
for c in l:
sort_time(c.children)