Merge pull request #117 from ThomasRubini/models_refactor

This commit is contained in:
Thomas Rubini 2023-03-25 20:42:23 +01:00 committed by GitHub
commit 45ae1de69b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 28 deletions

View File

@ -57,7 +57,7 @@ class Locale(Base):
texts.append(text) texts.append(text)
return texts return texts
def get_text(self, lang, auto_create): def get_text(self, lang, auto_create=False):
for text in self.TEXTS: for text in self.TEXTS:
if text.LANG == lang: if text.LANG == lang:
return text return text
@ -80,7 +80,7 @@ class Place(Base):
__tablename__ = 'T_PLACE' __tablename__ = 'T_PLACE'
PLACE_ID = Column(Integer, primary_key=True, autoincrement=True, comment="ID of this 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") 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): def __init__(self, PLACE_ID, NAME_LID):
self.PLACE_ID = PLACE_ID self.PLACE_ID = PLACE_ID
@ -101,7 +101,7 @@ class QuestionType(Base):
__tablename__ = "T_QUESTION_TYPE" __tablename__ = "T_QUESTION_TYPE"
QUESTION_TYPE_ID = Column(Integer, default=0, primary_key=True, comment="ID of this 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") 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): def __init__(self, QUESTION_TYPE_ID, TEXT_LID):
self.QUESTION_TYPE_ID = QUESTION_TYPE_ID 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") 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") 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") 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") NPC = relationship("Npc", backref="ANSWERS")
def __init__(self, QUESTION_TYPE_ID, NPC_ID, TEXT_LID): 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") 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") 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") 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): def __init__(self, NPC_ID, NAME_LID):
self.NPC_ID = NPC_ID 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") 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") DESC_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Description of this trait")
Name = relationship("Locale",foreign_keys=[NAME_LID]) NAME_LOCALE = relationship("Locale",foreign_keys=[NAME_LID])
Desc = relationship("Locale",foreign_keys=[DESC_LID]) DESC_LOCALE = relationship("Locale",foreign_keys=[DESC_LID])
def __init__(self, TRAIT_ID, NAME_LID, DESC_LID): def __init__(self, TRAIT_ID, NAME_LID, DESC_LID):

View File

@ -6,10 +6,12 @@ from truthinquiry.ext.database.fsa import db
routes_admin = flask.Blueprint("admin", __name__) routes_admin = flask.Blueprint("admin", __name__)
DEFAULT_LANG = "FR"
@routes_admin.route("/") @routes_admin.route("/")
def index(): def index():
npcs_objs = db.session.query(Npc).all() 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) return flask.render_template("admin/index.html", npcs=npcs_dicts)
@routes_admin.route("/npc/<npc_id>") @routes_admin.route("/npc/<npc_id>")
@ -21,12 +23,12 @@ def npc(npc_id):
npc_answers = [] npc_answers = []
for answer_type in npc_obj.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_answers.append(answer_list)
npc_dict = { npc_dict = {
"id": npc_obj.NPC_ID, "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, "img": npc_obj.NPC_ID,
"answers": npc_answers, "answers": npc_answers,
} }
@ -35,7 +37,7 @@ def npc(npc_id):
@routes_admin.route("/questions") @routes_admin.route("/questions")
def questions(): def questions():
lang = "FR" lang = DEFAULT_LANG
results = db.session.execute( results = db.session.execute(
select(QuestionType, Text) select(QuestionType, Text)
@ -61,12 +63,16 @@ def questions():
@routes_admin.route("/places") @routes_admin.route("/places")
def places(): def places():
lang = DEFAULT_LANG
places_objs = db.session.query(Place).all() 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) return flask.render_template("admin/places.html", places=places_dicts)
@routes_admin.route("/traits") @routes_admin.route("/traits")
def traits(): def traits():
lang = DEFAULT_LANG
traits_objs = db.session.query(Trait).all() 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) return flask.render_template("admin/traits.html", traits=traits_dicts)

View File

@ -57,10 +57,10 @@ def set_traits():
# modify # modify
db_trait = list(filter(lambda db_trait: db_trait.TRAIT_ID == int(input_trait["id"]), db_traits))[0] 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.NAME_LOCALE.get_text(input_lang))
db.session.delete(db_trait.Desc.TEXTS[0]) db.session.delete(db_trait.DESC_LOCALE.get_text(input_lang))
db_trait.Name.TEXTS = [Text(None, None, input_lang, input_trait["name"])] db_trait.NAME_LOCALE.TEXTS = [Text(None, None, input_lang, input_trait["name"])]
db_trait.Desc.TEXTS = [Text(None, None, input_lang, input_trait["desc"])] db_trait.DESC_LOCALE.TEXTS = [Text(None, None, input_lang, input_trait["desc"])]
db.session.add(db_trait) db.session.add(db_trait)
modified_db_traits.append(db_trait) modified_db_traits.append(db_trait)
@ -68,11 +68,11 @@ def set_traits():
# add # add
new_trait = Trait(None, None, None) new_trait = Trait(None, None, None)
new_trait.Name = Locale(None) new_trait.NAME_LOCALE = Locale(None)
new_trait.Desc = Locale(None) new_trait.DESC_LOCALE = Locale(None)
new_trait.Name.TEXTS.append(Text(None, None, input_lang, input_trait["name"])) new_trait.NAME_LOCALE.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.DESC_LOCALE.TEXTS.append(Text(None, None, input_lang, input_trait["desc"]))
db.session.add(new_trait) db.session.add(new_trait)
@ -99,9 +99,9 @@ def set_places():
# modify # modify
db_place = list(filter(lambda db_place: db_place.PLACE_ID == int(input_place["id"]), db_places))[0] 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) db.session.add(db_place)
modified_db_places.append(db_place) modified_db_places.append(db_place)
@ -109,8 +109,8 @@ def set_places():
# add # add
new_place = Place(None, None) new_place = Place(None, None)
new_place.LOCALE = Locale(None) new_place.NAME_LOCALE = Locale(None)
new_place.LOCALE.TEXTS = [Text(None, None, input_lang, input_place["name"])] new_place.NAME_LOCALE.TEXTS = [Text(None, None, input_lang, input_place["name"])]
db.session.add(new_place) db.session.add(new_place)
@ -134,13 +134,13 @@ def set_npc():
else: else:
npc_obj = db.session.get(Npc, input_npc["id"]) 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 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) db.session.delete(text)
for input_answer in input_answer_type["answers"]: 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() db.session.commit()