feat: admin: create subtask and upload file
This commit is contained in:
1 parent
36196a736a
commit
29ebab923d
5 files changed
+158
-31
No files matched your search
@@ -1,5 +1,6 @@
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, session, jsonify, Response, current_app, abort
|
||||
import json
|
||||
from werkzeug.utils import secure_filename
|
||||
from pathlib import Path
|
||||
import random
|
||||
import csv
|
||||
@@ -7,9 +8,12 @@ import hmac
|
||||
import io
|
||||
import os
|
||||
import time
|
||||
import re
|
||||
import uuid
|
||||
from functools import wraps
|
||||
from db import init_db, get_db, close_db, create_participant, now
|
||||
from datetime import datetime, timezone
|
||||
import threading
|
||||
|
||||
bp = Blueprint("main", __name__)
|
||||
|
||||
@@ -19,11 +23,40 @@ bp.teardown_app_request(close_db)
|
||||
BASE_DIR = Path(__file__).parent
|
||||
BASE_MUSIC_DIR = BASE_DIR / "static" / "data" / "music"
|
||||
BASE_MUSIC_FILENAME_DIR = "data/music/"
|
||||
SONGS_PATH = BASE_DIR / "data" / "songs.json"
|
||||
_songs_lock = threading.Lock()
|
||||
|
||||
def load_songs_list():
|
||||
with open(BASE_DIR / "data" / "songs.json", encoding="utf-8") as f:
|
||||
with open(SONGS_PATH, encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def save_songs_list(songs):
|
||||
tmp = SONGS_PATH.with_suffix(".json.tmp")
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
json.dump(songs, f, ensure_ascii=False, indent=2)
|
||||
os.replace(tmp, SONGS_PATH) # atomar ersetzen
|
||||
|
||||
def next_song_id(songs, prefix="", width=3):
|
||||
nums = []
|
||||
for s in songs:
|
||||
m = re.fullmatch(rf"{prefix}(\d+)", str(s["id"]))
|
||||
if m:
|
||||
nums.append(int(m.group(1)))
|
||||
return f"{prefix}{max(nums, default=0) + 1:0{width}d}"
|
||||
|
||||
def add_song(song):
|
||||
with _songs_lock:
|
||||
songs = load_songs_list()
|
||||
song["id"] = next_song_id(songs)
|
||||
songs.append(song)
|
||||
save_songs_list(songs)
|
||||
return song
|
||||
|
||||
ALLOWED_EXTENSIONS = {"mp3", "wav", "flac", "ogg", "m4a"}
|
||||
|
||||
def allowed_file(filename):
|
||||
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
def session_required(api=False, participant=True):
|
||||
"""Prüft Datenschutz-Zustimmung und (optional) angelegten Teilnehmer.
|
||||
|
||||
@@ -275,4 +308,34 @@ def debug_reset():
|
||||
@bp.route("/admin/add")
|
||||
@admin_required
|
||||
def admin_add():
|
||||
return render_template("admin_add.html")
|
||||
return render_template("admin_add.html")
|
||||
|
||||
@bp.route("/api/admin/add", methods=["POST"])
|
||||
@admin_required
|
||||
def api_admin_add():
|
||||
title = request.form.get("title", "").strip()
|
||||
artist = request.form.get("artist", "").strip()
|
||||
file = request.files.get("file")
|
||||
|
||||
if not title:
|
||||
return jsonify({"saved": False, "error": "Titel fehlt"}), 400
|
||||
if not file or file.filename == "":
|
||||
return jsonify({"saved": False, "error": "Keine Datei"}), 400
|
||||
if not allowed_file(file.filename):
|
||||
return jsonify({"saved": False, "error": "Dateityp nicht erlaubt"}), 400
|
||||
|
||||
ext = os.path.splitext(secure_filename(file.filename))[1].lower()
|
||||
filename_new = uuid.uuid4().hex + ext
|
||||
|
||||
BASE_MUSIC_DIR.mkdir(parents=True, exist_ok=True)
|
||||
file.save(BASE_MUSIC_DIR / filename_new)
|
||||
|
||||
stem = os.path.splitext(filename_new)[0]
|
||||
song = add_song({
|
||||
"name": title,
|
||||
"artist": artist,
|
||||
"original": {"filename": stem},
|
||||
"edits": [],
|
||||
})
|
||||
|
||||
return jsonify({"saved": True, "filename": filename_new})
|
||||
Reference in new issue
Block a user