# WebOrganizations.py import PersistentData import CockpitITSM def init_organizations_interface(ui): """ Registers the bridge between Organizations.html and Python logic. """ def handle_get_organizations(_, __): """Syncs with API then returns the active organizations list from DB.""" success = CockpitITSM.update_organizations() if not success: ui.send_message("organizations_response", { "success": False, "message": "Failed to refresh organizations. Check API configuration." }) return orgs = PersistentData.get_active_organizations() ui.send_message("organizations_data", {"organizations": orgs}) def handle_save_organizations(_, data): """Persists the selected state for each organization.""" try: organizations = data.get("organizations", []) for org in organizations: PersistentData.db.update( "organizations", {"selected": org["selected"]}, condition=f"id = {org['id']}" ) print(f"DEBUG: Saved selection for {len(organizations)} organizations.") ui.send_message("organizations_response", { "success": True, "message": f"Selection saved ({len(organizations)} organizations)." }) except Exception as e: print(f"ERROR saving organizations: {str(e)}") ui.send_message("organizations_response", { "success": False, "message": f"Save error: {str(e)}" }) # Registering the handlers ui.on_message("get_organizations", handle_get_organizations) ui.on_message("save_organizations", handle_save_organizations)