diff --git a/truthinquiry/ext/database/models.py b/truthinquiry/ext/database/models.py index c872d9b..3535f5c 100644 --- a/truthinquiry/ext/database/models.py +++ b/truthinquiry/ext/database/models.py @@ -57,7 +57,7 @@ class Locale(Base): texts.append(text) return texts - def get_text(self, lang, auto_create): + def get_text(self, lang, auto_create=False): for text in self.TEXTS: if text.LANG == lang: return text @@ -80,7 +80,7 @@ class Place(Base): __tablename__ = 'T_PLACE' PLACE_ID = Column(Integer, primary_key=True, autoincrement=True, comment="ID of this place") NAME_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Place name") - LOCALE = relationship("Locale") + NAME_LOCALE = relationship("Locale") def __init__(self, PLACE_ID, NAME_LID): self.PLACE_ID = PLACE_ID @@ -101,7 +101,7 @@ class QuestionType(Base): __tablename__ = "T_QUESTION_TYPE" QUESTION_TYPE_ID = Column(Integer, default=0, primary_key=True, comment="ID of this question type.") TEXT_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Question text") - LOCALE = relationship("Locale") + TEXT_LOCALE = relationship("Locale") def __init__(self, QUESTION_TYPE_ID, TEXT_LID): self.QUESTION_TYPE_ID = QUESTION_TYPE_ID @@ -124,7 +124,7 @@ class Answer(Base): QUESTION_TYPE_ID = Column(Integer, ForeignKey("T_QUESTION_TYPE.QUESTION_TYPE_ID"), primary_key=True, comment="Question type ID") NPC_ID = Column(Integer, ForeignKey("T_NPC.NPC_ID"), primary_key=True, comment="ID of the NPC that will say this answer") TEXT_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Text of the answer") - LOCALE = relationship("Locale") + TEXT_LOCALE = relationship("Locale") NPC = relationship("Npc", backref="ANSWERS") def __init__(self, QUESTION_TYPE_ID, NPC_ID, TEXT_LID): @@ -149,7 +149,7 @@ class Npc(Base): NPC_ID = Column(Integer, autoincrement=True, primary_key=True, comment="ID of this Npc") NAME_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Name of this Npc") DEFAULT_IMG = Column(LargeBinary(length=2**24), comment="Binary data of the default image of this Npc") - LOCALE = relationship("Locale") + NAME_LOCALE = relationship("Locale") def __init__(self, NPC_ID, NAME_LID): self.NPC_ID = NPC_ID @@ -171,8 +171,8 @@ class Trait(Base): NAME_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Name of this trait") DESC_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Description of this trait") - Name = relationship("Locale",foreign_keys=[NAME_LID]) - Desc = relationship("Locale",foreign_keys=[DESC_LID]) + NAME_LOCALE = relationship("Locale",foreign_keys=[NAME_LID]) + DESC_LOCALE = relationship("Locale",foreign_keys=[DESC_LID]) def __init__(self, TRAIT_ID, NAME_LID, DESC_LID): diff --git a/truthinquiry/routes/routes_admin.py b/truthinquiry/routes/routes_admin.py index e20db2b..4a03368 100644 --- a/truthinquiry/routes/routes_admin.py +++ b/truthinquiry/routes/routes_admin.py @@ -6,10 +6,12 @@ from truthinquiry.ext.database.fsa import db routes_admin = flask.Blueprint("admin", __name__) +DEFAULT_LANG = "FR" + @routes_admin.route("/") def index(): npcs_objs = db.session.query(Npc).all() - npcs_dicts = [{"id": npc_obj.NPC_ID, "name": npc_obj.LOCALE.TEXTS[0].TEXT} for npc_obj in npcs_objs] + npcs_dicts = [{"id": npc_obj.NPC_ID, "name": npc_obj.NAME_LOCALE.get_text(DEFAULT_LANG).TEXT} for npc_obj in npcs_objs] return flask.render_template("admin/index.html", npcs=npcs_dicts) @routes_admin.route("/npc/") @@ -21,12 +23,12 @@ def npc(npc_id): npc_answers = [] for answer_type in npc_obj.ANSWERS: - answer_list = [answer.TEXT for answer in answer_type.LOCALE.TEXTS] + answer_list = [answer.TEXT for answer in answer_type.TEXT_LOCALE.TEXTS] npc_answers.append(answer_list) npc_dict = { "id": npc_obj.NPC_ID, - "name": npc_obj.LOCALE.TEXTS[0].TEXT, + "name": npc_obj.NAME_LOCALE.get_text(DEFAULT_LANG).TEXT, "img": npc_obj.NPC_ID, "answers": npc_answers, } @@ -35,7 +37,7 @@ def npc(npc_id): @routes_admin.route("/questions") def questions(): - lang = "FR" + lang = DEFAULT_LANG results = db.session.execute( select(QuestionType, Text) @@ -61,12 +63,16 @@ def questions(): @routes_admin.route("/places") def places(): + lang = DEFAULT_LANG + places_objs = db.session.query(Place).all() - places_dicts = [{"id": place_obj.PLACE_ID, "name": place_obj.LOCALE.TEXTS[0].TEXT} for place_obj in places_objs] + places_dicts = [{"id": place_obj.PLACE_ID, "name": place_obj.NAME_LOCALE.get_text(lang).TEXT} for place_obj in places_objs] return flask.render_template("admin/places.html", places=places_dicts) @routes_admin.route("/traits") def traits(): + lang = DEFAULT_LANG + traits_objs = db.session.query(Trait).all() - traits_dicts = [{"id": trait_obj.TRAIT_ID, "name": trait_obj.Name.TEXTS[0].TEXT, "desc": trait_obj.Desc.TEXTS[0].TEXT} for trait_obj in traits_objs] + traits_dicts = [{"id": trait_obj.TRAIT_ID, "name": trait_obj.NAME_LOCALE.get_text(lang).TEXT, "desc": trait_obj.DESC_LOCALE.get_text(lang).TEXT} for trait_obj in traits_objs] return flask.render_template("admin/traits.html", traits=traits_dicts) diff --git a/truthinquiry/routes/routes_api_admin.py b/truthinquiry/routes/routes_api_admin.py index 87a052a..3b88cef 100644 --- a/truthinquiry/routes/routes_api_admin.py +++ b/truthinquiry/routes/routes_api_admin.py @@ -57,10 +57,10 @@ def set_traits(): # modify db_trait = list(filter(lambda db_trait: db_trait.TRAIT_ID == int(input_trait["id"]), db_traits))[0] - db.session.delete(db_trait.Name.TEXTS[0]) - db.session.delete(db_trait.Desc.TEXTS[0]) - db_trait.Name.TEXTS = [Text(None, None, input_lang, input_trait["name"])] - db_trait.Desc.TEXTS = [Text(None, None, input_lang, input_trait["desc"])] + db.session.delete(db_trait.NAME_LOCALE.get_text(input_lang)) + db.session.delete(db_trait.DESC_LOCALE.get_text(input_lang)) + db_trait.NAME_LOCALE.TEXTS = [Text(None, None, input_lang, input_trait["name"])] + db_trait.DESC_LOCALE.TEXTS = [Text(None, None, input_lang, input_trait["desc"])] db.session.add(db_trait) modified_db_traits.append(db_trait) @@ -68,11 +68,11 @@ def set_traits(): # add new_trait = Trait(None, None, None) - new_trait.Name = Locale(None) - new_trait.Desc = Locale(None) + new_trait.NAME_LOCALE = Locale(None) + new_trait.DESC_LOCALE = Locale(None) - new_trait.Name.TEXTS.append(Text(None, None, input_lang, input_trait["name"])) - new_trait.Desc.TEXTS.append(Text(None, None, input_lang, input_trait["desc"])) + new_trait.NAME_LOCALE.TEXTS.append(Text(None, None, input_lang, input_trait["name"])) + new_trait.DESC_LOCALE.TEXTS.append(Text(None, None, input_lang, input_trait["desc"])) db.session.add(new_trait) @@ -99,9 +99,9 @@ def set_places(): # modify db_place = list(filter(lambda db_place: db_place.PLACE_ID == int(input_place["id"]), db_places))[0] - db.session.delete(db_place.LOCALE.TEXTS[0]) + db.session.delete(db_place.NAME_LOCALE.get_text(input_lang)) - db_place.LOCALE.TEXTS = [Text(None, None, input_lang, input_place["name"])] + db_place.NAME_LOCALE.TEXTS = [Text(None, None, input_lang, input_place["name"])] db.session.add(db_place) modified_db_places.append(db_place) @@ -109,8 +109,8 @@ def set_places(): # add new_place = Place(None, None) - new_place.LOCALE = Locale(None) - new_place.LOCALE.TEXTS = [Text(None, None, input_lang, input_place["name"])] + new_place.NAME_LOCALE = Locale(None) + new_place.NAME_LOCALE.TEXTS = [Text(None, None, input_lang, input_place["name"])] db.session.add(new_place) @@ -134,13 +134,13 @@ def set_npc(): else: npc_obj = db.session.get(Npc, input_npc["id"]) - npc_obj.LOCALE.get_text(input_lang, True).TEXT = input_npc["name"] + npc_obj.NAME_LOCALE.get_text(input_lang, True).TEXT = input_npc["name"] for answer_type, input_answer_type in zip(npc_obj.ANSWERS, input_npc["allAnswers"]): - for text in answer_type.LOCALE.get_texts(input_lang): + for text in answer_type.TEXT_LOCALE.get_texts(input_lang): db.session.delete(text) for input_answer in input_answer_type["answers"]: - answer_type.LOCALE.TEXTS.append(Text(None, None, input_lang, input_answer["text"])) + answer_type.TEXT_LOCALE.TEXTS.append(Text(None, None, input_lang, input_answer["text"])) db.session.commit()