Spaces:
Running
Running
Download db_setup.py from sdurmaz/reportninja: direct link, hf CLI and curl.
- Browser
- Download file 1.58 kB
-
https://huggingface.co/spaces/sdurmaz/reportninja/resolve/main/db_setup.py
- Command line
-
hf download hf://spaces/sdurmaz/reportninja/db_setup.py
-
curl -L -o db_setup.py https://huggingface.co/spaces/sdurmaz/reportninja/resolve/main/db_setup.py
1.58 kB
| ```python | |
| import sqlite3 | |
| from werkzeug.security import generate_password_hash | |
| # Create SQLite database and tables | |
| def init_db(): | |
| conn = sqlite3.connect('reportninja.db') | |
| c = conn.cursor() | |
| # Users table | |
| c.execute('''CREATE TABLE IF NOT EXISTS users | |
| (id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| email TEXT UNIQUE NOT NULL, | |
| password TEXT NOT NULL, | |
| name TEXT, | |
| role TEXT DEFAULT 'user', | |
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''') | |
| # Reports table | |
| c.execute('''CREATE TABLE IF NOT EXISTS reports | |
| (id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| title TEXT NOT NULL, | |
| description TEXT, | |
| category TEXT, | |
| status TEXT DEFAULT 'draft', | |
| created_by INTEGER, | |
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | |
| updated_at TIMESTAMP, | |
| FOREIGN KEY(created_by) REFERENCES users(id))''') | |
| # Add admin user if not exists | |
| admin_email = "admin@reportninja.com" | |
| c.execute("SELECT id FROM users WHERE email=?", (admin_email,)) | |
| if not c.fetchone(): | |
| admin_password = generate_password_hash("admin123") | |
| c.execute("INSERT INTO users (email, password, name, role) VALUES (?, ?, ?, ?)", | |
| (admin_email, admin_password, "Admin", "admin")) | |
| conn.commit() | |
| conn.close() | |
| if __name__ == '__main__': | |
| init_db() | |
| print("Database initialized successfully!") | |
| ``` |