Merge pull request #94 from ThomasRubini/back_office

This commit is contained in:
Thomas Rubini 2023-03-12 12:58:44 +01:00 committed by GitHub
commit 709d4ced32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 184 additions and 9 deletions

View File

@ -10,7 +10,7 @@ from truthinquiry.ext.database import fsa
from truthinquiry.ext.socketio import socket_io
from truthinquiry.ext.discord_bot import discord_bot
from truthinquiry.routes import routes_api, routes_ui, routes_socketio, handlers
from truthinquiry.routes import routes_api, routes_ui, routes_socketio, routes_admin, routes_api_admin, handlers
def register_extensions(app):
fsa.setup_app_db(app)
@ -22,6 +22,8 @@ def register_extensions(app):
def register_routes(app):
app.register_blueprint(routes_api.routes_api, url_prefix="/api/v1")
app.register_blueprint(routes_ui.routes_ui, url_prefix="/")
app.register_blueprint(routes_admin.routes_admin, url_prefix="/admin")
app.register_blueprint(routes_api_admin.routes_api_admin, url_prefix="/api/v1/admin")
def create_app():

View File

@ -0,0 +1,19 @@
import flask
routes_admin = flask.Blueprint("admin", __name__)
@routes_admin.route("/")
def index():
return flask.render_template("admin/index.html")
@routes_admin.route("/npc/<npc_id>")
def npc(npc_id):
return flask.render_template("admin/npc.html")
@routes_admin.route("/questions")
def questions():
return flask.render_template("admin/questions.html", langs=["FR", "EN"])
@routes_admin.route("/places")
def places():
return flask.render_template("admin/places.html")

View File

@ -0,0 +1,44 @@
import flask
from sqlalchemy import select
from truthinquiry.ext.database.models import *
from truthinquiry.ext.database.fsa import db
routes_api_admin = flask.Blueprint("api_admin", __name__)
@routes_api_admin.route("/getQuestions", methods=["GET", "POST"])
def get_questions():
lang = flask.request.values.get("lang")
if lang is None:
return {"error": 1, "msg": "lang not set"}
results = db.session.execute(
select(QuestionType, Text)
.select_from(QuestionType)
.join(Locale)
.join(Text)
.filter(Text.LANG==lang)
.order_by(QuestionType.QUESTION_TYPE_ID)
)
data = []
old_question_type_id = None
for question_type, locale in results:
if question_type.QUESTION_TYPE_ID != old_question_type_id:
old_question_type_id = question_type.QUESTION_TYPE_ID
data.append([])
data[-1].append({"text": locale.TEXT})
return data
@routes_api_admin.route("/setQuestions", methods=["GET", "POST"])
def set_questions():
if not flask.request.json:
return {"error": 1, "msg": "no json set"}
lang = flask.request.json["lang"]
questions = flask.request.json["questions"]
return {"error": 0}

View File

@ -6,12 +6,22 @@
* @returns a Promise, which resolves when the server can be reached and responds without an error
* and rejects otherwise
*/
async function makeAPIRequest(endpoint, body) {
async function makeAPIRequest(endpoint, body, options={}) {
let fetchOptions = {
method: "POST",
headers: {
'Accept': 'application/json'
}
};
if (options["content"] === 'json') {
fetchOptions["headers"]["Content-Type"] = 'application/json'
fetchOptions["body"] = JSON.stringify(body)
} else {
fetchOptions["body"] = new URLSearchParams(body);
}
return new Promise((resolve, reject) => {
const fetchOptions = {
method: "POST",
body: new URLSearchParams(body)
};
fetch("/api/v1/" + endpoint, fetchOptions).then(response => {
const responseCode = response.status;
@ -22,12 +32,12 @@ async function makeAPIRequest(endpoint, body) {
}
response.json().then(jsonResponse => {
if (jsonResponse["error"] === 0) {
resolve(jsonResponse);
} else {
if (typeof(jsonResponse["error"]) === 'number' && jsonResponse["error"] !== 0) {
const message = jsonResponse["msg"];
alert("Erreur du serveur : " + message);
reject(endpoint + ": " + message);
} else {
resolve(jsonResponse);
}
});
}).catch((e) => {

View File

@ -0,0 +1,13 @@
<a href="/admin/questions"> Questions </a>
<br>
<a href="/admin/places"> Places </a>
<br>
<a href="/admin/reactions"> Reactions </a>
<section>
<p> NPC list :</p>
<a href="/admin/npc/1"> Diva </a>
<br>
<a href="/admin/npc/2"> Barron </a>
<br>
<a href="/admin/npc/3"> Machin </a>
</section>

View File

@ -0,0 +1 @@
<a href="/admin"> go Back </a> <br>

View File

@ -0,0 +1 @@
<a href="/admin"> go Back </a> <br>

View File

@ -0,0 +1,84 @@
<a href="/admin"> go Back </a> <br>
<select id="langs" onchange="langChangedEvent()">
{%for lang in langs%}
<option value="{{lang}}">{{lang}}</option>
{%endfor%}
</select>
<form id="questionsTag">
</form>
<button onclick="saveForm()"> Save changes </button>
<style>
.questionTypeTag{
border: thin solid red;
margin-top: 20px;
}
.questionTypeTag input{
width: 100%;
}
</style>
<script src="/static/js/api.js"></script>
<script>
let lang = null;
function saveForm(){
var formData = new FormData(questionsTag);
let questionsJson = [];
for(let questionTypeTag of questionsTag.children){
let questionTypeJson = [];
questionsJson.push(questionTypeJson);
for(let questionTag of questionTypeTag.children){
questionTypeJson.push({"text": questionTag.value})
}
}
if(lang!==null){
makeAPIRequest("admin/setQuestions", {"questions": questionsJson, "lang": lang}, {"content": "json"})
}
}
let lastQueriedData = [];
async function changeLang(newLang){
lang = null;
console.log("Changing language to "+newLang);
resp = await makeAPIRequest("admin/getQuestions", {"lang": newLang});
questionsTag.innerHTML = '';
for(let questionType of resp){
let questionTypeTag = document.createElement("fieldset")
questionTypeTag.className = 'questionTypeTag';
questionsTag.appendChild(questionTypeTag);
let i = 0;
for(let question of questionType){
let questionTag = document.createElement("input");
questionTypeTag.appendChild(questionTag);
questionTag.value = question.text;
i++;
}
}
lang = newLang;
}
function langChangedEvent(){
changeLang(langs.value)
}
changeLang(langs.value);
</script>

View File

@ -0,0 +1 @@
<a href="/admin"> go Back </a> <br>