Files
kbcrm/App_Server/root/approot.py
T
gg 13eaaec9c1 Server: CRM-Domain mit REST-API, Autorisierung und CLI
Neues crm-Paket mit Datenmodell, Geschäftslogik und REST-Endpunkten
für die Entität Kunde (/xhr/kunde/{list,get,create,update,delete}).

authorisation_required-Decorator: app-spezifische Rollenprüfung via
has_permission(appid, ...). SysAdmin systemweit, AppAdmin implizit in
seiner Anwendung, User (Mitarbeiter) als Basis-Rolle, Zusatz-Rollen
wie KundenManager additiv.

Flask CLI: init-db (Schema + Musterdaten + App-Rollen) und reset-db
(Bestätigung, behält SysAdmin).

config.ini: Neuer appid-Eintrag (KBPlusCRM), getrennt vom technischen
PROJEKTNAME. checkdb: appid aus config, Migration _db_V01_02 für
bestehende DBs.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-09-03 16:12:13 +02:00

65 lines
2.3 KiB
Python

from flask import Flask, jsonify
from flask_login import LoginManager
from settings import Configuration
from db import dbauth
from db import dbapp
from auth.model.checkdb import checkupdates as checkupdatesauth
from os import path
login_manager = LoginManager()
def init_app():
app = Flask(__name__)
config: Configuration = Configuration("root/config.ini")
_projektname = config.app_projectname
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////usr/var/pythonappdb/{}.db".format(_projektname)
app.config["SQLALCHEMY_BINDS"] = {
"auth": "sqlite:////usr/var/pythonappdb/auth.db",
"appdb": "sqlite:////usr/var/pythonappdb/{}.db".format(_projektname)
}
app.config["DEBUG"] = path.exists("root/debug")
app.config["PROJEKTNAME"] = config.app_projectname
app.config["APPID"] = config.app_appid
app.config["APPVERSION"] = config.clientapp_version
app.config["SECRET_KEY"] = config.flask_secret
# Initialize Plugins
# dbapp ist dasselbe Objekt wie dbauth (siehe db.py); beide teilen sich
# die SQLAlchemy-Registry. Daher genügt ein einziger init_app-Aufruf, der
# sowohl den auth- als auch den appdb-Bind initialisiert.
login_manager.init_app(app)
dbauth.init_app(app)
# Globale Fehlerbehandlung für ApplicationExceptionen. Die Route-internen
# try/except-Blöcke (auth) fangen weiterhin lokal ab; für alle nicht
# gefangenen Fälle (insbesondere crm) greift dieser Handler.
from auth.model.errors import ApplicationError, AutorisationError
@app.errorhandler(ApplicationError)
def handle_application_error(ex):
return jsonify(ex.error), ex.htmlstatus
@app.errorhandler(AutorisationError)
def handle_autorisation_error(ex):
return jsonify(ex.error), ex.htmlstatus
with app.app_context():
# Include our Routes
print("before auth.routes", flush=True)
from auth import routes
from routes import general
from crm import routes as crm_routes
from crm.model import checkdb as crm_checkdb
checkupdatesauth()
crm_checkdb.checkupdates()
# Flask CLI-Befehle (init-db, reset-db) registrieren
from cli import register_cli_commands
register_cli_commands(app)
return app