user update logs

This commit is contained in:
2025-05-29 23:02:30 +02:00
parent 37fcc5f34c
commit 986db28494
4 changed files with 77 additions and 65 deletions

View File

@@ -2,6 +2,7 @@ from flask import render_template, Blueprint, redirect, url_for, request, flash,
from flask_login import current_user, login_required
from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings, Event
from routes.auth import require_password_change
from utils.event_logger import log_event
import os
from werkzeug.utils import secure_filename
from sqlalchemy import func, case, literal_column, text
@@ -273,6 +274,7 @@ def init_routes(main_bp):
logger.debug(f"Profile form submitted with data: {request.form}")
logger.debug(f"Files in request: {request.files}")
try:
# Handle profile picture removal
if 'remove_picture' in request.form:
logger.debug("Removing profile picture")
@@ -294,6 +296,7 @@ def init_routes(main_bp):
if existing_user:
flash('A user with this email already exists.', 'error')
return render_template('profile/profile.html')
# Handle profile picture upload
file = request.files.get('profile_picture')
if file and file.filename:
@@ -302,6 +305,7 @@ def init_routes(main_bp):
file_path = os.path.join(UPLOAD_FOLDER, filename)
file.save(file_path)
current_user.profile_picture = filename
# Update user information
current_user.username = request.form.get('first_name')
current_user.last_name = request.form.get('last_name')
@@ -328,10 +332,8 @@ def init_routes(main_bp):
elif confirm_password:
flash('Please enter a new password.', 'error')
return render_template('profile/profile.html')
try:
db.session.commit()
logger.debug("Profile changes committed to database")
# Log profile update event
# Create event details
event_details = {
'user_id': current_user.id,
'email': current_user.email,
@@ -357,15 +359,26 @@ def init_routes(main_bp):
'password_changed': bool(new_password)
}
}
logger.debug(f"Creating profile update event with details: {event_details}")
logger.debug(f"Preparing to create profile update event with details: {event_details}")
# Create the event
event = log_event('user_update', event_details, current_user.id)
logger.debug(f"Event created successfully with ID: {event.id}")
logger.debug("Event object created and added to session")
# Commit all changes
db.session.commit()
logger.debug("Profile changes and event committed to database successfully")
flash('Profile updated successfully!', 'success')
return redirect(url_for('main.dashboard'))
except Exception as e:
logger.error(f"Error updating profile or logging event: {str(e)}")
logger.error(f"Error updating profile: {str(e)}")
logger.error(f"Full error details: {str(e.__class__.__name__)}: {str(e)}")
db.session.rollback()
flash('An error occurred while updating your profile.', 'error')
return redirect(url_for('main.dashboard'))
return render_template('profile/profile.html')
return render_template('profile/profile.html')
@main_bp.route('/starred')

View File

@@ -39,12 +39,11 @@ def log_event(event_type: str, details: Optional[Dict[str, Any]] = None, user_id
logger.debug(f"Created event object: {event}")
db.session.add(event)
db.session.commit()
logger.debug(f"Event saved to database with ID: {event.id}")
# Don't commit here - let the caller handle the transaction
logger.debug("Event object added to session")
return event
except Exception as e:
logger.error(f"Error creating event: {str(e)}")
db.session.rollback()
raise
def get_user_events(user_id: int, limit: int = 50) -> List[Event]: