adapted code to new models

This commit is contained in:
Djalim Simaila 2023-03-14 19:11:07 +01:00
parent d07a61c2a0
commit 666bb079db
2 changed files with 33 additions and 27 deletions

View File

@ -28,27 +28,27 @@ def bulk_export():
data["questions"]["withwho"]["text"] = [] data["questions"]["withwho"]["text"] = []
data["questions"]["where"] = {} data["questions"]["where"] = {}
data["questions"]["where"]["text"] = [] data["questions"]["where"]["text"] = []
for question in session.query(Question).all(): for question in session.query(QuestionType).all():
question_list = data["questions"]["where"]["text"] if question.QUESTION_TYPE == 0 else data["questions"]["withwho"]["text"] question_list = data["questions"]["where"]["text"] if question.QUESTION_TYPE_ID == 0 else data["questions"]["withwho"]["text"]
for text in session.query(Locale).filter_by(TEXT_ID=question.TEXT_LID).all(): for text in session.query(Text).filter_by(LID=question.TEXT_LID).all():
question_list.append({text.LANG : text.TEXT}) question_list.append({text.LANG : text.TEXT})
traits = data["traits"] = {} traits = data["traits"] = {}
for trait in session.query(Trait).all(): for trait in session.query(Trait).all():
current_trait = traits[trait.TRAIT_ID] = {} current_trait = traits[trait.TRAIT_ID] = {}
current_trait["name"] = {} current_trait["name"] = {}
for text in session.query(Locale).filter_by(TEXT_ID=trait.NAME_LID): for text in session.query(TEXT).filter_by(LID=trait.NAME_LID):
current_trait["name"][text.LANG] = text.TEXT current_trait["name"][text.LANG] = text.TEXT
current_trait["description"] = {} current_trait["description"] = {}
for text in session.query(Locale).filter_by(TEXT_ID=trait.DESC_LID): for text in session.query(TEXT).filter_by(LID=trait.DESC_LID):
current_trait["description"][text.LANG] = text.TEXT current_trait["description"][text.LANG] = text.TEXT
npcs = data["npcs"] = {} npcs = data["npcs"] = {}
for npc in session.query(Npc).all(): for npc in session.query(Npc).all():
current_npc = npcs[npc.NPC_ID] = {} current_npc = npcs[npc.NPC_ID] = {}
current_npc["name"] = {} current_npc["name"] = {}
for text in session.query(Locale).filter_by(TEXT_ID=npc.NAME_LID): for text in session.query(TEXT).filter_by(LID=npc.NAME_LID):
current_npc["name"][text.LANG] = text.TEXT current_npc["name"][text.LANG] = text.TEXT
#TODO reactions #TODO reactions
@ -57,7 +57,7 @@ def bulk_export():
current_npc["answers"]["withwho"] = [] current_npc["answers"]["withwho"] = []
for answer in session.query(Answer).filter_by(NPC_ID=npc.NPC_ID): for answer in session.query(Answer).filter_by(NPC_ID=npc.NPC_ID):
answer_list = current_npc["answers"]["where"] if answer.QA_TYPE == 0 else current_npc["answers"]["withwho"] answer_list = current_npc["answers"]["where"] if answer.QA_TYPE == 0 else current_npc["answers"]["withwho"]
for text in session.query(Locale).filter_by(TEXT_ID=answer.TEXT_LID): for text in session.query(TEXT).filter_by(LID=answer.TEXT_LID):
answer_list.append({text.LANG: text.TEXT}) answer_list.append({text.LANG: text.TEXT})
return data return data

View File

