allow to add and delete reactions (backend)
This commit is contained in:
parent
1cd238b4ab
commit
93409c5e54
@ -1,7 +1,7 @@
|
||||
import os
|
||||
|
||||
import flask
|
||||
from sqlalchemy import select, delete, or_
|
||||
from sqlalchemy import select, delete, and_
|
||||
|
||||
from truthinquiry.ext.database.models import *
|
||||
from truthinquiry.ext.database.fsa import db
|
||||
@ -176,3 +176,36 @@ def delete_npc():
|
||||
db.session.execute(delete(Npc).where(Npc.NPC_ID==input_npc_id))
|
||||
db.session.commit()
|
||||
return {}
|
||||
|
||||
@routes_api_admin.route("/setReaction", methods=["GET", "POST"])
|
||||
@require_admin(api=True)
|
||||
def setReaction():
|
||||
input_npc_id = flask.request.values["npc_id"]
|
||||
input_trait_id = flask.request.values["trait_id"]
|
||||
|
||||
row = db.session.execute(
|
||||
select(Reaction)
|
||||
.where(and_(
|
||||
Reaction.NPC_ID==input_npc_id,
|
||||
Reaction.TRAIT_ID==input_trait_id
|
||||
))
|
||||
).first()
|
||||
|
||||
reaction = None if row == None else row[0]
|
||||
|
||||
|
||||
if len(flask.request.files) == 0: # want to delete
|
||||
if reaction:
|
||||
db.session.delete(reaction)
|
||||
else:
|
||||
return {"msg": "No such reaction"} # Not an error because this can be intentional
|
||||
else:
|
||||
input_reaction_file = flask.request.files['file']
|
||||
if not reaction:
|
||||
reaction = Reaction(None, input_npc_id, input_trait_id)
|
||||
db.session.add(reaction)
|
||||
reaction.IMG = input_reaction_file.read()
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return {}
|
||||
|
@ -1,4 +1,7 @@
|
||||
function createOrUpdateNpc() {
|
||||
|
||||
const reactionsDelta = {}
|
||||
|
||||
async function createOrUpdateNpc() {
|
||||
const data = {};
|
||||
data["id"] = document.querySelector("#npc_id").value;
|
||||
data["name"] = document.querySelector("#npc_name").value;
|
||||
@ -16,9 +19,36 @@ function createOrUpdateNpc() {
|
||||
});
|
||||
}
|
||||
|
||||
makeAPIRequest("admin/setNpc", {"npc": data, "lang": "FR"}, {"content": "json"}).then(() => {
|
||||
alert("Opération effectuée avec succès");
|
||||
});
|
||||
await makeAPIRequest("admin/setNpc", {"npc": data, "lang": "FR"}, {"content": "json"});
|
||||
|
||||
await uploadReactionsDelta();
|
||||
|
||||
alert("Opération effectuée avec succès");
|
||||
}
|
||||
|
||||
async function uploadReactionsDelta() {
|
||||
let requests = [];
|
||||
|
||||
|
||||
for(const [reactionId, reactionNode] of Object.entries(reactionsDelta)){
|
||||
const formData = new FormData();
|
||||
formData.append("npc_id", npc_id.value);
|
||||
formData.append("trait_id", reactionId);
|
||||
|
||||
if(reactionNode === null) formData.append("file", "null");
|
||||
else{
|
||||
const file = reactionNode.querySelector(".img_input").files[0]
|
||||
formData.append("file", file ? file : "");
|
||||
}
|
||||
|
||||
requests.push(makeAPIRequest("admin/setReaction", formData, {"content": "form"}));
|
||||
}
|
||||
|
||||
for(request of requests){
|
||||
await request;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
async function deleteNpc() {
|
||||
@ -35,12 +65,15 @@ async function deleteNpc() {
|
||||
function changeReaction(inputNode){
|
||||
const parentNode = inputNode.parentNode;
|
||||
const imgNode = parentNode.querySelector('img');
|
||||
const reactionId = parentNode.querySelector('.reaction_id').value;
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e)=>{
|
||||
imgNode.src = e.target.result
|
||||
}
|
||||
reader.readAsDataURL(inputNode.files[0]);
|
||||
|
||||
reactionsDelta[reactionId] = parentNode;
|
||||
}
|
||||
|
||||
function deleteReaction(node){
|
||||
@ -55,6 +88,8 @@ function deleteReaction(node){
|
||||
option.innerText = reactionName
|
||||
|
||||
reactions_to_add.appendChild(option);
|
||||
|
||||
reactionsDelta[reactionId] = null;
|
||||
}
|
||||
|
||||
function addReaction(selectNode){
|
||||
@ -72,4 +107,6 @@ function addReaction(selectNode){
|
||||
newReaction.querySelector("p").innerText = reactionName
|
||||
|
||||
reactions.appendChild(newReaction);
|
||||
|
||||
reactionsDelta[reactionId] = newReaction;
|
||||
}
|
@ -51,7 +51,7 @@
|
||||
|
||||
<select id="reactions_to_add" onchange="addReaction(this)">
|
||||
<option value="" default></option>
|
||||
{%for reaction_to_add in npc.get("reaction_to_add") or []%}
|
||||
{%for reaction_to_add in npc.get("reactions_to_add") or []%}
|
||||
<option value="{{reaction_to_add.get('id')}}">{{reaction_to_add.get('name')}}</option>
|
||||
{%endfor%}
|
||||
</select>
|
||||
|
Loading…
Reference in New Issue
Block a user