Finish set_questions endpoint
This commit is contained in:
parent
2968997381
commit
6c07221b7d
@ -1,5 +1,5 @@
|
|||||||
import flask
|
import flask
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select, delete, or_
|
||||||
|
|
||||||
from truthinquiry.ext.database.models import *
|
from truthinquiry.ext.database.models import *
|
||||||
from truthinquiry.ext.database.fsa import db
|
from truthinquiry.ext.database.fsa import db
|
||||||
@ -18,8 +18,8 @@ def get_questions():
|
|||||||
select(QuestionType, Text)
|
select(QuestionType, Text)
|
||||||
.select_from(QuestionType)
|
.select_from(QuestionType)
|
||||||
.join(Locale)
|
.join(Locale)
|
||||||
.join(Text)
|
.join(Text, isouter=True)
|
||||||
.filter(Text.LANG==lang)
|
.filter(or_(Text.LANG==None, Text.LANG==lang))
|
||||||
.order_by(QuestionType.QUESTION_TYPE_ID)
|
.order_by(QuestionType.QUESTION_TYPE_ID)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,7 +31,8 @@ def get_questions():
|
|||||||
old_question_type_id = question_type.QUESTION_TYPE_ID
|
old_question_type_id = question_type.QUESTION_TYPE_ID
|
||||||
data.append([])
|
data.append([])
|
||||||
|
|
||||||
data[-1].append({"text": locale.TEXT})
|
if locale:
|
||||||
|
data[-1].append({"text": locale.TEXT})
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
@ -40,5 +41,33 @@ def set_questions():
|
|||||||
if not flask.request.json:
|
if not flask.request.json:
|
||||||
return {"error": 1, "msg": "no json set"}
|
return {"error": 1, "msg": "no json set"}
|
||||||
lang = flask.request.json["lang"]
|
lang = flask.request.json["lang"]
|
||||||
questions = flask.request.json["questions"]
|
question_types = flask.request.json["questions"]
|
||||||
|
|
||||||
|
# Delete old questions
|
||||||
|
text_ids_requ = (
|
||||||
|
select(Locale.LID)
|
||||||
|
.join(Text).where(Text.LANG==lang)
|
||||||
|
.join(QuestionType)
|
||||||
|
)
|
||||||
|
db.session.execute(
|
||||||
|
delete(Text)
|
||||||
|
.where(Text.LID.in_(text_ids_requ.subquery()))
|
||||||
|
)
|
||||||
|
|
||||||
|
# get question LIDs in database
|
||||||
|
question_lids_requ = (
|
||||||
|
select(QuestionType.TEXT_LID)
|
||||||
|
.join(Locale)
|
||||||
|
)
|
||||||
|
question_lids = (data[0] for data in db.session.execute(question_lids_requ))
|
||||||
|
|
||||||
|
# set new questions
|
||||||
|
text_obs = []
|
||||||
|
for question_lid, questions in zip(question_lids, question_types):
|
||||||
|
for question in questions:
|
||||||
|
text_obs.append(Text(None, question_lid, lang, question["text"]))
|
||||||
|
|
||||||
|
db.session.add_all(text_obs)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
return {"error": 0}
|
return {"error": 0}
|
||||||
|
@ -6,7 +6,8 @@
|
|||||||
{%endfor%}
|
{%endfor%}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<form id="questionsTag">
|
|
||||||
|
<form id="questionsTag" action="javascript:void(0);">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<button onclick="saveForm()"> Save changes </button>
|
<button onclick="saveForm()"> Save changes </button>
|
||||||
@ -24,13 +25,14 @@
|
|||||||
<script src="/static/js/api.js"></script>
|
<script src="/static/js/api.js"></script>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
|
||||||
let lang = null;
|
let lang = null;
|
||||||
|
|
||||||
function saveForm(){
|
function saveForm(){
|
||||||
var formData = new FormData(questionsTag);
|
var formData = new FormData(questionsTag);
|
||||||
let questionsJson = [];
|
let questionsJson = [];
|
||||||
|
|
||||||
for(let questionTypeTag of questionsTag.children){
|
for(let questionTypeTag of questionsTag.querySelectorAll("fieldset")){
|
||||||
let questionTypeJson = [];
|
let questionTypeJson = [];
|
||||||
questionsJson.push(questionTypeJson);
|
questionsJson.push(questionTypeJson);
|
||||||
|
|
||||||
@ -59,16 +61,25 @@
|
|||||||
let questionTypeTag = document.createElement("fieldset")
|
let questionTypeTag = document.createElement("fieldset")
|
||||||
questionTypeTag.className = 'questionTypeTag';
|
questionTypeTag.className = 'questionTypeTag';
|
||||||
questionsTag.appendChild(questionTypeTag);
|
questionsTag.appendChild(questionTypeTag);
|
||||||
|
|
||||||
let i = 0;
|
|
||||||
for(let question of questionType){
|
|
||||||
|
function addNewInput(value=""){
|
||||||
let questionTag = document.createElement("input");
|
let questionTag = document.createElement("input");
|
||||||
questionTypeTag.appendChild(questionTag);
|
questionTypeTag.appendChild(questionTag);
|
||||||
|
|
||||||
questionTag.value = question.text;
|
questionTag.value = value;
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(let question of questionType){
|
||||||
|
addNewInput(question.text);
|
||||||
|
}
|
||||||
|
|
||||||
|
let addNewTag = document.createElement("input")
|
||||||
|
addNewTag.type = 'button'
|
||||||
|
addNewTag.value = "Add new";
|
||||||
|
addNewTag.onclick = () => addNewInput();
|
||||||
|
questionsTag.appendChild(addNewTag);
|
||||||
}
|
}
|
||||||
|
|
||||||
lang = newLang;
|
lang = newLang;
|
||||||
@ -80,5 +91,4 @@
|
|||||||
|
|
||||||
changeLang(langs.value);
|
changeLang(langs.value);
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
Reference in New Issue
Block a user