Files
CS374-Database-Systems/fab/mvstubs.py

55 lines
1.5 KiB
Python
Raw Normal View History

"""Generate a basic ModelView class for each table in the database."""
import socket
import psycopg
# Determine whether connecting from on/off campus
try:
socket.gethostbyname("data.cs.jmu.edu")
HOST = "data.cs.jmu.edu"
except socket.gaierror:
HOST = "localhost"
# Get all tables and their columns
with psycopg.connect(
host=HOST, user="tamassno", dbname="sec2", password="113880616"
) as conn:
with conn.cursor() as cur:
cur.execute(
"""
SELECT table_name, column_name
FROM information_schema.columns
WHERE table_schema = 'tamassno'
AND table_name NOT LIKE 'ab_%'
ORDER BY table_name, ordinal_position;
"""
)
schema = cur.fetchall()
# Build a dictionary of results by table
tables: dict[str, list[str]] = {}
for table_name, column_name in schema:
tables.setdefault(table_name, []).append(column_name)
# Generate Flask-AppBuilder ModelView classes
for table, columns in tables.items():
name = table.capitalize()
print(f"class {name}(ModelView):")
print(f" datamodel = SQLAInterface(models.{name})")
print(f" route_base = '/{table}'")
print(f" list_title = '{name}s'")
print(f" list_columns = {columns}")
print()
# Generate code to add each view to the app
for table in tables:
name = table.capitalize()
print("appbuilder.add_view(")
print(f" {name},")
print(f' "{name}s",')
print(' icon="fa-database",')
print(' category="Admin",')
print(")")
print()