Story #2638 :: Missing profile image file causes a 500 on every page that renders an avatar - #2639
Draft
herzog0 wants to merge 3 commits into
Draft
Story #2638 :: Missing profile image file causes a 500 on every page that renders an avatar#2639herzog0 wants to merge 3 commits into
herzog0 wants to merge 3 commits into
Conversation
5 tasks
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue: #[number]
Summary & Context
A user whose
profile_imagerow names a file that is not present in storagecaused a 500 on every page that rendered that user's avatar. This makes
User.get_thumbnail_url()degrade toNone, soget_avatar_url()returns theempty string and the template renders the coloured initials circle instead.
get_hq_image_url()had the identical defect against thehq_imagefield and isfixed the same way, since the avatar template tag calls both methods on the same
render paths and either one raising is enough to take the page down.
any library page, any user profile page
Changes
users/models.py- widened the error handling inget_thumbnail_url()andget_hq_image_url()so a missing image file is caught instead of escaping. Bothnow return
Noneand the avatar falls back to the initials circle.users/tests/test_models.py- five tests covering both methods, with the filepresent and missing. First test coverage for either.
Please list any potential risks or areas that need extra attention during review/testing
suppressnow also coversAttributeErrorandOSErrorraised while evaluatingself.profile_image/self.hq_image, not just the render's URL access. A genuinely broken storagebackend will now yield a fallback avatar silently rather than erroring. That is
the intent for the avatar case, but it does mean storage problems get quieter
here.
get_hq_image_url()degrading toNonechanges which image renders, notjust the status code.
avatar_tags.pycomputesuse_hq_image = use_user_hq_image and hq_image, so a caller that asked for thehigh-quality avatar now silently falls back to the standard thumbnail when the
hq_imagefile is missing. That is the desired outcome, but it is a visualchange on those call sites rather than a pure error fix.
Screenshots
Peer-review testing steps
Automated
The three missing-file tests fail with
FileNotFoundErroragainst the unfixedmethods and pass with the fix applied. Verified in both directions, one method at
a time: reverting
get_thumbnail_url()alone fails exactlytest_get_thumbnail_url_returns_none_when_file_missingandtest_get_avatar_url_falls_back_when_file_missing; revertingget_hq_image_url()alone fails exactlytest_get_hq_image_url_returns_none_when_file_missing. In every case thetraceback is the one from the issue, raised out of
imagekit/specs/__init__.py generate().users/tests/test_avatar_tags.pyalso passes unchanged, which covers thetemplate tag that calls both methods.
Note for anyone running the suite in a one-off container rather than the running
webservice:DEBUG_TOOLBARmust be off, otherwisedebug_toolbar.appscallsMIGRATION_MODULES.setdefault()on theDisableMigrationsstub inconfig/test_settings.pyand Django fails to boot.Manual, page level
The setup requires a local database change, which is reverted at the end.
Find a user whose avatar the homepage renders and note their id as
AUTHOR_ID:Record the current value so it can be restored:
Point the row at a file that does not exist:
On
develop, confirm the failure. Method level raisesFileNotFoundError: [Errno 2] No such file or directory: '/code/media/profile-images/does-not-exist.png'; page level returns500:On this branch, repeat step 4. Method level prints
Nonewith no exception;page level returns
200, and the affected author renders the colouredinitials circle instead of an image. Check a library page and that user's
profile page too.
Restore the original value and re-run the method-level check. It should
return the thumbnail URL again, confirming the happy path is untouched:
Repeat steps 2 to 6 against
hq_imageinstead ofprofile_image, on a userwho has one. Only a page that renders the avatar with
use_user_hq_imageexercises this path, so it is easiest to confirm at the method level:
Raises on
develop, printsNoneon this branch. Restore the recorded valueafterwards.
Self-review Checklist
Backend
pre-commit run --files users/models.py users/tests/test_models.py)