Refactor node relationship handling in app.py and introduce new routes for thoughts association with nodes.

This commit is contained in:
2025-04-28 13:20:41 +02:00
parent 5399169b11
commit 65c44ab371
20 changed files with 181 additions and 152 deletions

35
app.py
View File

@@ -22,7 +22,7 @@ from dotenv import load_dotenv
from models import (
db, User, Thought, Comment, MindMapNode, ThoughtRelation, ThoughtRating,
RelationType, Category, UserMindmap, UserMindmapNode, MindmapNote,
node_thought_association, user_thought_bookmark
node_thought_association, user_thought_bookmark, node_relationship
)
# Lade .env-Datei
@@ -903,45 +903,30 @@ def delete_note(note_id):
@app.route('/api/mindmap')
def get_mindmap():
"""API-Endpunkt zur Bereitstellung der Mindmap-Daten in hierarchischer Form."""
# Alle root-Nodes (ohne parent) abrufen
root_nodes = MindMapNode.query.filter_by(parent_id=None).all()
if not root_nodes:
# Wenn keine Nodes existieren, rufen wir initialize_database direkt auf
# anstatt create_sample_mindmap zu verwenden
with app.app_context():
initialize_database()
root_nodes = MindMapNode.query.filter_by(parent_id=None).all()
# Ergebnisse in hierarchischer Struktur zurückgeben
# Root-Knoten: Knoten ohne Eltern
root_nodes = MindMapNode.query.\
outerjoin(node_relationship, MindMapNode.id == node_relationship.c.child_id).\
filter(node_relationship.c.parent_id == None).all()
result = []
for node in root_nodes:
node_data = build_node_tree(node)
result.append(node_data)
return jsonify({"nodes": result})
def build_node_tree(node):
"""Erzeugt eine hierarchische Darstellung eines Knotens inkl. seiner Kindknoten."""
# Gedankenzähler abrufen von der many-to-many Beziehung
thought_count = len(node.thoughts)
# Daten für aktuellen Knoten
node_data = {
"id": node.id,
"name": node.name,
"description": f"Knoten mit {thought_count} Gedanken",
"description": node.description or "",
"thought_count": thought_count,
"children": []
}
# Rekursiv Kinder hinzufügen
child_nodes = MindMapNode.query.filter_by(parent_id=node.id).all()
for child_node in child_nodes:
child_data = build_node_tree(child_node)
for child in node.children:
child_data = build_node_tree(child)
node_data["children"].append(child_data)
return node_data
@app.route('/api/nodes/<int:node_id>/thoughts')
@@ -1250,7 +1235,7 @@ def chat_with_assistant():
# Zusammenfassen mehrerer Gedanken oder Analyse anfordern
system_message = (
"Du bist ein hilfreicher Assistent, der Zugriff auf die Wissensdatenbank hat. "
"Du bist ein hilfreicher Assistent, der Zugriff auf die Wissensdatenbank hat. Du antwortest nur auf Fragen bezüglich Systades und der Wissensdatenbank. "
"Du kannst Informationen zu Gedanken, Kategorien und Mindmaps liefern. "
"Antworte informativ, sachlich und gut strukturiert auf Deutsch."
)