add badges
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from flask import Flask, session, request, render_template
|
||||
from dotenv import load_dotenv
|
||||
from .models import Avatars, Users, PostHistory, Posts, MOTD
|
||||
from .models import Avatars, Users, PostHistory, Posts, MOTD, BadgeUploads
|
||||
from .auth import digest
|
||||
from .routes.users import is_logged_in, get_active_user, get_prefers_theme
|
||||
from .routes.threads import get_post_url
|
||||
@@ -16,6 +16,7 @@ import os
|
||||
import time
|
||||
import secrets
|
||||
import tomllib
|
||||
import json
|
||||
|
||||
def create_default_avatar():
|
||||
if Avatars.count() == 0:
|
||||
@@ -99,6 +100,31 @@ def reparse_babycode():
|
||||
|
||||
print('Re-parsing done.')
|
||||
|
||||
def bind_default_badges(path):
|
||||
from .db import db
|
||||
with db.transaction():
|
||||
potential_stales = BadgeUploads.get_default()
|
||||
d = os.listdir(path)
|
||||
for bu in potential_stales:
|
||||
if os.path.basename(bu.file_path) not in d:
|
||||
print(f'Deleted stale default badge{os.path.basename(bu.file_path)}')
|
||||
bu.delete()
|
||||
|
||||
for f in d:
|
||||
real_path = os.path.join(path, f)
|
||||
if not os.path.isfile(real_path):
|
||||
continue
|
||||
if not f.endswith('.webp'):
|
||||
continue
|
||||
proxied_path = f'/static/badges/{f}'
|
||||
bu = BadgeUploads.find({'file_path': proxied_path})
|
||||
if not bu:
|
||||
BadgeUploads.create({
|
||||
'file_path': proxied_path,
|
||||
'uploaded_at': int(os.path.getmtime(real_path)),
|
||||
})
|
||||
|
||||
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
app.config['SITE_NAME'] = 'Pyrom'
|
||||
@@ -123,9 +149,12 @@ def create_app():
|
||||
app.config["SECRET_KEY"] = os.getenv("FLASK_SECRET_KEY")
|
||||
|
||||
app.config['AVATAR_UPLOAD_PATH'] = 'data/static/avatars/'
|
||||
app.config['BADGES_PATH'] = 'data/static/badges/'
|
||||
app.config['BADGES_UPLOAD_PATH'] = 'data/static/badges/user/'
|
||||
app.config['MAX_CONTENT_LENGTH'] = 3 * 1000 * 1000 # 3M total, subject to further limits per route
|
||||
|
||||
os.makedirs(os.path.dirname(app.config["DB_PATH"]), exist_ok = True)
|
||||
os.makedirs(os.path.dirname(app.config["BADGES_UPLOAD_PATH"]), exist_ok = True)
|
||||
|
||||
css_dir = 'data/static/css/'
|
||||
allowed_themes = []
|
||||
@@ -150,6 +179,8 @@ def create_app():
|
||||
|
||||
reparse_babycode()
|
||||
|
||||
bind_default_badges(app.config['BADGES_PATH'])
|
||||
|
||||
from app.routes.app import bp as app_bp
|
||||
from app.routes.topics import bp as topics_bp
|
||||
from app.routes.threads import bp as threads_bp
|
||||
@@ -237,6 +268,10 @@ def create_app():
|
||||
for id_, text in matches
|
||||
]
|
||||
|
||||
@app.template_filter('basename_noext')
|
||||
def basename_noext(subj):
|
||||
return os.path.splitext(os.path.basename(subj))[0]
|
||||
|
||||
@app.errorhandler(404)
|
||||
def _handle_404(e):
|
||||
if request.path.startswith('/hyperapi/'):
|
||||
@@ -269,4 +304,8 @@ def create_app():
|
||||
|
||||
return f'{subject.removeprefix('theme-').replace('-', ' ').capitalize()} (beta)'
|
||||
|
||||
@app.template_filter('fromjson')
|
||||
def fromjson(subject: str):
|
||||
return json.loads(subject)
|
||||
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user