Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from sqlalchemy import create_engine, text
|
|
|
|
| 3 |
|
| 4 |
-
# Fixed SQL Server details
|
| 5 |
SERVER = "indsr-foundry.database.windows.net"
|
| 6 |
DATABASE = "foundry_db_monarch"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
st.set_page_config(page_title="SQL Server Connection Test", layout="centered")
|
| 9 |
|
|
@@ -13,10 +19,35 @@ st.title("SQL Server Connection Test")
|
|
| 13 |
username = st.text_input("Database Username", placeholder="Enter your username")
|
| 14 |
password = st.text_input("Database Password", type="password", placeholder="Enter your password")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
try:
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
engine = create_engine(conn_str, pool_pre_ping=True)
|
| 21 |
|
| 22 |
with engine.connect() as connection:
|
|
@@ -27,10 +58,15 @@ def test_connection(username, password):
|
|
| 27 |
except Exception as e:
|
| 28 |
return f"❌ Connection failed: {str(e)}"
|
| 29 |
|
|
|
|
| 30 |
# Button to test connection
|
| 31 |
if st.button("Test Connection"):
|
| 32 |
if username and password:
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
else:
|
| 36 |
st.write("⚠️ Please enter both username and password.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from sqlalchemy import create_engine, text
|
| 3 |
+
import pyodbc
|
| 4 |
|
| 5 |
+
# Fixed SQL Server details (same as VBA)
|
| 6 |
SERVER = "indsr-foundry.database.windows.net"
|
| 7 |
DATABASE = "foundry_db_monarch"
|
| 8 |
+
DRIVER = "ODBC Driver 18 for SQL Server"
|
| 9 |
+
|
| 10 |
+
# VBA uses a service account initially
|
| 11 |
+
SERVICE_USERNAME = "bulk_import_user"
|
| 12 |
+
SERVICE_PASSWORD = "673704CB-053D-4B66-8AFA-3A18147EB2B3"
|
| 13 |
|
| 14 |
st.set_page_config(page_title="SQL Server Connection Test", layout="centered")
|
| 15 |
|
|
|
|
| 19 |
username = st.text_input("Database Username", placeholder="Enter your username")
|
| 20 |
password = st.text_input("Database Password", type="password", placeholder="Enter your password")
|
| 21 |
|
| 22 |
+
|
| 23 |
+
def get_user_key():
|
| 24 |
+
"""Step 1: Connect using service account and retrieve user key"""
|
| 25 |
try:
|
| 26 |
+
service_conn_str = (
|
| 27 |
+
f"mssql+pyodbc://{SERVICE_USERNAME}:{SERVICE_PASSWORD}@{SERVER}/{DATABASE}?"
|
| 28 |
+
f"driver={DRIVER}&Encrypt=yes&TrustServerCertificate=no"
|
| 29 |
+
)
|
| 30 |
+
engine = create_engine(service_conn_str, pool_pre_ping=True)
|
| 31 |
+
|
| 32 |
+
with engine.connect() as connection:
|
| 33 |
+
result = connection.execute(text("EXEC [entity].[PR_user_find] @people_id=:user_id"), {"user_id": username})
|
| 34 |
+
user_key = result.scalar() # Extract the user key
|
| 35 |
+
|
| 36 |
+
return user_key
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"❌ Failed to retrieve user key: {str(e)}"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_connection(user_key, password):
|
| 42 |
+
"""Step 2: Reconnect using the retrieved user key (like VBA)"""
|
| 43 |
+
try:
|
| 44 |
+
if not user_key:
|
| 45 |
+
return "⚠️ User key not found. Cannot proceed."
|
| 46 |
+
|
| 47 |
+
conn_str = (
|
| 48 |
+
f"mssql+pyodbc://{user_key}:{password}@{SERVER}/{DATABASE}?"
|
| 49 |
+
f"driver={DRIVER}&Encrypt=yes&TrustServerCertificate=no"
|
| 50 |
+
)
|
| 51 |
engine = create_engine(conn_str, pool_pre_ping=True)
|
| 52 |
|
| 53 |
with engine.connect() as connection:
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
return f"❌ Connection failed: {str(e)}"
|
| 60 |
|
| 61 |
+
|
| 62 |
# Button to test connection
|
| 63 |
if st.button("Test Connection"):
|
| 64 |
if username and password:
|
| 65 |
+
user_key = get_user_key()
|
| 66 |
+
if isinstance(user_key, str) and user_key.startswith("❌"):
|
| 67 |
+
st.write(user_key) # Display error if user key retrieval failed
|
| 68 |
+
else:
|
| 69 |
+
result = test_connection(user_key, password)
|
| 70 |
+
st.write(result)
|
| 71 |
else:
|
| 72 |
st.write("⚠️ Please enter both username and password.")
|