from flask import Flask, render_template, url_for, request, redirect, send_file from datetime import datetime import json import os import io app = Flask(__name__) basedir = os.path.abspath(os.path.dirname(__file__)) jsonFile = os.path.join(basedir, 'static/questions.json') def getQuestions(): with open(jsonFile, "r", encoding="utf-8") as jsonData: wordList = json.load(jsonData) return wordList @app.route("/") def index (): year = datetime.now().year return render_template("main.html", questions=getQuestions(), year=year) @app.route("/fragen") def editQuestions(): year = datetime.now().year return render_template("questions.html", questions=buildStrings(), year=year) @app.route("/fragen/submit", methods=["POST"]) def questionPost(): appended = appendWordList(request.form.get("question")) return redirect(url_for("editQuestions", success=appended)) @app.route("/fragen/export") def exportQuestions(): exportStr = {'Questions' : buildStrings(), 'Lists': getQuestions()} jsonData = json.dumps(exportStr, ensure_ascii=False, indent=4) buffer = io.BytesIO() buffer.write(jsonData.encode('utf-8')) buffer.seek(0) return send_file( buffer, as_attachment=True, download_name='FragenDHMTM.json', mimetype='application/json' ) def appendWordList (newQuestion: str): wordList = getQuestions() newWords = newQuestion.split() nrWords = len(newWords) nrSegments = len(wordList) if nrWords < nrSegments: if nrWords != 3: for i in range(nrSegments-nrWords): newWords.insert(0, "") else: for i in range(3): newWords.insert(0, "") newWords += [""] elif nrWords > nrSegments: perSegment = nrWords//nrSegments moreWords = nrWords%nrSegments longQuestion = [] wordIndex = 0 for i in range(1, nrSegments+1): subword = "" targetLen = perSegment + 1 if i <= moreWords else perSegment for e in range(targetLen): subword += newWords[wordIndex] if i == nrSegments else newWords[wordIndex] + " " wordIndex+=1 longQuestion += [subword] newWords = longQuestion for i in range(nrSegments): wordList[i] += [newWords[i]] with open(jsonFile, "w") as jsonData: json.dump(wordList, jsonData) return 200 @app.route("/fragen/delete/") def delete (index: int): index = int(index) wordList = getQuestions() for i in wordList: i.pop(index) with open(jsonFile, "w") as jsonData: json.dump(wordList, jsonData) return redirect(url_for("editQuestions")) @app.route("/fragen/load/") def loadFromJSON(type: str): fileStr = os.path.join(basedir, "FragenDHMTM.json") with open(fileStr, "r", encoding="utf-8") as jsonData: fileDict = json.load(jsonData) fileQuestions = fileDict["Questions"] fileLists = fileDict["Lists"] if type.lower() == "questions": for i in range(len(buildStrings())): delete(0) for question in fileQuestions: appendWordList(question) elif type.lower() == "lists": with open(jsonFile, "w+", encoding="utf-8") as jsonData: json.dump(fileLists, jsonData) return redirect(url_for("editQuestions")) def buildStrings(): wordList = getQuestions() nrSegments = len(wordList) nrQuestions = len(wordList[0]) questionList = [] for i in range (nrQuestions): currentQuestion = "" for e in range (nrSegments): currentQuestion += wordList[e][i] currentQuestion += " " if (currentQuestion[:-1] != " ") and (e!=nrSegments-1) else "" questionList += [currentQuestion.strip()] return questionList if __name__ == "__main__": app.run(host='0.0.0.0', port=2025, debug=True)