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.
57 lines
1.5 KiB
57 lines
1.5 KiB
|
|
|
|
|
|
from dj_filer.admin import get_thumbs
|
|
from django.db.models import Avg, Q
|
|
from rest_framework import serializers
|
|
|
|
from apps.library.models import *
|
|
|
|
|
|
|
|
|
|
class CategorySerializer(serializers.ModelSerializer):
|
|
books_count = serializers.IntegerField(read_only=True)
|
|
|
|
class Meta:
|
|
model = Category
|
|
fields = ('id', 'title', 'slug', 'status', 'books_count', 'created_at', 'updated_at')
|
|
|
|
|
|
class PinnedBookCollectionSerializer(serializers.ModelSerializer):
|
|
covers = serializers.SerializerMethodField()
|
|
|
|
def get_covers(self, obj: BookCollection):
|
|
books = obj.books.all().order_by('-view_count')[:3]
|
|
|
|
images = []
|
|
for book in books:
|
|
if book.thumbnail:
|
|
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', 'covers')
|
|
|
|
|
|
class BookSerializer(serializers.ModelSerializer):
|
|
thumbnail = serializers.SerializerMethodField()
|
|
|
|
def get_thumbnail(self, obj):
|
|
if obj.thumbnail:
|
|
return get_thumbs(obj.thumbnail, self.context.get('request'))
|
|
return None
|
|
|
|
class Meta:
|
|
model = Book
|
|
fields = (
|
|
'id', 'title', 'slug', 'summary', 'description', 'thumbnail',
|
|
'author', 'status', 'pin', 'view_count', 'download_count',
|
|
'file_type', 'book_file', 'created_at'
|
|
)
|
|
|
|
|