from sorl thumbnail shortcuts import get_thumbnail class Matter Projec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from sorl.thumbnail.shortcuts import get_thumbnail
class Matter(ProjectReferenceNumberMixin, RedirectMixin, TimeStampedModel):
image = ImageField(upload_to='matters', null=True, blank=True)
class MatterSerializer(RefNoSerializerMixin, ReprSerializerMixin, serializers.ModelSerializer):
class Meta:
model = Matter
fields = ('thumbnail_url', 'image_url')
thumbnail_url = serializers.SerializerMethodField('get_thumbnail_url')
image_url = serializers.SerializerMethodField('get_thumbnail_url')
def get_thumbnail_url(self, obj):
if obj.image:
return get_thumbnail(obj.image, "144x104", crop='center').url
else:
return urljoin(settings.STATIC_URL, 'img/placeholder-144x104.jpg')
def get_image_url(self, obj):
if obj.image:
return get_thumbnail(obj.image, "1440x1040").url
else:
return urljoin(settings.STATIC_URL, 'img/placeholder-1440x1040.jpg')