From 0a79c0579f8faf8082809a11968df4b324c38455 Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Sat, 11 Mar 2023 22:14:17 +0100 Subject: [PATCH 1/4] added bulk_export script --- bulk_data.json | 278 +++++++++++++++++++++++++++++++++++++++++++++++++ bulk_data.yml | 158 ++++++++++++++++++++++++++++ bulk_export.py | 65 ++++++++++++ 3 files changed, 501 insertions(+) create mode 100644 bulk_data.json create mode 100644 bulk_data.yml create mode 100644 bulk_export.py diff --git a/bulk_data.json b/bulk_data.json new file mode 100644 index 0000000..854edb3 --- /dev/null +++ b/bulk_data.json @@ -0,0 +1,278 @@ +{ + "rooms": { + "1": { + "FR": "Le salon" + }, + "2": { + "FR": "La salle de r\u00e9ception" + }, + "3": { + "FR": "Le hall d'entr\u00e9e" + }, + "4": { + "FR": "La cuisine" + }, + "5": { + "FR": "La chambre du ma\u00eetre" + }, + "6": { + "FR": "Le jardin" + } + }, + "questions": { + "withwho": { + "text": [ + { + "FR": "Etiez-vous seul au moment des faits ?" + }, + { + "FR": "Quelqu\u2019un peu valider vous alibi pour la soir\u00e9e ?" + }, + { + "FR": "Vous \u00e9tiez accompagn\u00e9 ce soir-l\u00e0 ?" + } + ] + }, + "where": { + "text": [ + { + "FR": "Ce manoir est plut\u00f4t grand ... vous pouvez me dire o\u00f9 vous \u00e9tiez?" + }, + { + "FR": "Vous \u00e9tiez o\u00f9 au moment des faits?" + }, + { + "FR": "Dans quelle salle \u00e9tiez-vous pendant que le coffre \u00e9tait subtilis\u00e9 ?" + } + ] + } + }, + "traits": { + "1": { + "name": { + "FR": "m\u00e9fiant(e)," + }, + "description": { + "FR": "Un maintien rigide des traits du visage, un regard de travers. Une crispation des sourcils et parfois des rides autour de la bouche. Ces caract\u00e9ristiques sont synonymes d'incompr\u00e9hension ou de peur de ce que peut nous annoncer la personne en face." + } + }, + "2": { + "name": { + "FR": "heureux(se)," + }, + "description": { + "FR": "Un visage d\u00e9contract\u00e9 et ouvert, les muscles des joues contract\u00e9s qui laissent appara\u00eetre un sourire. On le d\u00e9termine aussi par des yeux pliss\u00e9s en accord avec les sourcils qui marquent la diff\u00e9rence avec un faux sourire o\u00f9 les sourcils ne sont pas contract\u00e9s. Cela montre une complicit\u00e9 avec l'interlocuteur ou un moyen de ne pas laisser para\u00eetre ses r\u00e9elles \u00e9motions." + } + }, + "3": { + "name": { + "FR": "triste" + }, + "description": { + "FR": "Des sourcils contract\u00e9s et resserr\u00e9s vers le centre du visage auxquels s'ajoute un regard vide ou fuyant de l'interlocuteur, soit en fermant les yeux soit en \u00e9vitant un contact visuel. Ces caract\u00e9ristiques t\u00e9moignent d'un sentiment puissant ou du fait d'\u00eatre atteint par les propos ou accusations de son interlocuteur." + } + }, + "4": { + "name": { + "FR": "stress\u00e9(e)," + }, + "description": { + "FR": "Un visage crisp\u00e9 qui s'accompagne habituellement de sourcils fronc\u00e9s, un regard perdu qui se d\u00e9tourne de celui de son interlocuteur. Cela s'accompagne souvent de mouvements de la t\u00eate et de la bouche en se mordant les l\u00e8vres par exemple. Tout cela traduit une difficult\u00e9 de concentration ou une peur de ce qu'annonce ou peut nous annoncer l'interlocuteur en face." + } + }, + "5": { + "name": { + "FR": "surpris(e)," + }, + "description": { + "FR": "G\u00e9n\u00e9ralement par des yeux \u00e9carquill\u00e9s et un haussement des sourcils. Cela peut \u00e9galement se distinguer par une bouche ouverte ou, au contraire, des dents serr\u00e9es et parfois par un rel\u00e2chement du visage. Ces caract\u00e9ristiques correspondent \u00e0 un choc, une incompr\u00e9hension ou encore un \u00e9tonnement de ce que voit ou entend la personne." + } + } + }, + "npcs": { + "1": { + "name": { + "FR": "Le M\u00e9decin" + }, + "answers": { + "where": [ + { + "FR": "Il y avait {SALLE} \u00e7a m'a intrigu\u00e9." + }, + { + "FR": "{SALLE} avait l'air sympa donc j'y suis all\u00e9." + } + ], + "withwho": [ + { + "FR": "Il me semble qu'il y avait {NPC}." + }, + { + "FR": "Je suis pratiquement s\u00fbr que j'\u00e9tais avec {NPC}." + } + ] + } + }, + "2": { + "name": { + "FR": "Le Diplomate" + }, + "answers": { + "where": [ + { + "FR": "Je profitais d'une collation dans {SALLE}." + }, + { + "FR": "J'admirais la d\u00e9coration subtile de {SALLE} ... je m'en inspirerais pour chez moi." + } + ], + "withwho": [ + { + "FR": "Je m'instruisais aupr\u00e8s de {NPC}." + }, + { + "FR": "Avec {NPC} pour exposer nos diff\u00e9rents points de vus sur divers sujets." + } + ] + } + }, + "3": { + "name": { + "FR": "Le Combattant" + }, + "answers": { + "where": [ + { + "FR": "{SALLE} nous a servi de salle de duel." + }, + { + "FR": "J'ai festoill\u00e9 dans {SALLE}." + } + ], + "withwho": [ + { + "FR": "On faisait un bras de fer avec {NPC}." + }, + { + "FR": "{NPC} et moi nous sommes engag\u00e9s dans une joute verbale des plus palpitante." + } + ] + } + }, + "4": { + "name": { + "FR": "La Duchesse" + }, + "answers": { + "where": [ + { + "FR": "Pour votre gouverne je me trouvais dans {SALLE}." + }, + { + "FR": "Si vous voulez le savoir ... j'\u00e9tais en train de me reposer dans {SALLE}." + } + ], + "withwho": [ + { + "FR": "{NPC} me tenait compagnie." + }, + { + "FR": "J'\u00e9tais avec {NPC}." + } + ] + } + }, + "5": { + "name": { + "FR": "La Diva" + }, + "answers": { + "where": [ + { + "FR": "{SALLE} me semblait \u00eatre la plus belle pi\u00e8ce de la maison." + }, + { + "FR": "Je buvais un verre dans {SALLE}." + } + ], + "withwho": [ + { + "FR": "Je profitais de la compagnie de {NPC}." + }, + { + "FR": "J'\u00e9tais avec {NPC} \u00e0 partager une d\u00e9licieuse conversation ainsi qu'une coupe de champagne." + } + ] + } + }, + "6": { + "name": { + "FR": "La Parieuse" + }, + "answers": { + "where": [ + { + "FR": "J'avais mont\u00e9 une table de jeu dans {SALLE}." + }, + { + "FR": "{SALLE} est tout de m\u00eame plus agr\u00e9able une fois chang\u00e9(e), en casino." + } + ], + "withwho": [ + { + "FR": "Vous saviez que {NPC} \u00e9tait incroyable avec des cartes \u00e0 la main ?" + }, + { + "FR": "Si vous tenez \u00e0 votre argent ne jouez jamais au poker avec {NPC}." + } + ] + } + }, + "7": { + "name": { + "FR": "L'Agent" + }, + "answers": { + "where": [ + { + "FR": "On pouvait me retrouver dans {SALLE}." + }, + { + "FR": "{SALLE}" + } + ], + "withwho": [ + { + "FR": "J'\u00e9tais avec {NPC} au moment des faits." + }, + { + "FR": "{NPC}" + } + ] + } + }, + "8": { + "name": { + "FR": "La Voyageuse" + }, + "answers": { + "where": [ + { + "FR": "{SALLE} me semblait un bon endroit pour me poser" + }, + { + "FR": "{SALLE} me rappelait mes voyages." + } + ], + "withwho": [ + { + "FR": "Nous organisions notre prochain voyage avec {NPC}." + }, + { + "FR": "Avec {NPC} on parlait des lieux que l\u2019on avait visit\u00e9s. C\u2019\u00e9tait tr\u00e8s instructif." + } + ] + } + } + } +} \ No newline at end of file diff --git a/bulk_data.yml b/bulk_data.yml new file mode 100644 index 0000000..b07d406 --- /dev/null +++ b/bulk_data.yml @@ -0,0 +1,158 @@ +npcs: + 1: + answers: + where: + - FR: "Il y avait {SALLE} \xE7a m'a intrigu\xE9." + - FR: "{SALLE} avait l'air sympa donc j'y suis all\xE9." + withwho: + - FR: Il me semble qu'il y avait {NPC}. + - FR: "Je suis pratiquement s\xFBr que j'\xE9tais avec {NPC}." + name: + FR: "Le M\xE9decin" + 2: + answers: + where: + - FR: Je profitais d'une collation dans {SALLE}. + - FR: "J'admirais la d\xE9coration subtile de {SALLE} ... je m'en inspirerais\ + \ pour chez moi." + withwho: + - FR: "Je m'instruisais aupr\xE8s de {NPC}." + - FR: "Avec {NPC} pour exposer nos diff\xE9rents points de vus sur divers sujets." + name: + FR: Le Diplomate + 3: + answers: + where: + - FR: '{SALLE} nous a servi de salle de duel.' + - FR: "J'ai festoill\xE9 dans {SALLE}." + withwho: + - FR: On faisait un bras de fer avec {NPC}. + - FR: "{NPC} et moi nous sommes engag\xE9s dans une joute verbale des plus palpitante." + name: + FR: Le Combattant + 4: + answers: + where: + - FR: Pour votre gouverne je me trouvais dans {SALLE}. + - FR: "Si vous voulez le savoir ... j'\xE9tais en train de me reposer dans {SALLE}." + withwho: + - FR: '{NPC} me tenait compagnie.' + - FR: "J'\xE9tais avec {NPC}." + name: + FR: La Duchesse + 5: + answers: + where: + - FR: "{SALLE} me semblait \xEAtre la plus belle pi\xE8ce de la maison." + - FR: Je buvais un verre dans {SALLE}. + withwho: + - FR: Je profitais de la compagnie de {NPC}. + - FR: "J'\xE9tais avec {NPC} \xE0 partager une d\xE9licieuse conversation ainsi\ + \ qu'une coupe de champagne." + name: + FR: La Diva + 6: + answers: + where: + - FR: "J'avais mont\xE9 une table de jeu dans {SALLE}." + - FR: "{SALLE} est tout de m\xEAme plus agr\xE9able une fois chang\xE9(e), en\ + \ casino." + withwho: + - FR: "Vous saviez que {NPC} \xE9tait incroyable avec des cartes \xE0 la main\ + \ ?" + - FR: "Si vous tenez \xE0 votre argent ne jouez jamais au poker avec {NPC}." + name: + FR: La Parieuse + 7: + answers: + where: + - FR: On pouvait me retrouver dans {SALLE}. + - FR: '{SALLE}' + withwho: + - FR: "J'\xE9tais avec {NPC} au moment des faits." + - FR: '{NPC}' + name: + FR: L'Agent + 8: + answers: + where: + - FR: '{SALLE} me semblait un bon endroit pour me poser' + - FR: '{SALLE} me rappelait mes voyages.' + withwho: + - FR: Nous organisions notre prochain voyage avec {NPC}. + - FR: "Avec {NPC} on parlait des lieux que l\u2019on avait visit\xE9s. C\u2019\ + \xE9tait tr\xE8s instructif." + name: + FR: La Voyageuse +questions: + where: + text: + - FR: "Ce manoir est plut\xF4t grand ... vous pouvez me dire o\xF9 vous \xE9tiez?" + - FR: "Vous \xE9tiez o\xF9 au moment des faits?" + - FR: "Dans quelle salle \xE9tiez-vous pendant que le coffre \xE9tait subtilis\xE9\ + \ ?" + withwho: + text: + - FR: Etiez-vous seul au moment des faits ? + - FR: "Quelqu\u2019un peu valider vous alibi pour la soir\xE9e ?" + - FR: "Vous \xE9tiez accompagn\xE9 ce soir-l\xE0 ?" +rooms: + 1: + FR: Le salon + 2: + FR: "La salle de r\xE9ception" + 3: + FR: "Le hall d'entr\xE9e" + 4: + FR: La cuisine + 5: + FR: "La chambre du ma\xEEtre" + 6: + FR: Le jardin +traits: + 1: + description: + FR: "Un maintien rigide des traits du visage, un regard de travers. Une crispation\ + \ des sourcils et parfois des rides autour de la bouche. Ces caract\xE9ristiques\ + \ sont synonymes d'incompr\xE9hension ou de peur de ce que peut nous annoncer\ + \ la personne en face." + name: + FR: "m\xE9fiant(e)," + 2: + description: + FR: "Un visage d\xE9contract\xE9 et ouvert, les muscles des joues contract\xE9\ + s qui laissent appara\xEEtre un sourire. On le d\xE9termine aussi par des\ + \ yeux pliss\xE9s en accord avec les sourcils qui marquent la diff\xE9rence\ + \ avec un faux sourire o\xF9 les sourcils ne sont pas contract\xE9s. Cela\ + \ montre une complicit\xE9 avec l'interlocuteur ou un moyen de ne pas laisser\ + \ para\xEEtre ses r\xE9elles \xE9motions." + name: + FR: heureux(se), + 3: + description: + FR: "Des sourcils contract\xE9s et resserr\xE9s vers le centre du visage auxquels\ + \ s'ajoute un regard vide ou fuyant de l'interlocuteur, soit en fermant les\ + \ yeux soit en \xE9vitant un contact visuel. Ces caract\xE9ristiques t\xE9\ + moignent d'un sentiment puissant ou du fait d'\xEAtre atteint par les propos\ + \ ou accusations de son interlocuteur." + name: + FR: triste + 4: + description: + FR: "Un visage crisp\xE9 qui s'accompagne habituellement de sourcils fronc\xE9\ + s, un regard perdu qui se d\xE9tourne de celui de son interlocuteur. Cela\ + \ s'accompagne souvent de mouvements de la t\xEAte et de la bouche en se mordant\ + \ les l\xE8vres par exemple. Tout cela traduit une difficult\xE9 de concentration\ + \ ou une peur de ce qu'annonce ou peut nous annoncer l'interlocuteur en face." + name: + FR: "stress\xE9(e)," + 5: + description: + FR: "G\xE9n\xE9ralement par des yeux \xE9carquill\xE9s et un haussement des\ + \ sourcils. Cela peut \xE9galement se distinguer par une bouche ouverte ou,\ + \ au contraire, des dents serr\xE9es et parfois par un rel\xE2chement du visage.\ + \ Ces caract\xE9ristiques correspondent \xE0 un choc, une incompr\xE9hension\ + \ ou encore un \xE9tonnement de ce que voit ou entend la personne." + name: + FR: surpris(e), + diff --git a/bulk_export.py b/bulk_export.py new file mode 100644 index 0000000..ffd2eea --- /dev/null +++ b/bulk_export.py @@ -0,0 +1,65 @@ +# Load .env file +from dotenv import load_dotenv +load_dotenv() +import json +import yaml +import argparse + +from sqlalchemy.orm import sessionmaker + +from truthinquiry.ext.database.sa import engine +from truthinquiry.ext.database.models import * + +Session = sessionmaker(bind=engine) +session = Session() + + +def bulk_export(): + data = {} + + rooms = data["rooms"] = {} + for room in session.query(Place).all(): + current_room = rooms[room.PLACE_ID] = {} + for text in session.query(Locale).filter_by(TEXT_ID=room.NAME_LID).all(): + current_room[text.LANG] = text.TEXT + + questions = data["questions"] = {} + data["questions"]["withwho"] = {} + data["questions"]["withwho"]["text"] = [] + data["questions"]["where"] = {} + data["questions"]["where"]["text"] = [] + for question in session.query(Question).all(): + question_list = data["questions"]["where"]["text"] if question.QUESTION_TYPE == 0 else data["questions"]["withwho"]["text"] + for text in session.query(Locale).filter_by(TEXT_ID=question.TEXT_LID).all(): + question_list.append({text.LANG : text.TEXT}) + + traits = data["traits"] = {} + for trait in session.query(Trait).all(): + current_trait = traits[trait.TRAIT_ID] = {} + current_trait["name"] = {} + for text in session.query(Locale).filter_by(TEXT_ID=trait.NAME_LID): + current_trait["name"][text.LANG] = text.TEXT + + current_trait["description"] = {} + for text in session.query(Locale).filter_by(TEXT_ID=trait.DESC_LID): + current_trait["description"][text.LANG] = text.TEXT + + npcs = data["npcs"] = {} + for npc in session.query(Npc).all(): + current_npc = npcs[npc.NPC_ID] = {} + current_npc["name"] = {} + for text in session.query(Locale).filter_by(TEXT_ID=npc.NAME_LID): + current_npc["name"][text.LANG] = text.TEXT + + #TODO reactions + current_npc["answers"] = {} + current_npc["answers"]["where"] = [] + current_npc["answers"]["withwho"] = [] + 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"] + for text in session.query(Locale).filter_by(TEXT_ID=answer.TEXT_LID): + answer_list.append({text.LANG: text.TEXT}) + return data + + +print(json.dumps(bulk_export())) From a8048d8366e9be50e3e725da549d1421e986130b Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Sat, 11 Mar 2023 21:24:30 +0100 Subject: [PATCH 2/4] added bulk_import script + fixed TRAIT model --- bulkData.yml | 65 ++++++++++++ bulk_import.py | 152 ++++++++++++++++++++++++++++ truthinquiry/ext/database/models.py | 3 +- 3 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 bulkData.yml create mode 100644 bulk_import.py diff --git a/bulkData.yml b/bulkData.yml new file mode 100644 index 0000000..5cc1b09 --- /dev/null +++ b/bulkData.yml @@ -0,0 +1,65 @@ +rooms: + livingRoom: + EN: "the living room" + FR: "le salon" + kitchen: + EN: "the kitchen" + FR: "La couisine >:(" + +questions: + where: + text: + - FR: "Ou étais tu ?" + - FR: "Where were you ?" + withwho: + text: + - FR: "Avec qui étais tu ?" + - EN: "Who were you with ?" + +traits: + happy: + name: + FR: "Heureux" + EN: "Happy" + description: + FR: "Une description" + EN: "Some description" + sad: + name: + FR: "Malheureux" + EN: "sad" + description: + FR: "Une description 2" + EN: "Some description 2" + +npc: + diva: + name: + FR: "La diva" + EN: "The diva" + reactions: + neutral: + img: "local/files/diva/neutral.png" + happy: + img: "local/files/diva/happy.png" + sad: + img: "local/files/diva/sad.png" + answers: + withwho: + - EN: "I was in {ROOM}" + - FR: "J'étais dans {ROOM}" + agent: + name: + FR: "L'agent" + EN: "The agent" + reactions: + neutral: + img: "local/files/agent/neutral.png" + sad: + img: "local/files/agent/happy/1.png" + + answers: + where: + - EN: "I was in {ROOM}" + - FR: "J'étais dans la salle {ROOM}" + - EN: "{ROOM} reminds me of my travls" \ No newline at end of file diff --git a/bulk_import.py b/bulk_import.py new file mode 100644 index 0000000..c890048 --- /dev/null +++ b/bulk_import.py @@ -0,0 +1,152 @@ + +import argparse +import yaml +from dotenv import load_dotenv +load_dotenv() +from sqlalchemy.orm import sessionmaker +from truthinquiry.ext.database.models import * +from truthinquiry.ext.database.sa import engine + +Session = sessionmaker(bind=engine) +session = Session() + +# full reset .w. +Base.metadata.drop_all(engine) +Base.metadata.create_all(engine) + + +class LocaleManager(): + def __init__(self): + self.used_lids = [0] + [self.used_lids.append(locale.TEXT_ID) for locale in session.query(Locale).all()] + + def get_unused_lid(self): + new_lid = max(self.used_lids) + 1 + self.used_lids.append(new_lid) + return new_lid + + + +def bulk_import(data): + + # Data list that will be commited to the db + LID_LIST = [] + TRAIT_LIST = [] + REACTION_LIST = [] + QUESTIONS_LIST = [] + ANSWER_LIST = [] + NPC_LIST = [] + ROOMS_LIST = [] + + # helper list to simplify + trait_names = {} + lm = LocaleManager() + getid = lm.get_unused_lid + + # Questions + + # Where type questions + + question_type_zero = Question(0, 0, getid()) + QUESTIONS_LIST.append(question_type_zero) + + question_type_one = Question(0, 1, getid()) + QUESTIONS_LIST.append(question_type_zero) + + questions = data["questions"] + # handle where type quetions + for question in questions["where"]["text"]: + lang = list(question.keys())[0] + text = list(question.values())[0] + LID_LIST.append(Locale(question_type_zero.TEXT_LID, lang, text)) + + # handle with who type quetions + for question in questions["withwho"]["text"]: + lang = list(question.keys())[0] + text = list(question.values())[0] + LID_LIST.append(Locale(question_type_zero.TEXT_LID, lang, text)) + + # Traits + traits = data["traits"] + for trait in traits.values(): + # create the new trait + new_trait = Trait(0,getid(), getid()) + + for lang in trait["name"]: + LID_LIST.append(Locale(new_trait.NAME_LID, + lang, trait["name"][lang])) + trait_names[trait["name"][lang]] = new_trait.TRAIT_ID + + for lang in trait["description"]: + LID_LIST.append(Locale(new_trait.DESC_LID, lang, + trait["description"][lang])) + + TRAIT_LIST.append(new_trait) + + # Npcs + npcs = data["npc"] + for npc in npcs.values(): + new_npc = Npc(0, getid()) + + # handle the names + for lang in npc["name"]: + LID_LIST.append(Locale(new_npc.NAME_LID, lang, npc["name"][lang])) + + for reaction in npc["reactions"]: + if reaction in list(trait_names.keys()): + new_reaction = Reaction(0,new_npc.NPC_ID, trait_names[reaction]) + REACTION_LIST.append(new_reaction) + for question_type in npc["answers"]: + question_type_id = 0 if question_type == "where" else 1 + + new_answer = Answer(0, question_type_id, new_npc.NPC_ID, getid()) + ANSWER_LIST.append(new_answer) + + for answer in npc["answers"][question_type]: + lang = list(answer.keys())[0] + text = list(answer.values())[0] + LID_LIST.append(Locale(new_answer.TEXT_LID, lang, text)) + + NPC_LIST.append(new_npc) + + # rooms + rooms = data["rooms"] + for room in rooms.values(): + new_room = Place(0,getid()) + for lang in room: + LID_LIST.append(Locale(new_room.NAME_LID, lang, room[lang])) + ROOMS_LIST.append(new_room) + + for text in LID_LIST: + print("Locale : "+str(text)) + session.add(text) + session.commit() + for question in QUESTIONS_LIST: + print("Question : "+str(question)) + session.add(question) + session.commit() + for trait in TRAIT_LIST: + print("Trait : "+ str(trait)) + session.add(trait) + session.commit() + for npc in NPC_LIST: + print("Npc : "+ str(npc)) + session.add(npc) + session.commit() + for reaction in REACTION_LIST: + print("Reaction : " + str(reaction)) + session.add(reaction) + session.commit() + for answer in ANSWER_LIST: + print("Answer : "+ str(answer)) + session.add(answer) + session.commit() + for room in ROOMS_LIST: + print("Room : "+str(room)) + session.add(room) + session.commit() + + +file = open("bulkData.yml", "r") + +bulk_import(yaml.load(file, yaml.Loader)) diff --git a/truthinquiry/ext/database/models.py b/truthinquiry/ext/database/models.py index e030672..29e10fc 100644 --- a/truthinquiry/ext/database/models.py +++ b/truthinquiry/ext/database/models.py @@ -116,9 +116,10 @@ class Trait(Base): Desc = relationship("Locale",foreign_keys=[DESC_LID]) - def __init__(self, TRAIT_ID, NAME_LID): + def __init__(self, TRAIT_ID, NAME_LID, DESC_LID): self.TRAIT_ID = TRAIT_ID self.NAME_LID = NAME_LID + self.DESC_LID = DESC_LID def __str__(self) -> str: return f"{self.TRAIT_ID} {self.NAME_LID}" From d07a61c2a0fd94f75516e04fb49e52c2f1d89510 Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Sat, 11 Mar 2023 22:39:05 +0100 Subject: [PATCH 3/4] fixed mismatched keys + renamed sql import file --- truthInquiry.sql => bulk_data.sql | 0 bulk_import.py | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) rename truthInquiry.sql => bulk_data.sql (100%) diff --git a/truthInquiry.sql b/bulk_data.sql similarity index 100% rename from truthInquiry.sql rename to bulk_data.sql diff --git a/bulk_import.py b/bulk_import.py index c890048..70fa3ad 100644 --- a/bulk_import.py +++ b/bulk_import.py @@ -84,7 +84,7 @@ def bulk_import(data): TRAIT_LIST.append(new_trait) # Npcs - npcs = data["npc"] + npcs = data["npcs"] for npc in npcs.values(): new_npc = Npc(0, getid()) @@ -92,10 +92,13 @@ def bulk_import(data): for lang in npc["name"]: LID_LIST.append(Locale(new_npc.NAME_LID, lang, npc["name"][lang])) + # TODO handle reactions + """ for reaction in npc["reactions"]: if reaction in list(trait_names.keys()): new_reaction = Reaction(0,new_npc.NPC_ID, trait_names[reaction]) - REACTION_LIST.append(new_reaction) + REACTION_LIST.append(new_reaction) """ + for question_type in npc["answers"]: question_type_id = 0 if question_type == "where" else 1 @@ -121,32 +124,39 @@ def bulk_import(data): print("Locale : "+str(text)) session.add(text) session.commit() + for question in QUESTIONS_LIST: print("Question : "+str(question)) session.add(question) session.commit() + for trait in TRAIT_LIST: print("Trait : "+ str(trait)) session.add(trait) session.commit() + for npc in NPC_LIST: print("Npc : "+ str(npc)) session.add(npc) session.commit() + for reaction in REACTION_LIST: print("Reaction : " + str(reaction)) session.add(reaction) session.commit() + for answer in ANSWER_LIST: print("Answer : "+ str(answer)) session.add(answer) session.commit() + for room in ROOMS_LIST: print("Room : "+str(room)) session.add(room) session.commit() -file = open("bulkData.yml", "r") + +file = open("bulk_data.yml", "r") bulk_import(yaml.load(file, yaml.Loader)) From 666bb079db03c29ae9da3015de669c446f5a462a Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Tue, 14 Mar 2023 19:11:07 +0100 Subject: [PATCH 4/4] adapted code to new models --- bulk_export.py | 14 +++++++------- bulk_import.py | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/bulk_export.py b/bulk_export.py index ffd2eea..cf84f62 100644 --- a/bulk_export.py +++ b/bulk_export.py @@ -28,27 +28,27 @@ def bulk_export(): data["questions"]["withwho"]["text"] = [] data["questions"]["where"] = {} data["questions"]["where"]["text"] = [] - for question in session.query(Question).all(): - question_list = data["questions"]["where"]["text"] if question.QUESTION_TYPE == 0 else data["questions"]["withwho"]["text"] - for text in session.query(Locale).filter_by(TEXT_ID=question.TEXT_LID).all(): + for question in session.query(QuestionType).all(): + question_list = data["questions"]["where"]["text"] if question.QUESTION_TYPE_ID == 0 else data["questions"]["withwho"]["text"] + for text in session.query(Text).filter_by(LID=question.TEXT_LID).all(): question_list.append({text.LANG : text.TEXT}) traits = data["traits"] = {} for trait in session.query(Trait).all(): current_trait = traits[trait.TRAIT_ID] = {} 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["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 npcs = data["npcs"] = {} for npc in session.query(Npc).all(): current_npc = npcs[npc.NPC_ID] = {} 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 #TODO reactions @@ -57,7 +57,7 @@ def bulk_export(): current_npc["answers"]["withwho"] = [] 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"] - 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}) return data diff --git a/bulk_import.py b/bulk_import.py index 70fa3ad..92f698b 100644 --- a/bulk_import.py +++ b/bulk_import.py @@ -18,19 +18,21 @@ Base.metadata.create_all(engine) class LocaleManager(): def __init__(self): 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): new_lid = max(self.used_lids) + 1 self.used_lids.append(new_lid) return new_lid - + + def get_used_lids(self): + return self.used_lids[1:] def bulk_import(data): # Data list that will be commited to the db - LID_LIST = [] + TEXT_LIST = [] TRAIT_LIST = [] REACTION_LIST = [] QUESTIONS_LIST = [] @@ -47,24 +49,24 @@ def bulk_import(data): # Where type questions - question_type_zero = Question(0, 0, getid()) + question_type_zero = QuestionType(0, getid()) QUESTIONS_LIST.append(question_type_zero) - question_type_one = Question(0, 1, getid()) - QUESTIONS_LIST.append(question_type_zero) + question_type_one = QuestionType(1, getid()) + QUESTIONS_LIST.append(question_type_one) questions = data["questions"] # handle where type quetions for question in questions["where"]["text"]: lang = list(question.keys())[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 for question in questions["withwho"]["text"]: lang = list(question.keys())[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 = data["traits"] @@ -73,24 +75,25 @@ def bulk_import(data): new_trait = Trait(0,getid(), getid()) 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])) trait_names[trait["name"][lang]] = new_trait.TRAIT_ID 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_LIST.append(new_trait) # Npcs npcs = data["npcs"] + npcid = 1 for npc in npcs.values(): - new_npc = Npc(0, getid()) + new_npc = Npc(npcid, getid()) # handle the names 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 """ @@ -100,28 +103,33 @@ def bulk_import(data): REACTION_LIST.append(new_reaction) """ 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) for answer in npc["answers"][question_type]: lang = list(answer.keys())[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) + npcid += 1 # rooms rooms = data["rooms"] for room in rooms.values(): new_room = Place(0,getid()) 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) - for text in LID_LIST: - print("Locale : "+str(text)) + for lid in lm.get_used_lids(): + print("lid :"+ str(lid)) + session.add(Locale(lid)); + + for text in TEXT_LIST: + print("Text : "+str(text)) session.add(text) session.commit() @@ -155,8 +163,6 @@ def bulk_import(data): session.add(room) session.commit() - - file = open("bulk_data.yml", "r") bulk_import(yaml.load(file, yaml.Loader))