Implement error handling and default node creation for mindmap routes; initialize database on first request to ensure root node exists.
This commit is contained in:
@@ -116,7 +116,16 @@ def index():
|
|||||||
# Route for the mindmap page
|
# Route for the mindmap page
|
||||||
@app.route('/mindmap')
|
@app.route('/mindmap')
|
||||||
def mindmap():
|
def mindmap():
|
||||||
return render_template('mindmap.html')
|
try:
|
||||||
|
root_node = MindMapNode.query.filter_by(parent_id=None).first()
|
||||||
|
if not root_node:
|
||||||
|
root_node = MindMapNode(name="Wissenschaft")
|
||||||
|
db.session.add(root_node)
|
||||||
|
db.session.commit()
|
||||||
|
return render_template('mindmap.html')
|
||||||
|
except Exception as e:
|
||||||
|
app.logger.error(f"Error loading mindmap: {str(e)}")
|
||||||
|
return render_template('mindmap.html', error="Fehler beim Laden der Mindmap. Bitte versuchen Sie es erneut.")
|
||||||
|
|
||||||
# Route for user profile
|
# Route for user profile
|
||||||
@app.route('/profile')
|
@app.route('/profile')
|
||||||
@@ -128,17 +137,28 @@ def profile():
|
|||||||
# API routes for mindmap and thoughts
|
# API routes for mindmap and thoughts
|
||||||
@app.route('/api/mindmap')
|
@app.route('/api/mindmap')
|
||||||
def get_mindmap():
|
def get_mindmap():
|
||||||
root_nodes = MindMapNode.query.filter_by(parent_id=None).all()
|
try:
|
||||||
|
root_nodes = MindMapNode.query.filter_by(parent_id=None).all()
|
||||||
def build_tree(node):
|
|
||||||
return {
|
def build_tree(node):
|
||||||
'id': node.id,
|
return {
|
||||||
'name': node.name,
|
'id': node.id,
|
||||||
'children': [build_tree(child) for child in node.children]
|
'name': node.name,
|
||||||
}
|
'children': [build_tree(child) for child in node.children]
|
||||||
|
}
|
||||||
result = [build_tree(node) for node in root_nodes]
|
|
||||||
return jsonify(result)
|
result = [build_tree(node) for node in root_nodes]
|
||||||
|
if not result:
|
||||||
|
# Create default root if no nodes exist
|
||||||
|
root = MindMapNode(name="Wissenschaft")
|
||||||
|
db.session.add(root)
|
||||||
|
db.session.commit()
|
||||||
|
result = [build_tree(root)]
|
||||||
|
|
||||||
|
return jsonify(result)
|
||||||
|
except Exception as e:
|
||||||
|
app.logger.error(f"Error in get_mindmap: {str(e)}")
|
||||||
|
return jsonify({'error': 'Fehler beim Laden der Mindmap'}), 500
|
||||||
|
|
||||||
@app.route('/api/thoughts/<int:node_id>', methods=['GET'])
|
@app.route('/api/thoughts/<int:node_id>', methods=['GET'])
|
||||||
def get_thoughts(node_id):
|
def get_thoughts(node_id):
|
||||||
@@ -256,9 +276,27 @@ def admin():
|
|||||||
|
|
||||||
return render_template('admin.html', users=users, nodes=nodes, thoughts=thoughts)
|
return render_template('admin.html', users=users, nodes=nodes, thoughts=thoughts)
|
||||||
|
|
||||||
|
@app.before_first_request
|
||||||
|
def initialize_database():
|
||||||
|
db.create_all()
|
||||||
|
# Create root node if it doesn't exist
|
||||||
|
if not MindMapNode.query.first():
|
||||||
|
root = MindMapNode(name="Wissenschaft")
|
||||||
|
db.session.add(root)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
# Flask starten
|
# Flask starten
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
# Make sure tables exist
|
# Initialize database and create root node
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
try:
|
||||||
|
root = MindMapNode.query.filter_by(parent_id=None).first()
|
||||||
|
if not root:
|
||||||
|
root = MindMapNode(name="Wissenschaft")
|
||||||
|
db.session.add(root)
|
||||||
|
db.session.commit()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error initializing database: {str(e)}")
|
||||||
|
|
||||||
app.run(host="0.0.0.0", debug=True)
|
app.run(host="0.0.0.0", debug=True)
|
||||||
Reference in New Issue
Block a user