File tree Expand file tree Collapse file tree 3 files changed +24
-3
lines changed Expand file tree Collapse file tree 3 files changed +24
-3
lines changed Original file line number Diff line number Diff line change 11
11
from django .core .files .storage import Storage
12
12
from django .db .models import ImageField
13
13
from django .db .models .fields .files import ImageFieldFile
14
+ from django .urls import reverse
14
15
from PIL import Image , ImageOps
15
16
16
17
__all__ = ["PictureField" , "PictureFieldFile" ]
17
18
19
+
18
20
from pictures import conf , utils
19
21
20
22
@@ -33,6 +35,18 @@ def __post_init__(self):
33
35
34
36
@property
35
37
def url (self ) -> str :
38
+ if conf .get_settings ().USE_PLACEHOLDERS :
39
+ return reverse (
40
+ "pictures:placeholder" ,
41
+ kwargs = {
42
+ "alt" : Path (self .parent_name ).stem ,
43
+ "width" : self .width ,
44
+ "ratio" : f"{ self .aspect_ratio .numerator } x{ self .aspect_ratio .denominator } "
45
+ if self .aspect_ratio
46
+ else None ,
47
+ "file_type" : self .file_type ,
48
+ },
49
+ )
36
50
return self .storage .url (self .name )
37
51
38
52
@property
Original file line number Diff line number Diff line change @@ -18,7 +18,8 @@ class Meta:
18
18
fields = ["picture" ]
19
19
20
20
21
- def test_default ():
21
+ def test_default (settings ):
22
+ settings .PICTURES ["USE_PLACEHOLDERS" ] = False
22
23
assert (
23
24
rest_framework .default (
24
25
obj = SimplePicture (
@@ -41,7 +42,8 @@ def test_default__type_error():
41
42
42
43
class TestPictureField :
43
44
@pytest .mark .django_db
44
- def test_to_representation (self , image_upload_file ):
45
+ def test_to_representation (self , image_upload_file , settings ):
46
+ settings .PICTURES ["USE_PLACEHOLDERS" ] = False
45
47
46
48
profile = models .Profile .objects .create (picture = image_upload_file )
47
49
serializer = ProfileSerializer (profile )
Original file line number Diff line number Diff line change @@ -37,12 +37,17 @@ class TestSimplePicture:
37
37
width = 800 ,
38
38
)
39
39
40
- def test_url (self ):
40
+ def test_url (self , settings ):
41
+ settings .PICTURES ["USE_PLACEHOLDERS" ] = False
41
42
assert (
42
43
self .picture_with_ratio .url
43
44
== "/media/testapp/simplemodel/image/4_3/800w.webp"
44
45
)
45
46
47
+ def test_url__placeholder (self , settings ):
48
+ settings .PICTURES ["USE_PLACEHOLDERS" ] = True
49
+ assert self .picture_with_ratio .url == "/_pictures/image/4x3/800w.WEBP"
50
+
46
51
def test_height (self ):
47
52
assert self .picture_with_ratio .height == 600
48
53
assert not self .picture_without_ratio .height
You can’t perform that action at this time.
0 commit comments