fixed some issues with profile and events

This commit is contained in:
2025-05-29 22:40:59 +02:00
parent 8f24e21d5d
commit 37fcc5f34c
7 changed files with 79 additions and 18 deletions

View File

@@ -270,8 +270,12 @@ def init_routes(main_bp):
@require_password_change
def profile():
if request.method == 'POST':
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)
@@ -283,6 +287,7 @@ def init_routes(main_bp):
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()
@@ -292,6 +297,7 @@ def init_routes(main_bp):
# 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)
@@ -304,22 +310,62 @@ def init_routes(main_bp):
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
event_details = {
'user_id': current_user.id,
'email': current_user.email,
'update_type': 'profile_update',
'updated_fields': {
'username': current_user.username,
'last_name': current_user.last_name,
'email': current_user.email,
'phone': current_user.phone,
'company': current_user.company,
'position': current_user.position,
'notes': current_user.notes,
'profile_picture': bool(current_user.profile_picture)
},
'changes': {
'username': request.form.get('first_name'),
'last_name': request.form.get('last_name'),
'email': request.form.get('email'),
'phone': request.form.get('phone'),
'company': request.form.get('company'),
'position': request.form.get('position'),
'notes': request.form.get('notes'),
'password_changed': bool(new_password)
}
}
logger.debug(f"Creating profile update event with details: {event_details}")
event = log_event('user_update', event_details, current_user.id)
logger.debug(f"Event created successfully with ID: {event.id}")
flash('Profile updated successfully!', 'success')
except Exception as e:
logger.error(f"Error updating profile or logging event: {str(e)}")
db.session.rollback()
flash('An error occurred while updating your profile.', 'error')
return redirect(url_for('main.profile'))
return redirect(url_for('main.dashboard'))
return render_template('profile/profile.html')
@main_bp.route('/starred')