-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.py
24 lines (22 loc) · 923 Bytes
/
init_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from app import create_app
from extensions import db
from models import User
from werkzeug.security import generate_password_hash
import os
def initialize_database():
app = create_app()
with app.app_context():
if not os.path.exists('stockfolio.db'):
db.create_all()
print("Database and tables created successfully.")
admin = User.query.filter_by(username='admin').first()
if not admin:
hashed_password = generate_password_hash('admin', method='pbkdf2:sha256')
admin_user = User(username='admin', password=hashed_password)
db.session.add(admin_user)
db.session.commit()
print("Admin user created successfully.")
else:
print("Admin user already exists.")
else:
print("Database already exists.")