You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
853 B
33 lines
853 B
|
|
|
|
|
|
from dj_filer.admin import get_thumbs
|
|
from django.db.models import Avg
|
|
from rest_framework import serializers
|
|
|
|
from apps.library.models import *
|
|
|
|
|
|
class BannerListSerializer(serializers.ModelSerializer):
|
|
description = serializers.CharField(source='summary')
|
|
title = serializers.SerializerMethodField()
|
|
covers = serializers.SerializerMethodField()
|
|
|
|
def get_title(self, obj):
|
|
return obj.title
|
|
|
|
def get_covers(self, obj: BookCollection):
|
|
books = obj.get_books().order_by('-view_count')[:3]
|
|
|
|
images = []
|
|
for book in books:
|
|
url = get_thumbs(book.thumbnail, self.context.get('request'))
|
|
if url.get('md'):
|
|
images.append(url['md'])
|
|
|
|
return images
|
|
|
|
class Meta:
|
|
model = BookCollection
|
|
fields = ('id', 'title', 'summary', 'covers')
|
|
|