X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/5aa13410812d3fc4b35c039bdc3f0a2edc5185a1..7010b5cb6b4e4380d9e23add9c7c98431b6e3f95:/src/api/fields.py diff --git a/src/api/fields.py b/src/api/fields.py index 4bb44dcb9..d1f9da4bf 100644 --- a/src/api/fields.py +++ b/src/api/fields.py @@ -1,5 +1,10 @@ +# 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 django.core.urlresolvers import reverse +from sorl.thumbnail import default +from django.urls import reverse +from club.models import Membership class AbsoluteURLField(serializers.ReadOnlyField): @@ -12,7 +17,7 @@ class AbsoluteURLField(serializers.ReadOnlyField): if view_args: for v in view_args: fields = v.split(':', 1) - self.view_args[fields[0]] = fields[1] if len(fields)>1 else fields[0] + self.view_args[fields[0]] = fields[1] if len(fields) > 1 else fields[0] def to_representation(self, value): if self.view_name is not None: @@ -24,7 +29,7 @@ class AbsoluteURLField(serializers.ReadOnlyField): return self.context['request'].build_absolute_uri(value) -class LegacyMixin(object): +class LegacyMixin: def to_representation(self, value): value = super(LegacyMixin, self).to_representation(value) non_null_fields = getattr(getattr(self, 'Meta', None), 'legacy_non_null_fields', []) @@ -32,3 +37,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 Membership.is_active_for(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) + )