Merge pull request #94 from ThomasRubini/back_office
This commit is contained in:
commit
709d4ced32
@ -10,7 +10,7 @@ from truthinquiry.ext.database import fsa
|
|||||||
from truthinquiry.ext.socketio import socket_io
|
from truthinquiry.ext.socketio import socket_io
|
||||||
from truthinquiry.ext.discord_bot import discord_bot
|
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):
|
def register_extensions(app):
|
||||||
fsa.setup_app_db(app)
|
fsa.setup_app_db(app)
|
||||||
@ -22,6 +22,8 @@ def register_extensions(app):
|
|||||||
def register_routes(app):
|
def register_routes(app):
|
||||||
app.register_blueprint(routes_api.routes_api, url_prefix="/api/v1")
|
app.register_blueprint(routes_api.routes_api, url_prefix="/api/v1")
|
||||||
app.register_blueprint(routes_ui.routes_ui, url_prefix="/")
|
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():
|
def create_app():
|
||||||
|
19
truthinquiry/routes/routes_admin.py
Normal file
19
truthinquiry/routes/routes_admin.py
Normal 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")
|
44
truthinquiry/routes/routes_api_admin.py
Normal file
44
truthinquiry/routes/routes_api_admin.py
Normal 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}
|
@ -6,13 +6,23 @@
|
|||||||
* @returns a Promise, which resolves when the server can be reached and responds without an error
|
* @returns a Promise, which resolves when the server can be reached and responds without an error
|
||||||
* and rejects otherwise
|
* and rejects otherwise
|
||||||
*/
|
*/
|
||||||
async function makeAPIRequest(endpoint, body) {
|
async function makeAPIRequest(endpoint, body, options={}) {
|
||||||
return new Promise((resolve, reject) => {
|
let fetchOptions = {
|
||||||
const fetchOptions = {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: new URLSearchParams(body)
|
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) => {
|
||||||
|
|
||||||
fetch("/api/v1/" + endpoint, fetchOptions).then(response => {
|
fetch("/api/v1/" + endpoint, fetchOptions).then(response => {
|
||||||
const responseCode = response.status;
|
const responseCode = response.status;
|
||||||
if (responseCode >= 500) {
|
if (responseCode >= 500) {
|
||||||
@ -22,12 +32,12 @@ async function makeAPIRequest(endpoint, body) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
response.json().then(jsonResponse => {
|
response.json().then(jsonResponse => {
|
||||||
if (jsonResponse["error"] === 0) {
|
if (typeof(jsonResponse["error"]) === 'number' && jsonResponse["error"] !== 0) {
|
||||||
resolve(jsonResponse);
|
|
||||||
} else {
|
|
||||||
const message = jsonResponse["msg"];
|
const message = jsonResponse["msg"];
|
||||||
alert("Erreur du serveur : " + message);
|
alert("Erreur du serveur : " + message);
|
||||||
reject(endpoint + ": " + message);
|
reject(endpoint + ": " + message);
|
||||||
|
} else {
|
||||||
|
resolve(jsonResponse);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
|
13
truthinquiry/templates/admin/index.html
Normal file
13
truthinquiry/templates/admin/index.html
Normal 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>
|
1
truthinquiry/templates/admin/npc.html
Normal file
1
truthinquiry/templates/admin/npc.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<a href="/admin"> go Back </a> <br>
|
1
truthinquiry/templates/admin/places.html
Normal file
1
truthinquiry/templates/admin/places.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<a href="/admin"> go Back </a> <br>
|
84
truthinquiry/templates/admin/questions.html
Normal file
84
truthinquiry/templates/admin/questions.html
Normal 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>
|
1
truthinquiry/templates/admin/reactions.html
Normal file
1
truthinquiry/templates/admin/reactions.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<a href="/admin"> go Back </a> <br>
|
Loading…
Reference in New Issue
Block a user