homedocs/app/__init__.py
2025-03-31 17:40:16 +02:00

104 lines
2.9 KiB
Python

from flask import Flask, g, redirect, url_for, render_template, session
import datetime
import os
import secrets
def create_app(config_name="development"):
app = Flask(__name__, static_folder="static", template_folder="templates")
# Load configuration
if config_name == "production":
app.config.from_object("config.ProductionConfig")
elif config_name == "testing":
app.config.from_object("config.TestingConfig")
else:
app.config.from_object("config.DevelopmentConfig")
# Ensure SECRET_KEY is set
if not app.config.get('SECRET_KEY'):
app.config['SECRET_KEY'] = secrets.token_hex(32)
# Initialize extensions
from app.core.extensions import db, migrate, login_manager, bcrypt, limiter, csrf
db.init_app(app)
migrate.init_app(app, db)
login_manager.init_app(app)
bcrypt.init_app(app)
csrf.init_app(app)
limiter.init_app(app)
# Initialize login manager
from app.core.auth import User
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
login_manager.login_view = "auth.login"
login_manager.login_message = "Please log in to access this page."
login_manager.login_message_category = "info"
# Register template filters
from app.core.template_filters import bp as filters_bp
from app.core.template_filters import markdown_filter
app.register_blueprint(filters_bp)
# Register the markdown filter directly
app.jinja_env.filters['markdown'] = markdown_filter
# Create database tables
with app.app_context():
try:
db.create_all()
print("Database tables created successfully")
except Exception as e:
print(f"Error with database setup: {e}")
# Register blueprints
from app.routes.auth import bp as auth_bp
app.register_blueprint(auth_bp)
from app.routes.dashboard import bp as dashboard_bp
app.register_blueprint(dashboard_bp)
from app.routes.ipam import bp as ipam_bp
app.register_blueprint(ipam_bp)
from app.routes.api import bp as api_bp
app.register_blueprint(api_bp)
from app.routes.importexport import bp as importexport_bp
app.register_blueprint(importexport_bp)
# Add static assets blueprint
from app.routes.static import bp as static_bp
app.register_blueprint(static_bp)
# Add session handling
@app.before_request
def make_session_permanent():
session.permanent = True
# Add error handlers
@app.errorhandler(404)
def page_not_found(e):
return render_template("errors/404.html", title="Page Not Found"), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template("errors/500.html", title="Server Error"), 500
@app.errorhandler(403)
def forbidden(e):
return render_template("errors/403.html", title="Forbidden"), 403
return app