X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/5aa13410812d3fc4b35c039bdc3f0a2edc5185a1..746bfb66224e6a1065e5355ed63f9c4836b0a498:/src/api/fields.py?ds=inline diff --git a/src/api/fields.py b/src/api/fields.py index 4bb44dcb9..66fd7d2e9 100644 --- a/src/api/fields.py +++ b/src/api/fields.py @@ -1,5 +1,11 @@ +# -*- 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 class AbsoluteURLField(serializers.ReadOnlyField): @@ -32,3 +38,23 @@ class LegacyMixin(object): if field in value and value[field] is None: value[field] = '' return value + + +class UserPremiumField(serializers.ReadOnlyField): + def __init__(self, *args, **kwargs): + super(UserPremiumField, self).__init__(*args, source='*', **kwargs) + + 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) + )