Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow modifying pep images with same name #1556

Open
wants to merge 2 commits into
base: master
from

Conversation

@lukpueh
Copy link

@lukpueh lukpueh commented Feb 12, 2020

Fixes #824

Currently, peps.converters.add_pep_image does not allow to add an image that already exists in the database for the same name. As a consequence images cannot be updated.

This PR modifies peps.converters.add_pep_image to also update the image db record if the corresponding file has changed as per filecmp.cmp' This requires removing the file prior to the db update, because the django FileField does not override existing files.

Alternatives to the ad hoc removal of the file in add_pep_image include passing a custom OverwriteStorage to the used ImageField, see e.g.:

https://code.djangoproject.com/ticket/4339
https://stackoverflow.com/questions/9522759/imagefield-overwrite-image-file-with-same-name

Currently, 'peps.converters.add_pep_image' does not allow to add an
image that already exists in the database for the same name. As a
consequence images cannot be updated.

This commit modifies 'peps.converters.add_pep_image' to also update
the image db record if the corresponding file has changed as per
'filecmp.cmp'. This requires removing the file prior to the db
update, because the django 'FileField' does not override existing
files.

Alternatives to the ad hoc removal of the file in 'add_pep_image'
include passing a custom 'OverwriteStorage' to the used
'ImageField', see e.g.:

https://code.djangoproject.com/ticket/4339
https://stackoverflow.com/questions/9522759/imagefield-overwrite-image-file-with-same-name

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
@lukpueh
Copy link
Author

@lukpueh lukpueh commented Feb 13, 2020

I just skimmed through the peps git history to see how other image updates were propagated in the past. It generally doesn't seem to have happened often.

One case that I found is pep-0602-example-release-calendar.png. Interestingly, the html img tag on the pep site does point to the latest version of the image, i.e. pep-0602-example-release-calendar_cTnYawu.png. @ambv, @brettcannon, do you know how this is possible? Did I miss something in the peps conversion script? Or did you patch the html or db manually on the production server? Thanks!

@brettcannon
Copy link
Member

@brettcannon brettcannon commented Feb 28, 2020

@lukpueh I'm not aware of any special thing done for that PEP.

@ncoghlan
Copy link

@ncoghlan ncoghlan commented Mar 1, 2020

There was at least one point where we accidentally broke PEP image publication in general, so I was wondering if it could be that the earlier versions of the image didn't make it into the website DB.

However, I don't think the image commit history backs that up.

Test a recent change in `add_pep_image` that allows modifying
images.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
@lukpueh lukpueh force-pushed the lukpueh:allow-modify-pep-image branch from 3c870e9 to bc91a39 Mar 4, 2020
Copy link
Author

@lukpueh lukpueh left a comment

I just pushed a test for my patch. While writing it I found another little issue (see comment inline), which suggests to use my alternative solution (override on upload). What are others' thoughts?

It would be nice to fix this. Pep 458 still references an outdated file, although the repo has the correct one.

Thanks


# We need to remove a prior uploaded file to fix an inconsistency
# between db and filesystem.
# TODO: This should be dealt with in page.model or converters module

This comment has been minimized.

@lukpueh

lukpueh Mar 4, 2020
Author

Here's some additional info for this line;

A prior test that calls add_pep_image might have already created the 'pep-3001-1.png' file, without persisting the db object. Now a new call to add_pep_image will check for objects, whose name ends with 'pep-3001-1.png', and, since it does not find any, create it anew along with the file. But since Django does not allow to override files it duplicates it using a random infix, e.g 'pep-3001-1_0u7tlLY.png', which is also used as a name on the object.

Now due to the endswith-check and with the random infix, each subsequent attempt to add 'pep-3001-1.png' with add_pep_image, will also duplicate it the object and file.

Maybe we should just override images on upload as suggested as alternative fix in the PR description?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants
You can’t perform that action at this time.