Spaces:
Running
Running
File size: 4,592 Bytes
ae1db1b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# database.py
import pyodbc
import urllib.parse
import hashlib
import json
import pickle
import random
from typing import Dict, List
from flask import Flask
from config import SQL_DRIVER, SQL_SERVER, SQL_DB, SQL_TRUSTED, SQL_USER, SQL_PASSWORD, SQL_PORT, SQL_ENCRYPT, SQL_TRUSTCERT
from models import db
def get_db_connection():
"""Get a raw pyodbc connection"""
return pyodbc.connect(
f"DRIVER={SQL_DRIVER};"
f"SERVER={SQL_SERVER};"
f"DATABASE={SQL_DB};"
f"Trusted_Connection={SQL_TRUSTED};"
)
def row_to_dict(cursor, row) -> Dict:
"""Convert a database row to dictionary"""
if row is None:
return {}
cols = [col[0] for col in cursor.description]
return {cols[i]: row[i] for i in range(len(cols))}
def hash_password(password: str) -> str:
"""Hash password using SHA256"""
return hashlib.sha256(password.encode("utf-8")).hexdigest()
def init_database(app: Flask):
"""Initialize database connection for Flask app"""
_server = SQL_SERVER
if SQL_PORT:
_server = f"{SQL_SERVER},{SQL_PORT}"
if SQL_TRUSTED == "yes":
raw = (
f"DRIVER={{{SQL_DRIVER}}};"
f"SERVER={_server};"
f"DATABASE={SQL_DB};"
f"Trusted_Connection=yes;"
)
else:
raw = (
f"DRIVER={{{SQL_DRIVER}}};"
f"SERVER={_server};"
f"DATABASE={SQL_DB};"
f"UID={SQL_USER};PWD={SQL_PASSWORD};"
)
if SQL_ENCRYPT == "yes":
raw += "Encrypt=yes;"
if SQL_TRUSTCERT == "yes":
raw += "TrustServerCertificate=yes;"
params = urllib.parse.quote_plus(raw)
SQLALCHEMY_DATABASE_URI = f"mssql+pyodbc:///?odbc_connect={params}"
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
return db
def fetch_profile_for_role(user_id: str, role: str) -> Dict:
"""Fetch profile from the correct table based on role"""
table = {
"marriage": "Marriage",
"interview": "Interview",
"partnership": "Partnership"
}.get(role.lower())
if not table:
return {}
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"""
SELECT TOP 1 *
FROM {table}
WHERE user_id = ?
ORDER BY created_at DESC
""", (user_id,))
row = cur.fetchone()
if row is None:
return {}
prof = row_to_dict(cur, row)
# Normalize hobbies_interests if it exists
if "hobbies_interests" in prof and isinstance(prof["hobbies_interests"], str):
if prof["hobbies_interests"].strip().startswith("["):
try:
prof["hobbies_interests"] = json.loads(prof["hobbies_interests"])
except Exception:
prof["hobbies_interests"] = [s.strip() for s in prof["hobbies_interests"].split(",") if s.strip()]
else:
prof["hobbies_interests"] = [s.strip() for s in prof["hobbies_interests"].split(",") if s.strip()]
prof["user_id"] = str(user_id)
return prof
except pyodbc.Error as e:
print("Profile fetch error:", e)
return {}
finally:
try: conn.close()
except: pass
def fetch_expectation_data(user_id: str) -> Dict:
"""Fetch expectation data from ExpectationResponse table"""
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("""
SELECT * FROM ExpectationResponse
WHERE user_id = ?
ORDER BY created_at DESC
""", (user_id,))
row = cur.fetchone()
if row is None:
return {}
return row_to_dict(cur, row)
except Exception as e:
print(f"Error fetching expectation data: {e}")
return {}
finally:
try: conn.close()
except: pass
def fetch_marriage_profile_data(user_id: str) -> Dict:
"""Fetch marriage profile data for comparison"""
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("""
SELECT * FROM Marriage
WHERE user_id = ?
ORDER BY created_at DESC
""", (user_id,))
row = cur.fetchone()
if row is None:
return {}
return row_to_dict(cur, row)
except Exception as e:
print(f"Error fetching marriage profile data: {e}")
return {}
finally:
try: conn.close()
except: pass |