diff --git a/truthinquiry/routes/routes_api_admin.py b/truthinquiry/routes/routes_api_admin.py index 8e2881b..ea0fdad 100644 --- a/truthinquiry/routes/routes_api_admin.py +++ b/truthinquiry/routes/routes_api_admin.py @@ -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} diff --git a/truthinquiry/templates/admin/questions.html b/truthinquiry/templates/admin/questions.html index 1061e07..6ff811b 100644 --- a/truthinquiry/templates/admin/questions.html +++ b/truthinquiry/templates/admin/questions.html @@ -6,7 +6,8 @@ {%endfor%} -
@@ -24,13 +25,14 @@