X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/540e76dd8e04d7bab431da696565e826788bd218..b0b9c1f0f9f4cc0f5c4d00d913735ab6a81bf214:/src/api/fields.py diff --git a/src/api/fields.py b/src/api/fields.py index 1ce83cc3e..66fd7d2e9 100644 --- a/src/api/fields.py +++ b/src/api/fields.py @@ -1,4 +1,9 @@ +# -*- coding: utf-8 -*- +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# from rest_framework import serializers +from sorl.thumbnail import default from django.core.urlresolvers import reverse from paypal.rest import user_is_subscribed @@ -41,3 +46,15 @@ class UserPremiumField(serializers.ReadOnlyField): def to_representation(self, value): return user_is_subscribed(value) + + +class ThumbnailField(serializers.FileField): + def __init__(self, geometry, *args, **kwargs): + self.geometry = geometry + super(ThumbnailField, self).__init__(*args, **kwargs) + + def to_representation(self, value): + if value: + return super(ThumbnailField, self).to_representation( + default.backend.get_thumbnail(value, self.geometry) + )