Skip to content

Fix: Add duplicate name check in rename_image to prevent 500 errors#142

Merged
maubreville merged 2 commits intomasterfrom
copilot/fix-d2f3181c-8f9c-416e-858a-25e7c493426b
Sep 26, 2025
Merged

Fix: Add duplicate name check in rename_image to prevent 500 errors#142
maubreville merged 2 commits intomasterfrom
copilot/fix-d2f3181c-8f9c-416e-858a-25e7c493426b

Conversation

Copy link
Contributor

Copilot AI commented Sep 26, 2025

Problem

When renaming an image in EXACT, if the new name already exists in the imageset, the system would attempt to perform os.rename() operations without checking for duplicates first. This causes a 500 Internal Server Error because the filesystem operation fails when trying to rename to an existing filename.

Steps to Reproduce

  1. Upload two images to an imageset: "image1.jpg" and "image2.jpg"
  2. Attempt to rename "image1.jpg" to "image2.jpg"
  3. System tries to execute: os.rename("path/image1.jpg", "path/image2.jpg")
  4. Operating system fails because "image2.jpg" already exists
  5. Django returns a 500 Internal Server Error

Solution

Added duplicate checking logic in images/views.py:rename_image() that queries the database for existing images with the same name or filename before attempting any file operations:

# Check if an image with the new name already exists in the imageset
existing_image = Image.objects.filter(Q(name=newName)|Q(filename=newstem+filenameext), 
                                    image_set=imageset).exclude(id=image.id).first()
if existing_image is not None:
    # Image with this name already exists, redirect without changes
    return redirect(reverse('images:view_imageset', args=(imageset_id,)))

Key Features

  • Graceful handling: Instead of crashing with a 500 error, the function now gracefully redirects back to the imageset view without making changes
  • Comprehensive checking: Checks both name and filename fields to catch all potential conflicts
  • Self-exclusion: Uses .exclude(id=image.id) to allow renaming to the same name (no-op scenario)
  • Consistent pattern: Follows the same Q(name=...|Q(filename=...) query pattern used elsewhere in the codebase
  • Early exit: Prevents any file system operations when duplicates are detected

Testing

The fix handles all edge cases correctly:

  • ✅ Renaming to unique name: succeeds as before
  • ✅ Renaming to existing name: fails gracefully (no 500 error)
  • ✅ Renaming to same name: succeeds (no-op)
  • ✅ Permission checks: maintains existing access control

This is a minimal, surgical fix that resolves the 500 error issue while maintaining backward compatibility and all existing functionality.

Original prompt

This section details on the original issue you should resolve

<issue_title>Bug: Renaming does not check if filename previously existed already</issue_title>
<issue_description>The title says it all. When renaming an image, we don't check if the filename already exists.
This directly results in a 500 error.

Should be easy to fix, by changing the appropriate lines in images/views.py:rename_image.</issue_description>

<agent_instructions>Create a fix for images/views.py to check all images in the current imageset for the existence of an image of same filename before renaming.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #141

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: maubreville <10051592+maubreville@users.noreply.github.com>
Copilot AI changed the title [WIP] Bug: Renaming does not check if filename previously existed already Fix: Add duplicate name check in rename_image to prevent 500 errors Sep 26, 2025
Copilot AI requested a review from maubreville September 26, 2025 05:58
@maubreville
Copy link
Collaborator

Tested successfully.

@maubreville maubreville marked this pull request as ready for review September 26, 2025 06:57
@maubreville maubreville merged commit 6861417 into master Sep 26, 2025
1 check passed
@maubreville maubreville deleted the copilot/fix-d2f3181c-8f9c-416e-858a-25e7c493426b branch September 26, 2025 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Renaming does not check if filename previously existed already

2 participants