+class PartnerAudiobookSerializer(BookSerializer2):
+ price = serializers.SerializerMethodField()
+ duration = serializers.SerializerMethodField()
+ files = serializers.SerializerMethodField()
+
+ class Meta:
+ model = Book
+ fields = [
+ 'slug', 'title',
+ 'href', 'url', 'language',
+ 'authors', 'translators',
+ 'epochs', 'genres', 'kinds',
+ 'files',
+ 'cover',
+ 'isbn_mp3',
+ 'abstract',
+ 'content_warnings', 'audiences',
+ 'changed_at', 'duration',
+ 'price',
+ ]
+
+ def get_duration(self, obj):
+ return obj.get_audiobooks(True)[2]
+
+ def get_files(self, obj):
+ def get_for_single(b):
+ fs = []
+ for m in b.media.filter(type='mp3'):
+ fs.append({
+ "name": m.name,
+ "part_name": m.part_name,
+ "url": 'https://wolnelektury.pl' + m.file.url,
+ })
+ for c in b.get_children():
+ fs.extend(get_for_single(c))
+ return fs
+ return get_for_single(obj)
+
+ def get_price(self, obj):
+ duration = obj.get_audiobooks(True, True)[2]
+ if not duration:
+ return None
+ duration /= 60
+ return self.context['partner'].get_audio_price(obj.pages)
+
+