Skip to content

Commit 2a3dbd6

Browse files
committed
Do not retry processing when there is no picture
1 parent ca6e0d6 commit 2a3dbd6

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

pictures/tasks.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ def _process_picture(
3333
old = old or []
3434
storage = utils.reconstruct(*storage)
3535
if new:
36-
with storage.open(file_name) as fs:
37-
with Image.open(fs) as img:
38-
for picture in new:
39-
picture = utils.reconstruct(*picture)
40-
picture.save(img)
36+
try:
37+
with storage.open(file_name) as fs:
38+
with Image.open(fs) as img:
39+
for picture in new:
40+
picture = utils.reconstruct(*picture)
41+
picture.save(img)
42+
except FileNotFoundError:
43+
# The file no longer exists (for example, because it was deleted or replaced).
44+
return
4145

4246
for picture in old:
4347
picture = utils.reconstruct(*picture)

tests/test_tasks.py

+15
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,20 @@ def test_process_picture__file_cannot_be_reopened(image_upload_file):
2222
)
2323

2424

25+
@pytest.mark.django_db
26+
def test_process_picture__file_missing(image_upload_file):
27+
obj = SimpleModel.objects.create(picture=image_upload_file)
28+
setattr(
29+
obj.picture.file,
30+
"open",
31+
Mock(side_effect=FileNotFoundError("The file does not exist anymore.")),
32+
)
33+
tasks._process_picture(
34+
obj.picture.storage.deconstruct(),
35+
obj.picture.name,
36+
new=[i.deconstruct() for i in obj.picture.get_picture_files_list()],
37+
)
38+
39+
2540
def test_noop():
2641
tasks.noop() # does nothing

0 commit comments

Comments
 (0)