Skip to content

Commit fa602ec

Browse files
author
Lukas Puehringer
committed
Allow modifying pep images 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>
1 parent 6902c19 commit fa602ec

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

peps/converters.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
import re
44
import os
5+
import filecmp
56

67
from bs4 import BeautifulSoup
78

@@ -221,8 +222,15 @@ def add_pep_image(artifact_path, pep_number, path):
221222
FOUND = True
222223
break
223224

224-
if not FOUND:
225-
image = Image(page=page)
225+
# Add image if it does not exist or the new image differs from the existing
226+
if not FOUND or not filecmp.cmp(image_path, image.image.path):
227+
# If it does not exist we need a new image object ...
228+
if not FOUND:
229+
image = Image(page=page)
230+
# ... otherwise we re-use the existing object from the loop above but
231+
# first remove the image file (django does not override)
232+
else:
233+
os.remove(image.image.path)
226234

227235
with open(image_path, 'rb') as image_obj:
228236
image.image.save(path, File(image_obj))

0 commit comments

Comments
 (0)