User Profil Fix Versuch 1
This commit is contained in:
180
app.py
180
app.py
@@ -19,6 +19,7 @@ from openai import OpenAI
|
||||
from dotenv import load_dotenv
|
||||
from flask_socketio import SocketIO, emit
|
||||
from flask_migrate import Migrate
|
||||
import sqlalchemy
|
||||
|
||||
# Modelle importieren
|
||||
from models import (
|
||||
@@ -316,15 +317,21 @@ def register():
|
||||
user = User(username=username, email=email)
|
||||
user.set_password(password)
|
||||
db.session.add(user)
|
||||
db.session.commit() # Commit, um eine ID für den Benutzer zu erhalten
|
||||
|
||||
# Erstelle eine Standard-Mindmap für den neuen Benutzer
|
||||
default_mindmap = UserMindmap(
|
||||
name='Meine Mindmap',
|
||||
description='Meine persönliche Wissenslandschaft',
|
||||
user=user
|
||||
)
|
||||
db.session.add(default_mindmap)
|
||||
db.session.commit()
|
||||
try:
|
||||
default_mindmap = UserMindmap(
|
||||
name='Meine Mindmap',
|
||||
description='Meine persönliche Wissenslandschaft',
|
||||
user_id=user.id
|
||||
)
|
||||
db.session.add(default_mindmap)
|
||||
db.session.commit()
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Erstellen der Standard-Mindmap: {e}")
|
||||
# Stelle sicher, dass wir trotzdem weitermachen können
|
||||
db.session.rollback()
|
||||
|
||||
login_user(user)
|
||||
flash('Dein Konto wurde erfolgreich erstellt!', 'success')
|
||||
@@ -379,49 +386,96 @@ def mindmap():
|
||||
@app.route('/profile')
|
||||
@login_required
|
||||
def profile():
|
||||
# Lade Benutzer-Mindmaps
|
||||
user_mindmaps = UserMindmap.query.filter_by(user_id=current_user.id).all()
|
||||
|
||||
# Lade Statistiken
|
||||
thought_count = Thought.query.filter_by(user_id=current_user.id).count()
|
||||
bookmark_count = db.session.query(user_thought_bookmark).filter(
|
||||
user_thought_bookmark.c.user_id == current_user.id).count()
|
||||
|
||||
# Berechne tatsächliche Werte für Benutzerstatistiken
|
||||
contributions_count = Comment.query.filter_by(user_id=current_user.id).count()
|
||||
|
||||
# Berechne Verbindungen (Anzahl der Gedankenverknüpfungen)
|
||||
connections_count = ThoughtRelation.query.filter(
|
||||
(ThoughtRelation.source_id.in_(
|
||||
db.session.query(Thought.id).filter_by(user_id=current_user.id)
|
||||
)) |
|
||||
(ThoughtRelation.target_id.in_(
|
||||
db.session.query(Thought.id).filter_by(user_id=current_user.id)
|
||||
))
|
||||
).count()
|
||||
|
||||
# Berechne durchschnittliche Bewertung der Gedanken des Benutzers
|
||||
avg_rating = db.session.query(func.avg(ThoughtRating.relevance_score)).join(
|
||||
Thought, Thought.id == ThoughtRating.thought_id
|
||||
).filter(Thought.user_id == current_user.id).scalar() or 0
|
||||
|
||||
# Hole die Anzahl der Follower (falls implementiert)
|
||||
# In diesem Beispiel nehmen wir an, dass es keine Follower-Funktionalität gibt
|
||||
followers_count = 0
|
||||
|
||||
# Hole den Standort des Benutzers aus der Datenbank, falls vorhanden
|
||||
location = "Deutschland" # Standardwert
|
||||
|
||||
return render_template('profile.html',
|
||||
user=current_user,
|
||||
user_mindmaps=user_mindmaps,
|
||||
thought_count=thought_count,
|
||||
bookmark_count=bookmark_count,
|
||||
connections_count=connections_count,
|
||||
contributions_count=contributions_count,
|
||||
followers_count=followers_count,
|
||||
rating=round(avg_rating, 1),
|
||||
location=location)
|
||||
try:
|
||||
# Versuche auf die neue Benutzermodellstruktur zuzugreifen
|
||||
_ = current_user.bio # Dies wird fehlschlagen, wenn die Spalte nicht existiert
|
||||
|
||||
# Wenn keine Ausnahme, fahre mit normalem Profil fort
|
||||
# Lade Benutzer-Mindmaps
|
||||
user_mindmaps = UserMindmap.query.filter_by(user_id=current_user.id).all()
|
||||
|
||||
# Prüfe, ob der Benutzer eine Standard-Mindmap hat, sonst erstelle eine
|
||||
if not user_mindmaps:
|
||||
try:
|
||||
default_mindmap = UserMindmap(
|
||||
name='Meine Mindmap',
|
||||
description='Meine persönliche Wissenslandschaft',
|
||||
user_id=current_user.id
|
||||
)
|
||||
db.session.add(default_mindmap)
|
||||
db.session.commit()
|
||||
|
||||
# Aktualisiere die Liste nach dem Erstellen
|
||||
user_mindmaps = [default_mindmap]
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Erstellen der Standard-Mindmap in Profil: {e}")
|
||||
# Flash-Nachricht für den Benutzer
|
||||
flash('Es gab ein Problem beim Laden deiner Mindmaps. Bitte versuche es später erneut.', 'warning')
|
||||
|
||||
# Lade Statistiken
|
||||
thought_count = Thought.query.filter_by(user_id=current_user.id).count()
|
||||
bookmark_count = db.session.query(user_thought_bookmark).filter(
|
||||
user_thought_bookmark.c.user_id == current_user.id).count()
|
||||
|
||||
# Berechne tatsächliche Werte für Benutzerstatistiken
|
||||
contributions_count = Comment.query.filter_by(user_id=current_user.id).count()
|
||||
|
||||
# Berechne Verbindungen (Anzahl der Gedankenverknüpfungen)
|
||||
connections_count = ThoughtRelation.query.filter(
|
||||
(ThoughtRelation.source_id.in_(
|
||||
db.session.query(Thought.id).filter_by(user_id=current_user.id)
|
||||
)) |
|
||||
(ThoughtRelation.target_id.in_(
|
||||
db.session.query(Thought.id).filter_by(user_id=current_user.id)
|
||||
))
|
||||
).count()
|
||||
|
||||
# Berechne durchschnittliche Bewertung der Gedanken des Benutzers
|
||||
avg_rating = db.session.query(func.avg(ThoughtRating.relevance_score)).join(
|
||||
Thought, Thought.id == ThoughtRating.thought_id
|
||||
).filter(Thought.user_id == current_user.id).scalar() or 0
|
||||
|
||||
# Sammle alle Statistiken in einem Wörterbuch
|
||||
stats = {
|
||||
'thought_count': thought_count,
|
||||
'bookmark_count': bookmark_count,
|
||||
'connections_count': connections_count,
|
||||
'contributions_count': contributions_count,
|
||||
'followers_count': 0, # Platzhalter für zukünftige Funktionalität
|
||||
'rating': round(avg_rating, 1)
|
||||
}
|
||||
|
||||
# Hole die letzten Gedanken des Benutzers
|
||||
thoughts = Thought.query.filter_by(user_id=current_user.id).order_by(Thought.created_at.desc()).limit(5).all()
|
||||
|
||||
# Hole den Standort des Benutzers aus der Datenbank, falls vorhanden
|
||||
location = "Deutschland" # Standardwert
|
||||
|
||||
return render_template('profile.html',
|
||||
user=current_user,
|
||||
user_mindmaps=user_mindmaps,
|
||||
stats=stats,
|
||||
thoughts=thoughts,
|
||||
location=location)
|
||||
|
||||
except (AttributeError, sqlalchemy.exc.OperationalError) as e:
|
||||
# Die Spalte existiert nicht, verwende stattdessen das einfache Profil
|
||||
print(f"Verwende einfaches Profil wegen Datenbankfehler: {e}")
|
||||
flash('Dein Profil wird im einfachen Modus angezeigt, bis die Datenbank aktualisiert wird.', 'warning')
|
||||
|
||||
# Lade nur die grundlegenden Informationen
|
||||
user_mindmaps = UserMindmap.query.filter_by(user_id=current_user.id).all()
|
||||
thoughts = Thought.query.filter_by(user_id=current_user.id).order_by(Thought.created_at.desc()).limit(5).all()
|
||||
|
||||
return render_template('simple_profile.html',
|
||||
user=current_user,
|
||||
user_mindmaps=user_mindmaps,
|
||||
thoughts=thoughts)
|
||||
except Exception as e:
|
||||
# Eine andere Ausnahme ist aufgetreten
|
||||
print(f"Fehler beim Laden des Profils: {e}")
|
||||
flash('Dein Benutzerprofil konnte nicht geladen werden. Bitte kontaktiere den Support.', 'error')
|
||||
return redirect(url_for('index'))
|
||||
|
||||
# Route für Benutzereinstellungen
|
||||
@app.route('/settings', methods=['GET', 'POST'])
|
||||
@@ -1548,4 +1602,28 @@ def redirect_to_index():
|
||||
@app.route('/static/js/mindmap-init.js')
|
||||
def serve_mindmap_init_js():
|
||||
"""Bedient die Mindmap-Initialisierungsdatei."""
|
||||
return app.send_static_file('js/mindmap-init.js'), 200, {'Content-Type': 'application/javascript'}
|
||||
return app.send_static_file('js/mindmap-init.js'), 200, {'Content-Type': 'application/javascript'}
|
||||
|
||||
# Datenbank-Update-Route (admin-geschützt)
|
||||
@app.route('/admin/update-database', methods=['GET', 'POST'])
|
||||
@admin_required
|
||||
def admin_update_database():
|
||||
"""Admin-Route zum Aktualisieren der Datenbank"""
|
||||
message = None
|
||||
success = None
|
||||
|
||||
if request.method == 'POST':
|
||||
try:
|
||||
import update_db
|
||||
update_success = update_db.update_user_table()
|
||||
if update_success:
|
||||
message = "Die Datenbank wurde erfolgreich aktualisiert."
|
||||
success = True
|
||||
else:
|
||||
message = "Es gab ein Problem bei der Aktualisierung der Datenbank."
|
||||
success = False
|
||||
except Exception as e:
|
||||
message = f"Fehler: {str(e)}"
|
||||
success = False
|
||||
|
||||
return render_template('admin/update_database.html', message=message, success=success)
|
||||
Reference in New Issue
Block a user