X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/540e76dd8e04d7bab431da696565e826788bd218..59947fddf28d67c6822267c8a0fc28e13a6e9175:/src/api/fields.py?ds=sidebyside diff --git a/src/api/fields.py b/src/api/fields.py index 1ce83cc3e..728323473 100644 --- a/src/api/fields.py +++ b/src/api/fields.py @@ -1,6 +1,10 @@ +# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Wolne Lektury. See NOTICE for more information. +# from rest_framework import serializers -from django.core.urlresolvers import reverse -from paypal.rest import user_is_subscribed +from sorl.thumbnail import default +from rest_framework.reverse import reverse +from club.models import Membership class AbsoluteURLField(serializers.ReadOnlyField): @@ -13,19 +17,20 @@ 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): + request = self.context['request'] if self.view_name is not None: kwargs = { arg: getattr(value, field) for (arg, field) in self.view_args.items() } - value = reverse(self.view_name, kwargs=kwargs) - return self.context['request'].build_absolute_uri(value) + return reverse(self.view_name, kwargs=kwargs, request=request) + return 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', []) @@ -40,4 +45,16 @@ class UserPremiumField(serializers.ReadOnlyField): super(UserPremiumField, self).__init__(*args, source='*', **kwargs) def to_representation(self, value): - return user_is_subscribed(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) + )