From 33888a3b18c90bd117e030285edd6ba69b239606 Mon Sep 17 00:00:00 2001 From: Jan Remmer Siebels Date: Mon, 29 Sep 2025 20:40:51 +0200 Subject: [PATCH] Fix backup cancellation user reference issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 Enhanced Error Handling: - Fixed 'User matching query does not exist' error in backup_cancel - Handle case where backup_job.created_by is None (deleted user) - Added error handling for audit logging to prevent cancellation failure - Improved permission check to handle null created_by references ✅ Backup Cancellation Robustness: - Now works even if the user who created the backup was deleted - Audit logging failure won't prevent successful cancellation - Better error isolation and reporting The error occurred because the system tried to compare a null user reference or failed during audit logging when user records were inconsistent. --- app/stiftung/views.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app/stiftung/views.py b/app/stiftung/views.py index f0ebd3c..6b13d88 100644 --- a/app/stiftung/views.py +++ b/app/stiftung/views.py @@ -6023,7 +6023,8 @@ def backup_cancel(request, backup_id): return redirect("stiftung:backup_management") # Check if user has permission to cancel (either own job or admin) - if backup_job.created_by != request.user and not request.user.is_staff: + # Handle case where created_by might be None (user deleted) + if backup_job.created_by is not None and backup_job.created_by != request.user and not request.user.is_staff: messages.error(request, "Sie können nur Ihre eigenen Backup-Jobs abbrechen.") return redirect("stiftung:backup_management") @@ -6034,14 +6035,18 @@ def backup_cancel(request, backup_id): backup_job.error_message = f"Abgebrochen von {request.user.username}" backup_job.save() - # Log the cancellation - from stiftung.audit import log_system_action - log_system_action( - request=request, - action="backup_cancel", - description=f"Backup-Job abgebrochen: {backup_job.get_backup_type_display()}", - details={"backup_job_id": str(backup_job.id)}, - ) + # Log the cancellation (with error handling) + try: + from stiftung.audit import log_system_action + log_system_action( + request=request, + action="backup_cancel", + description=f"Backup-Job abgebrochen: {backup_job.get_backup_type_display()}", + details={"backup_job_id": str(backup_job.id)}, + ) + except Exception as audit_error: + # Don't fail the cancellation if logging fails + print(f"Warning: Could not log backup cancellation: {audit_error}") messages.success(request, f"Backup-Job wurde abgebrochen.")