File tree Expand file tree Collapse file tree 2 files changed +30
-12
lines changed Expand file tree Collapse file tree 2 files changed +30
-12
lines changed Original file line number Diff line number Diff line change @@ -40,18 +40,22 @@ def process_picture_async(
40
40
field = model ._meta .get_field (field_name )
41
41
storage = construct_storage (* storage_construct )
42
42
43
- with storage .open (file_name ) as file :
44
- with Image .open (file ) as img :
45
- for ratio , sources in PictureFieldFile .get_picture_files (
46
- file_name = file_name ,
47
- img_width = img .width ,
48
- img_height = img .height ,
49
- storage = storage ,
50
- field = field ,
51
- ).items ():
52
- for file_type , srcset in sources .items ():
53
- for width , picture in srcset .items ():
54
- picture .save (img )
43
+ try :
44
+ with storage .open (file_name ) as file :
45
+ with Image .open (file ) as img :
46
+ for ratio , sources in PictureFieldFile .get_picture_files (
47
+ file_name = file_name ,
48
+ img_width = img .width ,
49
+ img_height = img .height ,
50
+ storage = storage ,
51
+ field = field ,
52
+ ).items ():
53
+ for file_type , srcset in sources .items ():
54
+ for width , picture in srcset .items ():
55
+ picture .save (img )
56
+ except FileNotFoundError :
57
+ # The file no longer exists (for example, because it was deleted or replaced).
58
+ return
55
59
56
60
57
61
try :
Original file line number Diff line number Diff line change @@ -16,3 +16,17 @@ def test_process_picture__file_cannot_be_reopened(image_upload_file):
16
16
Mock (side_effect = ValueError ("The file cannot be reopened." )),
17
17
)
18
18
tasks ._process_picture (obj .picture )
19
+
20
+
21
+ @pytest .mark .django_db
22
+ def test_process_picture__file_missing (image_upload_file ):
23
+ # Simulate the case where the file has been removed (or never existed)
24
+ # by making file.open raise FileNotFoundError.
25
+ obj = SimpleModel .objects .create (picture = image_upload_file )
26
+ setattr (
27
+ obj .picture .file ,
28
+ "open" ,
29
+ Mock (side_effect = FileNotFoundError ("File does not exist: test.jpg" )),
30
+ )
31
+ # _process_picture should catch the FileNotFoundError and exit gracefully.
32
+ tasks ._process_picture (obj .picture )
You can’t perform that action at this time.
0 commit comments