Finish set_questions endpoint
This commit is contained in:
parent
2968997381
commit
6c07221b7d
@ -1,5 +1,5 @@
|
||||
import flask
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import select, delete, or_
|
||||
|
||||
from truthinquiry.ext.database.models import *
|
||||
from truthinquiry.ext.database.fsa import db
|
||||
@ -18,8 +18,8 @@ def get_questions():
|
||||
select(QuestionType, Text)
|
||||
.select_from(QuestionType)
|
||||
.join(Locale)
|
||||
.join(Text)
|
||||
.filter(Text.LANG==lang)
|
||||
.join(Text, isouter=True)
|
||||
.filter(or_(Text.LANG==None, Text.LANG==lang))
|
||||
.order_by(QuestionType.QUESTION_TYPE_ID)
|
||||
)
|
||||
|
||||
@ -31,7 +31,8 @@ def get_questions():
|
||||
old_question_type_id = question_type.QUESTION_TYPE_ID
|
||||
data.append([])
|
||||
|
||||
data[-1].append({"text": locale.TEXT})
|
||||
if locale:
|
||||
data[-1].append({"text": locale.TEXT})
|
||||
|
||||
return data
|
||||
|
||||
@ -40,5 +41,33 @@ 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"]
|
||||
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}
|
||||
|
@ -6,7 +6,8 @@
|
||||
{%endfor%}
|
||||
</select>
|
||||
|
||||
<form id="questionsTag">
|
||||
|
||||
<form id="questionsTag" action="javascript:void(0);">
|
||||
</form>
|
||||
|
||||
<button onclick="saveForm()"> Save changes </button>
|
||||
@ -24,13 +25,14 @@
|
||||
<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){
|
||||
for(let questionTypeTag of questionsTag.querySelectorAll("fieldset")){
|
||||
let questionTypeJson = [];
|
||||
questionsJson.push(questionTypeJson);
|
||||
|
||||
@ -59,16 +61,25 @@
|
||||
let questionTypeTag = document.createElement("fieldset")
|
||||
questionTypeTag.className = 'questionTypeTag';
|
||||
questionsTag.appendChild(questionTypeTag);
|
||||
|
||||
let i = 0;
|
||||
for(let question of questionType){
|
||||
|
||||
|
||||
|
||||
function addNewInput(value=""){
|
||||
let questionTag = document.createElement("input");
|
||||
questionTypeTag.appendChild(questionTag);
|
||||
|
||||
questionTag.value = question.text;
|
||||
|
||||
i++;
|
||||
questionTag.value = value;
|
||||
}
|
||||
|
||||
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;
|
||||
@ -80,5 +91,4 @@
|
||||
|
||||
changeLang(langs.value);
|
||||
|
||||
|
||||
</script>
|
||||
|
Loading…
Reference in New Issue
Block a user