api/modules/database/init/init_school.py
2025-11-14 14:47:19 +00:00

78 lines
3.6 KiB
Python

import os
from modules.logger_tool import initialise_logger
logger = initialise_logger(__name__, os.getenv("LOG_LEVEL"), os.getenv("LOG_PATH"), 'default', True)
from modules.database.schemas.nodes.schools.schools import SchoolNode
import modules.database.init.init_school_timetable as init_school_timetable
import modules.database.tools.neontology_tools as neon
import modules.database.tools.supabase_storage_tools as storage_tools
def create_school(db_name: str, uuid_string: str, name: str, website: str, school_type: str, is_public: bool = True, school_node: SchoolNode | None = None, dataframes=None):
if not name or not uuid_string or not website or not school_type:
logger.error("School name, uuid_string, website and school_type are required to create a school.")
raise ValueError("School name, uuid_string, website and school_type are required to create a school.")
logger.info(f"Initialising Neontology connection...")
neon.init_neontology_connection()
# Initialize storage tools for school
storage_tools_instance = storage_tools.SupabaseStorageTools(db_name, init_run_type="school")
# Generate the storage path for the school node
school_dir_created, node_storage_path = storage_tools_instance.create_school_storage_path(uuid_string)
logger.info(f"Generated school storage path: {node_storage_path}")
# Create School Node if not provided
if not school_node:
if is_public:
school_node = SchoolNode(
uuid_string=uuid_string,
node_storage_path=node_storage_path,
name=name,
website=website,
school_type=school_type
)
else:
# Create private school node with default values
school_node = SchoolNode(
uuid_string=uuid_string,
node_storage_path=node_storage_path,
name=name,
website=website,
school_type=school_type,
establishment_number="0000",
establishment_name=name,
establishment_type="Default",
establishment_status="Open",
phase_of_education="All",
statutory_low_age=11,
statutory_high_age=18,
school_capacity=1000
)
else:
# Update existing school node with the storage path
school_node.node_storage_path = node_storage_path
# First create/merge the school node in the main cc.institutes database
logger.info(f"Creating school node in main cc.institutes database...")
neon.create_or_merge_neontology_node(school_node, database="cc.institutes", operation='merge')
# Then create/merge the school node in the specific school database
logger.info(f"Creating school node in specific database {db_name}...")
neon.create_or_merge_neontology_node(school_node, database=db_name, operation='merge')
school_nodes = {
'school_node': school_node,
'db_name': db_name,
'storage_path': node_storage_path
}
if dataframes is not None:
logger.info(f"Creating school timetable for {name} with {len(dataframes)} dataframes...")
school_timetable_nodes = init_school_timetable.create_school_timetable(dataframes, db_name, school_node, storage_tools_instance)
school_nodes['school_timetable_nodes'] = school_timetable_nodes
else:
logger.warning(f"No dataframes provided for {name}, skipping school timetable...")
logger.info(f"School {name} created successfully with storage path: {node_storage_path}")
return school_nodes