"Refactor database connection for improved data consistency (feat"
This commit is contained in:
Binary file not shown.
47
app.py
47
app.py
@@ -2026,6 +2026,7 @@ def admin_update_database():
|
|||||||
def get_mindmap_node(node_id):
|
def get_mindmap_node(node_id):
|
||||||
"""Liefert die Mindmap-Daten für einen bestimmten Knoten und seine Subthemen."""
|
"""Liefert die Mindmap-Daten für einen bestimmten Knoten und seine Subthemen."""
|
||||||
try:
|
try:
|
||||||
|
# Erkennen besonderer Knotennamen
|
||||||
if node_id == 'root':
|
if node_id == 'root':
|
||||||
# Hauptknoten (Wissen) abrufen
|
# Hauptknoten (Wissen) abrufen
|
||||||
wissen_node = MindMapNode.query.filter_by(name="Wissen").first()
|
wissen_node = MindMapNode.query.filter_by(name="Wissen").first()
|
||||||
@@ -2041,11 +2042,39 @@ def get_mindmap_node(node_id):
|
|||||||
|
|
||||||
# Alle direkten Kinder des Wissen-Knotens holen
|
# Alle direkten Kinder des Wissen-Knotens holen
|
||||||
nodes = wissen_node.children.all()
|
nodes = wissen_node.children.all()
|
||||||
else:
|
parent_node = wissen_node
|
||||||
|
elif node_id.isdigit():
|
||||||
# Bestimmten Knoten und seine Kinder abrufen
|
# Bestimmten Knoten und seine Kinder abrufen
|
||||||
parent_node = MindMapNode.query.get_or_404(node_id)
|
parent_node = MindMapNode.query.get_or_404(int(node_id))
|
||||||
|
nodes = parent_node.children.all()
|
||||||
|
else:
|
||||||
|
# Versuche, einen Knoten mit diesem Namen zu finden
|
||||||
|
parent_node = MindMapNode.query.filter_by(name=node_id.capitalize()).first()
|
||||||
|
|
||||||
|
if not parent_node:
|
||||||
|
# Versuche, eine Kategorie mit diesem Namen zu finden
|
||||||
|
category = Category.query.filter(func.lower(Category.name) == func.lower(node_id)).first()
|
||||||
|
if category:
|
||||||
|
# Finde oder erstelle einen Knoten für diese Kategorie
|
||||||
|
parent_node = MindMapNode.query.filter_by(category_id=category.id).first()
|
||||||
|
if not parent_node:
|
||||||
|
parent_node = MindMapNode(
|
||||||
|
name=category.name,
|
||||||
|
description=category.description,
|
||||||
|
color_code=category.color_code,
|
||||||
|
category_id=category.id,
|
||||||
|
is_public=True
|
||||||
|
)
|
||||||
|
db.session.add(parent_node)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
if not parent_node:
|
||||||
|
# Fallback zum Wissen-Knoten
|
||||||
|
parent_node = MindMapNode.query.filter_by(name="Wissen").first()
|
||||||
|
if not parent_node:
|
||||||
|
return jsonify({'error': 'Knoten nicht gefunden'}), 404
|
||||||
|
|
||||||
nodes = parent_node.children.all()
|
nodes = parent_node.children.all()
|
||||||
wissen_node = parent_node
|
|
||||||
|
|
||||||
# Ergebnisdaten vorbereiten
|
# Ergebnisdaten vorbereiten
|
||||||
nodes_data = []
|
nodes_data = []
|
||||||
@@ -2053,10 +2082,10 @@ def get_mindmap_node(node_id):
|
|||||||
|
|
||||||
# Hauptknoten hinzufügen
|
# Hauptknoten hinzufügen
|
||||||
nodes_data.append({
|
nodes_data.append({
|
||||||
'id': wissen_node.id,
|
'id': parent_node.id,
|
||||||
'name': wissen_node.name,
|
'name': parent_node.name,
|
||||||
'description': wissen_node.description or '',
|
'description': parent_node.description or '',
|
||||||
'color_code': wissen_node.color_code or '#4299E1',
|
'color_code': parent_node.color_code or '#4299E1',
|
||||||
'is_center': True,
|
'is_center': True,
|
||||||
'has_children': len(nodes) > 0
|
'has_children': len(nodes) > 0
|
||||||
})
|
})
|
||||||
@@ -2074,7 +2103,7 @@ def get_mindmap_node(node_id):
|
|||||||
|
|
||||||
# Verbindung zum Elternknoten hinzufügen
|
# Verbindung zum Elternknoten hinzufügen
|
||||||
edges_data.append({
|
edges_data.append({
|
||||||
'source_id': wissen_node.id,
|
'source_id': parent_node.id,
|
||||||
'target_id': node.id,
|
'target_id': node.id,
|
||||||
'strength': 0.8
|
'strength': 0.8
|
||||||
})
|
})
|
||||||
@@ -2086,6 +2115,8 @@ def get_mindmap_node(node_id):
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Fehler beim Abrufen der Mindmap-Daten: {str(e)}")
|
print(f"Fehler beim Abrufen der Mindmap-Daten: {str(e)}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'success': False,
|
'success': False,
|
||||||
'error': 'Mindmap-Daten konnten nicht geladen werden'
|
'error': 'Mindmap-Daten konnten nicht geladen werden'
|
||||||
|
|||||||
Binary file not shown.
@@ -11,19 +11,33 @@ import importlib.util
|
|||||||
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
sys.path.insert(0, parent_dir)
|
sys.path.insert(0, parent_dir)
|
||||||
|
|
||||||
from app import app, db_path
|
# Direkt den Datenbankpfad berechnen, statt ihn aus app.py zu importieren
|
||||||
|
def get_db_path():
|
||||||
|
"""Berechnet den absoluten Pfad zur Datenbank"""
|
||||||
|
basedir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
|
||||||
|
return os.path.join(basedir, 'database', 'systades.db')
|
||||||
|
|
||||||
|
# Import models direkt
|
||||||
from models import db
|
from models import db
|
||||||
|
|
||||||
def ensure_db_dir():
|
def ensure_db_dir():
|
||||||
"""Make sure the database directory exists."""
|
"""Make sure the database directory exists."""
|
||||||
|
db_path = get_db_path()
|
||||||
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
||||||
|
|
||||||
def fix_database_schema():
|
def fix_database_schema():
|
||||||
"""Fix the database schema by adding missing columns."""
|
"""Fix the database schema by adding missing columns."""
|
||||||
|
# Import Flask-App erst innerhalb der Funktion
|
||||||
|
from flask import Flask
|
||||||
|
from app import app
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
# Ensure directory exists
|
# Ensure directory exists
|
||||||
ensure_db_dir()
|
ensure_db_dir()
|
||||||
|
|
||||||
|
# Get database path
|
||||||
|
db_path = get_db_path()
|
||||||
|
|
||||||
# Check if database exists, create tables if needed
|
# Check if database exists, create tables if needed
|
||||||
if not os.path.exists(db_path):
|
if not os.path.exists(db_path):
|
||||||
print("Database doesn't exist. Creating all tables from scratch...")
|
print("Database doesn't exist. Creating all tables from scratch...")
|
||||||
|
|||||||
Reference in New Issue
Block a user