Files
TimeTracker/app.py
Dries Peeters 8a378b7078 feat(clients,license,db): add client management, enhanced DB init, and tests
- Clients: add model, routes, and templates
  - app/models/client.py
  - app/routes/clients.py
  - templates/clients/{create,edit,list,view}.html
  - docs/CLIENT_MANAGEMENT_README.md
- Database: add enhanced init/verify scripts, migrations, and docs
  - docker/{init-database-enhanced.py,start-enhanced.py,verify-database.py}
  - docs/ENHANCED_DATABASE_STARTUP.md
  - migrations/{add_analytics_column.sql,add_analytics_setting.py,migrate_to_client_model.py}
- Scripts: add version manager and docker network test helpers
  - scripts/version-manager.{bat,ps1,py,sh}
  - scripts/test-docker-network.{bat,sh}
  - docs/VERSION_MANAGEMENT.md
- UI: tweak base stylesheet
  - app/static/base.css
- Tests: add client system test
  - test_client_system.py
2025-09-01 11:34:45 +02:00

84 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""
Time Tracker Application Entry Point
"""
import os
import atexit
from app import create_app, db
from app.models import User, Project, TimeEntry, Task, Settings, Invoice, InvoiceItem, Client
app = create_app()
def cleanup_on_exit():
"""Cleanup function called when the application exits"""
try:
from app.utils.license_server import stop_license_client
stop_license_client()
print("Phone home function stopped")
except Exception as e:
print(f"Error stopping phone home function: {e}")
# Register cleanup function
atexit.register(cleanup_on_exit)
@app.shell_context_processor
def make_shell_context():
"""Add database models to Flask shell context"""
return {
'db': db,
'User': User,
'Project': Project,
'TimeEntry': TimeEntry,
'Task': Task,
'Settings': Settings,
'Invoice': Invoice,
'InvoiceItem': InvoiceItem,
'Client': Client
}
@app.cli.command()
def init_db():
"""Initialize the database with tables and default data"""
from app.models import Settings, User
# 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')