diff --git a/routes/__pycache__/main.cpython-313.pyc b/routes/__pycache__/main.cpython-313.pyc index 27887f2..78e2e9f 100644 Binary files a/routes/__pycache__/main.cpython-313.pyc and b/routes/__pycache__/main.cpython-313.pyc differ diff --git a/routes/main.py b/routes/main.py index 47fa755..e63c796 100644 --- a/routes/main.py +++ b/routes/main.py @@ -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,65 +274,66 @@ def init_routes(main_bp): logger.debug(f"Profile form submitted with data: {request.form}") 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: - db.session.commit() - logger.debug("Profile changes committed to database") - # Log profile update event + # 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') + + # 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') diff --git a/utils/__pycache__/event_logger.cpython-313.pyc b/utils/__pycache__/event_logger.cpython-313.pyc index 77fedbc..d89842b 100644 Binary files a/utils/__pycache__/event_logger.cpython-313.pyc and b/utils/__pycache__/event_logger.cpython-313.pyc differ diff --git a/utils/event_logger.py b/utils/event_logger.py index 87fda6a..6033864 100644 --- a/utils/event_logger.py +++ b/utils/event_logger.py @@ -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]: