mirror of
https://github.com/DRYTRIX/TimeTracker.git
synced 2026-01-19 19:09:56 -06:00
67 lines
1.8 KiB
Python
67 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Time Tracker Application Entry Point
|
|
"""
|
|
|
|
import os
|
|
from app import create_app, db
|
|
from app.models import User, Project, TimeEntry, Settings
|
|
|
|
app = create_app()
|
|
|
|
@app.shell_context_processor
|
|
def make_shell_context():
|
|
"""Add database models to Flask shell context"""
|
|
return {
|
|
'db': db,
|
|
'User': User,
|
|
'Project': Project,
|
|
'TimeEntry': TimeEntry,
|
|
'Settings': Settings
|
|
}
|
|
|
|
@app.cli.command()
|
|
def init_db():
|
|
"""Initialize the database with tables and default data"""
|
|
from app.models import Settings
|
|
|
|
# Create all tables
|
|
db.create_all()
|
|
|
|
# Initialize settings if they don't exist
|
|
if not Settings.query.first():
|
|
settings = Settings()
|
|
db.session.add(settings)
|
|
db.session.commit()
|
|
print("Database initialized with default settings")
|
|
|
|
# Create admin user if it doesn't exist
|
|
admin_username = os.getenv('ADMIN_USERNAMES', 'admin').split(',')[0]
|
|
if not User.query.filter_by(username=admin_username).first():
|
|
admin_user = User(username=admin_username, role='admin')
|
|
db.session.add(admin_user)
|
|
db.session.commit()
|
|
print(f"Created admin user: {admin_username}")
|
|
|
|
print("Database initialization complete!")
|
|
|
|
@app.cli.command()
|
|
def create_admin():
|
|
"""Create an admin user"""
|
|
username = input("Enter admin username: ").strip()
|
|
if not username:
|
|
print("Username cannot be empty")
|
|
return
|
|
|
|
if User.query.filter_by(username=username).first():
|
|
print(f"User {username} already exists")
|
|
return
|
|
|
|
user = User(username=username, role='admin')
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
print(f"Created admin user: {username}")
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=8080, debug=os.getenv('FLASK_DEBUG', 'false').lower() == 'true')
|