Fix User DoesNotExist error in backup_cancel by using created_by_id

🐛 Root Cause Found & Fixed:
- Error occurred when accessing backup_job.created_by where the referenced user was deleted
- Django was trying to fetch a User object that no longer exists
- Changed to use backup_job.created_by_id instead of backup_job.created_by
- This avoids the foreign key lookup that was causing the 'User matching query does not exist' error

 Backup cancellation now works even when:
- Original creator user has been deleted from the database
- Foreign key relationship is broken but ID is still stored

The backup job can now be cancelled without triggering user lookup errors.
This commit is contained in:
2025-09-29 22:14:06 +02:00
parent c46c3543b1
commit 92b689f5e7

View File

@@ -6019,7 +6019,9 @@ def backup_cancel(request, backup_id):
print(f"DEBUG: Attempting to cancel backup job {backup_id}")
backup_job = BackupJob.objects.get(id=backup_id)
print(f"DEBUG: Found backup job - ID: {backup_job.id}, Status: {backup_job.status}")
print(f"DEBUG: Created by: {backup_job.created_by}, Current user: {request.user}")
# Use created_by_id instead of created_by to avoid triggering the foreign key lookup
print(f"DEBUG: Created by ID: {backup_job.created_by_id}, Current user ID: {request.user.id}")
# Only allow cancelling running or pending jobs
if backup_job.status not in ['running', 'pending']:
@@ -6027,9 +6029,9 @@ def backup_cancel(request, backup_id):
return redirect("stiftung:backup_management")
# Check if user has permission to cancel (either own job or admin)
# Handle case where created_by might be None (user deleted)
print(f"DEBUG: Checking permissions - created_by: {backup_job.created_by}, is_staff: {request.user.is_staff}")
if backup_job.created_by is not None and backup_job.created_by != request.user and not request.user.is_staff:
# Use created_by_id to avoid database lookup for potentially non-existent user
print(f"DEBUG: Checking permissions - created_by_id: {backup_job.created_by_id}, is_staff: {request.user.is_staff}")
if backup_job.created_by_id is not None and backup_job.created_by_id != request.user.id and not request.user.is_staff:
messages.error(request, "Sie können nur Ihre eigenen Backup-Jobs abbrechen.")
return redirect("stiftung:backup_management")