Files
website/website/init_db.py

130 lines
4.4 KiB
Python

from app import app, db, User, Thought, Comment, MindMapNode, ThoughtRelation, ThoughtRating, RelationType
import os
def init_database():
with app.app_context():
# Datenbank löschen und neu erstellen
if os.path.exists('instance/mindmap.db'):
os.remove('instance/mindmap.db')
db.create_all()
# Admin-Benutzer erstellen
admin = User(username='admin', email='admin@example.com', is_admin=True)
admin.set_password('admin')
db.session.add(admin)
# Beispiel-Benutzer erstellen
user = User(username='user', email='user@example.com')
user.set_password('user')
db.session.add(user)
# Commit um IDs zu generieren
db.session.commit()
# Grundlegende Mindmap-Struktur erstellen
root = MindMapNode(name='Wissen')
db.session.add(root)
# Hauptzweige erstellen
branches = [
'Philosophie',
'Wissenschaft',
'Technologie',
'Kunst',
'Geschichte'
]
branch_nodes = {}
for branch in branches:
node = MindMapNode(name=branch, parent=root)
branch_nodes[branch] = node
db.session.add(node)
# Commit um IDs zu generieren
db.session.commit()
# Beispiel-Gedanken erstellen
thoughts = [
{
'title': 'Künstliche Intelligenz und Bewusstsein',
'content': 'Die Frage nach maschinellem Bewusstsein ist fundamental für die KI-Ethik.',
'abstract': 'Eine Untersuchung der philosophischen Implikationen von KI-Bewusstsein.',
'keywords': 'KI, Bewusstsein, Ethik, Philosophie',
'branch': 'Philosophie',
'color_code': '#FF5733',
'source_type': 'Markdown'
},
{
'title': 'Quantenmechanik und Realität',
'content': 'Die Kopenhagener Deutung und ihre Auswirkungen auf unser Verständnis der Realität.',
'abstract': 'Eine Analyse verschiedener Interpretationen der Quantenmechanik.',
'keywords': 'Quantenmechanik, Physik, Realität',
'branch': 'Wissenschaft',
'color_code': '#33FF57',
'source_type': 'PDF'
}
]
thought_objects = []
for t in thoughts:
thought = Thought(
title=t['title'],
content=t['content'],
abstract=t['abstract'],
keywords=t['keywords'],
branch=t['branch'],
color_code=t['color_code'],
source_type=t['source_type'],
user_id=user.id # Hier wird die user_id gesetzt
)
branch_node = branch_nodes[t['branch']]
branch_node.thoughts.append(thought)
thought_objects.append(thought)
db.session.add(thought)
# Commit um IDs zu generieren
db.session.commit()
# Beispiel-Relation erstellen
relation = ThoughtRelation(
source_id=thought_objects[0].id,
target_id=thought_objects[1].id,
relation_type=RelationType.INSPIRES,
created_by_id=user.id
)
db.session.add(relation)
# Beispiel-Bewertung erstellen
rating = ThoughtRating(
thought_id=thought_objects[0].id,
user_id=admin.id,
relevance_score=5
)
db.session.add(rating)
# Beispiel-Kommentare erstellen
for thought in thought_objects:
comment = Comment(
content=f'Interessante Perspektive zu {thought.title}!',
thought_id=thought.id,
user_id=admin.id
)
db.session.add(comment)
# Finale Commit
db.session.commit()
print("Datenbank wurde erfolgreich initialisiert!")
def init_db():
# Alias für die Kompatibilität mit älteren Scripts
init_database()
if __name__ == '__main__':
init_database()
print("Datenbank wurde erfolgreich initialisiert!")
print("Sie können die Anwendung jetzt mit 'python app.py' starten")
print("Anmelden mit:")
print(" Admin: username=admin, password=admin")
print(" User: username=user, password=user")