@ -18,19 +18,21 @@ Base.metadata.create_all(engine)
class LocaleManager(): class LocaleManager():
def __init__(self): def __init__(self):
self.used_lids = [0] self.used_lids = [0]
[self.used_lids.append(locale.TEXT_ID) for locale in session.query(Locale).all()] [self.used_lids.append(locale.LID) for locale in session.query(Locale).all()]
def get_unused_lid(self): def get_unused_lid(self):
new_lid = max(self.used_lids) + 1 new_lid = max(self.used_lids) + 1
self.used_lids.append(new_lid) self.used_lids.append(new_lid)
return new_lid return new_lid
def get_used_lids(self):
return self.used_lids[1:]
def bulk_import(data): def bulk_import(data):
# Data list that will be commited to the db # Data list that will be commited to the db
LID_LIST = [] TEXT_LIST = []
TRAIT_LIST = [] TRAIT_LIST = []
REACTION_LIST = [] REACTION_LIST = []
QUESTIONS_LIST = [] QUESTIONS_LIST = []
@ -47,24 +49,24 @@ def bulk_import(data):
# Where type questions # Where type questions
question_type_zero = Question(0, 0, getid()) question_type_zero = QuestionType(0, getid())
QUESTIONS_LIST.append(question_type_zero) QUESTIONS_LIST.append(question_type_zero)
question_type_one = Question(0, 1, getid()) question_type_one = QuestionType(1, getid())
QUESTIONS_LIST.append(question_type_zero) QUESTIONS_LIST.append(question_type_one)
questions = data["questions"] questions = data["questions"]
# handle where type quetions # handle where type quetions
for question in questions["where"]["text"]: for question in questions["where"]["text"]:
lang = list(question.keys())[0] lang = list(question.keys())[0]
text = list(question.values())[0] text = list(question.values())[0]
LID_LIST.append(Locale(question_type_zero.TEXT_LID, lang, text)) TEXT_LIST.append(Text(0,question_type_zero.TEXT_LID, lang, text))
# handle with who type quetions # handle with who type quetions
for question in questions["withwho"]["text"]: for question in questions["withwho"]["text"]:
lang = list(question.keys())[0] lang = list(question.keys())[0]
text = list(question.values())[0] text = list(question.values())[0]
LID_LIST.append(Locale(question_type_zero.TEXT_LID, lang, text)) TEXT_LIST.append(Text(0,question_type_one.TEXT_LID, lang, text))
# Traits # Traits
traits = data["traits"] traits = data["traits"]
@ -73,24 +75,25 @@ def bulk_import(data):
new_trait = Trait(0,getid(), getid()) new_trait = Trait(0,getid(), getid())
for lang in trait["name"]: for lang in trait["name"]:
LID_LIST.append(Locale(new_trait.NAME_LID, TEXT_LIST.append(Text(0,new_trait.NAME_LID,
lang, trait["name"][lang])) lang, trait["name"][lang]))
trait_names[trait["name"][lang]] = new_trait.TRAIT_ID trait_names[trait["name"][lang]] = new_trait.TRAIT_ID
for lang in trait["description"]: for lang in trait["description"]:
LID_LIST.append(Locale(new_trait.DESC_LID, lang, TEXT_LIST.append(Text(0,new_trait.DESC_LID, lang,
trait["description"][lang])) trait["description"][lang]))
TRAIT_LIST.append(new_trait) TRAIT_LIST.append(new_trait)
# Npcs # Npcs
npcs = data["npcs"] npcs = data["npcs"]
npcid = 1
for npc in npcs.values(): for npc in npcs.values():
new_npc = Npc(0, getid()) new_npc = Npc(npcid, getid())
# handle the names # handle the names
for lang in npc["name"]: for lang in npc["name"]:
LID_LIST.append(Locale(new_npc.NAME_LID, lang, npc["name"][lang])) TEXT_LIST.append(Text(0,new_npc.NAME_LID, lang, npc["name"][lang]))
# TODO handle reactions # TODO handle reactions
""" """
@ -100,28 +103,33 @@ def bulk_import(data):
REACTION_LIST.append(new_reaction) """ REACTION_LIST.append(new_reaction) """
for question_type in npc["answers"]: for question_type in npc["answers"]:
question_type_id = 0 if question_type == "where" else 1 question_type_id = question_type_zero.QUESTION_TYPE_ID if question_type == "where" else question_type_one.QUESTION_TYPE_ID
new_answer = Answer(0, question_type_id, new_npc.NPC_ID, getid()) new_answer = Answer(question_type_id, new_npc.NPC_ID, getid())
ANSWER_LIST.append(new_answer) ANSWER_LIST.append(new_answer)
for answer in npc["answers"][question_type]: for answer in npc["answers"][question_type]:
lang = list(answer.keys())[0] lang = list(answer.keys())[0]
text = list(answer.values())[0] text = list(answer.values())[0]
LID_LIST.append(Locale(new_answer.TEXT_LID, lang, text)) TEXT_LIST.append(Text(0,new_answer.TEXT_LID, lang, text))
NPC_LIST.append(new_npc) NPC_LIST.append(new_npc)
npcid += 1
# rooms # rooms
rooms = data["rooms"] rooms = data["rooms"]
for room in rooms.values(): for room in rooms.values():
new_room = Place(0,getid()) new_room = Place(0,getid())
for lang in room: for lang in room:
LID_LIST.append(Locale(new_room.NAME_LID, lang, room[lang])) TEXT_LIST.append(Text(0,new_room.NAME_LID, lang, room[lang]))
ROOMS_LIST.append(new_room) ROOMS_LIST.append(new_room)
for text in LID_LIST: for lid in lm.get_used_lids():
print("Locale : "+str(text)) print("lid :"+ str(lid))
session.add(Locale(lid));
for text in TEXT_LIST:
print("Text : "+str(text))
session.add(text) session.add(text)
session.commit() session.commit()
@ -155,8 +163,6 @@ def bulk_import(data):
session.add(room) session.add(room)
session.commit() session.commit()
file = open("bulk_data.yml", "r") file = open("bulk_data.yml", "r")
bulk_import(yaml.load(file, yaml.Loader)) bulk_import(yaml.load(file, yaml.Loader))