Features added: - Admin authentication system with is_admin field - Complete admin dashboard with user statistics - User management (create, edit, delete, toggle admin) - Protected admin routes with @admin_required decorator - Security features (prevent self-deletion, last admin protection) - Responsive admin UI with Bootstrap integration - Database migration script for admin field - Admin navigation link for authorized users Technical improvements: - Enhanced 3-column dashboard layout (tables | editor | saved queries) - Removed plus button and made right sidebar more compact - Admin user (admin/admin123) automatically created with admin privileges - Full CRUD operations for user management - Flash messages for user feedback - Form validation and error handling
59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_login import LoginManager
|
|
from flask_cors import CORS
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
# Lade Umgebungsvariablen
|
|
load_dotenv()
|
|
|
|
db = SQLAlchemy()
|
|
login_manager = LoginManager()
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
|
|
# Konfiguration
|
|
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY') or 'dev-secret-key'
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL') or 'sqlite:///querybuilder.db'
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
# Initialisiere Extensions
|
|
db.init_app(app)
|
|
login_manager.init_app(app)
|
|
login_manager.login_view = 'auth.login'
|
|
login_manager.login_message = 'Bitte melden Sie sich an, um auf diese Seite zuzugreifen.'
|
|
|
|
CORS(app)
|
|
|
|
# Registriere Blueprints
|
|
from app.routes.auth import auth_bp
|
|
from app.routes.main import main_bp
|
|
from app.routes.api import api_bp
|
|
from app.routes.admin import admin_bp
|
|
|
|
app.register_blueprint(auth_bp, url_prefix='/auth')
|
|
app.register_blueprint(main_bp)
|
|
app.register_blueprint(api_bp, url_prefix='/api')
|
|
app.register_blueprint(admin_bp, url_prefix='/admin')
|
|
|
|
# Erstelle Datenbanktabellen
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
# Erstelle Standard-Admin-User falls nicht vorhanden
|
|
from app.models import User
|
|
admin = User.query.filter_by(username='admin').first()
|
|
if not admin:
|
|
admin = User(username='admin', email='admin@example.com', is_admin=True)
|
|
admin.set_password('admin123')
|
|
db.session.add(admin)
|
|
db.session.commit()
|
|
|
|
return app
|
|
|
|
@login_manager.user_loader
|
|
def load_user(user_id):
|
|
from app.models import User
|
|
return User.query.get(int(user_id)) |