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 flask_login import current_user, login_required
from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings, Event from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings, Event
from routes.auth import require_password_change from routes.auth import require_password_change
from utils.event_logger import log_event
import os import os
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from sqlalchemy import func, case, literal_column, text from sqlalchemy import func, case, literal_column, text
@@ -273,65 +274,66 @@ def init_routes(main_bp):
logger.debug(f"Profile form submitted with data: {request.form}") logger.debug(f"Profile form submitted with data: {request.form}")
logger.debug(f"Files in request: {request.files}") logger.debug(f"Files in request: {request.files}")
# Handle profile picture removal
if 'remove_picture' in request.form:
logger.debug("Removing profile picture")
if current_user.profile_picture:
# Delete the old profile picture file
old_picture_path = os.path.join(UPLOAD_FOLDER, current_user.profile_picture)
if os.path.exists(old_picture_path):
os.remove(old_picture_path)
current_user.profile_picture = None
db.session.commit()
flash('Profile picture removed successfully!', 'success')
return redirect(url_for('main.profile'))
new_email = request.form.get('email')
logger.debug(f"New email: {new_email}")
# Check if the new email is already used by another user
if new_email != current_user.email:
existing_user = User.query.filter_by(email=new_email).first()
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:
logger.debug(f"Uploading new profile picture: {file.filename}")
filename = secure_filename(file.filename)
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')
current_user.email = new_email
current_user.phone = request.form.get('phone')
current_user.company = request.form.get('company')
current_user.position = request.form.get('position')
current_user.notes = request.form.get('notes')
logger.debug(f"Updated user data: username={current_user.username}, last_name={current_user.last_name}, email={current_user.email}")
# Handle password change if provided
new_password = request.form.get('new_password')
confirm_password = request.form.get('confirm_password')
if new_password:
if not confirm_password:
flash('Please confirm your new password.', 'error')
return render_template('profile/profile.html')
if new_password != confirm_password:
flash('Passwords do not match.', 'error')
return render_template('profile/profile.html')
current_user.set_password(new_password)
flash('Password updated successfully.', 'success')
elif confirm_password:
flash('Please enter a new password.', 'error')
return render_template('profile/profile.html')
try: try:
db.session.commit() # Handle profile picture removal
logger.debug("Profile changes committed to database") if 'remove_picture' in request.form:
# Log profile update event logger.debug("Removing profile picture")
if current_user.profile_picture:
# Delete the old profile picture file
old_picture_path = os.path.join(UPLOAD_FOLDER, current_user.profile_picture)
if os.path.exists(old_picture_path):
os.remove(old_picture_path)
current_user.profile_picture = None
db.session.commit()
flash('Profile picture removed successfully!', 'success')
return redirect(url_for('main.profile'))
new_email = request.form.get('email')
logger.debug(f"New email: {new_email}")
# Check if the new email is already used by another user
if new_email != current_user.email:
existing_user = User.query.filter_by(email=new_email).first()
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:
logger.debug(f"Uploading new profile picture: {file.filename}")
filename = secure_filename(file.filename)
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')
current_user.email = new_email
current_user.phone = request.form.get('phone')
current_user.company = request.form.get('company')
current_user.position = request.form.get('position')
current_user.notes = request.form.get('notes')
logger.debug(f"Updated user data: username={current_user.username}, last_name={current_user.last_name}, email={current_user.email}")
# Handle password change if provided
new_password = request.form.get('new_password')
confirm_password = request.form.get('confirm_password')
if new_password:
if not confirm_password:
flash('Please confirm your new password.', 'error')
return render_template('profile/profile.html')
if new_password != confirm_password:
flash('Passwords do not match.', 'error')
return render_template('profile/profile.html')
current_user.set_password(new_password)
flash('Password updated successfully.', 'success')
elif confirm_password:
flash('Please enter a new password.', 'error')
return render_template('profile/profile.html')
# Create event details
event_details = { event_details = {
'user_id': current_user.id, 'user_id': current_user.id,
'email': current_user.email, 'email': current_user.email,
@@ -357,15 +359,26 @@ def init_routes(main_bp):
'password_changed': bool(new_password) '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) 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') flash('Profile updated successfully!', 'success')
return redirect(url_for('main.dashboard'))
except Exception as e: 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() db.session.rollback()
flash('An error occurred while updating your profile.', 'error') 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') return render_template('profile/profile.html')
@main_bp.route('/starred') @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}") logger.debug(f"Created event object: {event}")
db.session.add(event) db.session.add(event)
db.session.commit() # Don't commit here - let the caller handle the transaction
logger.debug(f"Event saved to database with ID: {event.id}") logger.debug("Event object added to session")
return event return event
except Exception as e: except Exception as e:
logger.error(f"Error creating event: {str(e)}") logger.error(f"Error creating event: {str(e)}")
db.session.rollback()
raise raise
def get_user_events(user_id: int, limit: int = 50) -> List[Event]: def get_user_events(user_id: int, limit: int = 50) -> List[Event]: