Skip to content

Commit

Permalink
Recipe file creation (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinkova authored Nov 7, 2024
1 parent a0825d0 commit d1190ab
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 14 deletions.
119 changes: 105 additions & 14 deletions src/dsw_seed_maker/logic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import os
import uuid
from datetime import datetime
from operator import index

from dotenv import load_dotenv
import pathlib
Expand Down Expand Up @@ -192,21 +194,33 @@ def download_file_logic(file_name: str, target_path: str) -> bool:
def create_recipe_file(output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
with open("recipe_tmp.json", 'r') as template_recipe:
data = template_recipe.read()
with open(os.path.join(output_dir, 'recipe.json'), 'w') as recipe:
recipe.write(data)

def add_db_file_to_recipe(recipe_path, db_file_name):
with open(recipe_path, 'r') as recipe_file:
recipe_data = json.load(recipe_file)

recipe_data["db"]["scripts"].append({"filename" : db_file_name})

with open(recipe_path, 'w') as recipe_file:
#recipe_file.write(str(recipe_data))
json.dump(recipe_data, recipe_file, ensure_ascii=False, indent=4)

file_path = os.path.join(output_dir, "recipe.txt")
file = open(file_path, 'w')
return file

def process_input(data, output_dir):
db = connect_to_db_logic()
recipe = create_recipe_file(output_dir)
create_recipe_file(output_dir)
for resource_type, items in data.items():
handler = resource_handlers.get(resource_type)
file = create_seed_files_db(resource_type, output_dir)
recipe_file = create_seed_files_db(resource_type, output_dir)
add_db_file_to_recipe(output_dir + "/recipe.json", "add_" + resource_type + ".sql")
if handler:
# Call the handler for each item in the list associated with this resource type
for item in items:
handler(item, db, file, resource_type)
handler(item, db, recipe_file, resource_type)
else:
print(f"Unrecognized resource type: {resource_type}")

Expand Down Expand Up @@ -266,7 +280,7 @@ def handle_uuid(data, db, file, resource_type):
insert_query = generate_insert_query(resource[0], resource_tables[resource_type])
write_seed_files_db(file, insert_query)
else:
print("User not found in database")
print("User not found in database")

def handle_id(data, db, file, resource_type):
query = "SELECT * FROM {resource_type} WHERE id = '{id}'".format(id=data['id'], resource_type=resource_tables[resource_type])
Expand All @@ -279,16 +293,93 @@ def handle_id(data, db, file, resource_type):
else:
print("User not found in database")

def handle_users(input_data, db, recipe_file, resource_type):
query = "SELECT * FROM {resource_type} WHERE uuid = '{uuid}'".format(uuid=input_data['uuid'], resource_type=resource_tables[resource_type])
resource = db.execute_query(query)
if len(resource) == 1:
print("This is what i got from db:")
print(resource)
insert_query = generate_insert_query(resource[0], resource_tables[resource_type])
write_seed_files_db(recipe_file, insert_query)
else:
print("User not found in database")

def handle_projects(input_data, db, recipe_file, resource_type):
query = "SELECT * FROM {resource_type} WHERE uuid = '{uuid}'".format(uuid=input_data['uuid'], resource_type=resource_tables[resource_type])
resource = db.execute_query(query)
if len(resource) == 1:
print("This is what i got from db:")
print(resource)
insert_query = generate_insert_query(resource[0], resource_tables[resource_type])
write_seed_files_db(recipe_file, insert_query)
else:
print("User not found in database")

def handle_documents(input_data, db, recipe_file, resource_type):
query = "SELECT * FROM {resource_type} WHERE uuid = '{uuid}'".format(uuid=input_data['uuid'], resource_type=resource_tables[resource_type])
resource = db.execute_query(query)
if len(resource) == 1:
print("This is what i got from db:")
print(resource)
insert_query = generate_insert_query(resource[0], resource_tables[resource_type])
write_seed_files_db(recipe_file, insert_query)
else:
print("User not found in database")

def handle_project_importers(input_data, db, recipe_file, resource_type):
query = "SELECT * FROM {resource_type} WHERE id = '{id}'".format(id=input_data['id'], resource_type=resource_tables[resource_type])
resource = db.execute_query(query)
if len(resource) == 1:
print("This is what i got from db:")
print(resource)
insert_query = generate_insert_query(resource[0], resource_tables[resource_type])
write_seed_files_db(recipe_file, insert_query)
else:
print("User not found in database")

def handle_knowledge_models(input_data, db, recipe_file, resource_type):
query = "SELECT * FROM {resource_type} WHERE id = '{id}'".format(id=input_data['id'], resource_type=resource_tables[resource_type])
resource = db.execute_query(query)
if len(resource) == 1:
print("This is what i got from db:")
print(resource)
insert_query = generate_insert_query(resource[0], resource_tables[resource_type])
write_seed_files_db(recipe_file, insert_query)
else:
print("User not found in database")

def handle_locales(input_data, db, recipe_file, resource_type):
query = "SELECT * FROM {resource_type} WHERE id = '{id}'".format(id=input_data['id'], resource_type=resource_tables[resource_type])
resource = db.execute_query(query)
if len(resource) == 1:
print("This is what i got from db:")
print(resource)
insert_query = generate_insert_query(resource[0], resource_tables[resource_type])
write_seed_files_db(recipe_file, insert_query)
else:
print("User not found in database")

def handle_document_templates(input_data, db, recipe_file, resource_type):
query = "SELECT * FROM {resource_type} WHERE id = '{id}'".format(id=input_data['id'], resource_type=resource_tables[resource_type])
resource = db.execute_query(query)
if len(resource) == 1:
print("This is what i got from db:")
print(resource)
insert_query = generate_insert_query(resource[0], resource_tables[resource_type])
write_seed_files_db(recipe_file, insert_query)
else:
print("User not found in database")


# Map resource types to handler functions
resource_handlers = {
"users": handle_uuid,
"projects": handle_uuid,
"documents": handle_uuid,
"project_importers": handle_id,
"knowledge_models": handle_id,
"locales": handle_id,
"document_templates": handle_id,
"users": handle_users,
"projects": handle_projects,
"documents": handle_documents,
"project_importers": handle_project_importers,
"knowledge_models": handle_knowledge_models,
"locales":handle_locales,
"document_templates": handle_document_templates,
}

# Map resources to their table names
Expand Down
85 changes: 85 additions & 0 deletions tmp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"users": [
{
"uuid": "e1c58e52-0824-4526-8ebe-ec38eec67030",
"first_name": "Isaac",
"last_name": "Newton",
"role": "researcher"
},
{
"uuid": "30d48cf4-8c8a-496f-bafe-585bd238f798",
"first_name": "Nikola",
"last_name": "Tesla",
"role": "dataSteward"
},
{
"uuid": "00000000-0000-0000-0000-000000000000",
"first_name": "System",
"last_name": "User",
"role": "admin"
},
{
"uuid": "ec6f8e90-2a91-49ec-aa3f-9eab2267fc66",
"first_name": "Albert",
"last_name": "Einstein",
"role": "admin"
}
],
"project_importers": [
{
"id": "dsw:replies-importer:0.1.0",
"name": "DSW Replies (JSON)",
"description": "Import from replies in JSON exported from DSW"
}
],
"knowledge_models": [
{
"id": "myorg:km-for-seeding:0.0.1",
"name": "KM for seeding",
"km_id": "km-for-seeding",
"description": ""
}
],
"locales": [
{
"id": "wizard:default:1.0.0",
"name": "English",
"code": "en",
"description": "Default English locale for Wizard UI"
}
],
"document_templates": [
{
"id": "myorg:dsw-seeding:0.0.1",
"name": "DSW seeding",
"template_id": "dsw-seeding"
},
{
"id": "myorg:dsw-seeding2:0.0.1",
"name": "DSW seeding 2",
"template_id": "dsw-seeding2"
},
{
"id": "myorg:dsw-seeding2:0.1.0",
"name": "DSW seeding 2",
"template_id": "dsw-seeding2"
},
{
"id": "dsw:questionnaire-report:2.12.0",
"name": "Questionnaire Report",
"template_id": "questionnaire-report"
}
],
"projects": [
{
"uuid": "7ec5c86a-946a-4386-ba3c-b27481288a62",
"name": "Jana Mart\u00ednkov\u00e1"
}
],
"documents": [
{
"uuid": "1034a4b0-d867-4b4b-b2a0-a3956b43cf95",
"name": "Jana Mart\u00ednkov\u00e1"
}
]
}

0 comments on commit d1190ab

Please sign in to comment.