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.
131 lines
4.1 KiB
131 lines
4.1 KiB
|
|
|
|
|
|
from utils import get_thumbs
|
|
from django.db.models import Avg, Q
|
|
from rest_framework import serializers
|
|
|
|
from apps.library.models import *
|
|
from apps.bookmark.serializers import *
|
|
|
|
|
|
|
|
|
|
class CategorySerializer(serializers.ModelSerializer):
|
|
books_count = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Category
|
|
fields = ('id', 'title', 'slug', 'status', 'books_count', 'created_at', 'updated_at')
|
|
|
|
def get_books_count(self, obj):
|
|
# Use the annotation if available, otherwise fall back to the property
|
|
if hasattr(obj, 'books_count_annotation'):
|
|
return obj.books_count_annotation
|
|
return obj.books_count
|
|
|
|
|
|
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', 'summary', 'covers')
|
|
|
|
|
|
class BookSerializer(serializers.ModelSerializer):
|
|
thumbnail = serializers.SerializerMethodField()
|
|
bookmark = 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', 'bookmark'
|
|
)
|
|
|
|
def get_bookmark(self, obj):
|
|
"""
|
|
Get bookmark information for this book.
|
|
"""
|
|
# Get the current user from the request context
|
|
request = self.context.get('request')
|
|
user = request.user if request else None
|
|
book_mark = BookmarkStatusSerializer.get_bookmark_info(
|
|
obj=obj,
|
|
user=user,
|
|
service='library'
|
|
)
|
|
return book_mark.get('is_bookmarked', False)
|
|
|
|
|
|
|
|
class MiddleBookCollectionSerializer(serializers.ModelSerializer):
|
|
"""Serializer for Middle Book Collections with their books"""
|
|
books = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = BookCollection
|
|
fields = ('id', 'title', 'slug', 'summary', 'status', 'order', 'books')
|
|
|
|
def get_books(self, obj):
|
|
"""Get all books in this collection"""
|
|
books = obj.books.filter(status=True).order_by('-view_count')[:8]
|
|
return BookSerializer(books, many=True, context=self.context).data
|
|
|
|
|
|
class BookDownloadSerializer(serializers.ModelSerializer):
|
|
"""Serializer for book downloads"""
|
|
book_id = serializers.IntegerField(write_only=True)
|
|
|
|
class Meta:
|
|
model = BookDownload
|
|
fields = ('id', 'book_id', 'created_at', 'updated_at', 'status')
|
|
read_only_fields = ('id', 'created_at', 'updated_at', 'status')
|
|
|
|
def validate_book_id(self, value):
|
|
"""Validate that the book exists and is active"""
|
|
try:
|
|
book = Book.objects.get(id=value, status=True)
|
|
return value
|
|
except Book.DoesNotExist:
|
|
raise serializers.ValidationError("Book not found or inactive")
|
|
|
|
def create(self, validated_data):
|
|
"""Create a new book download record"""
|
|
book_id = validated_data.pop('book_id')
|
|
user = self.context['request'].user
|
|
book = Book.objects.get(id=book_id)
|
|
|
|
# Create or update the download record
|
|
download, created = BookDownload.objects.update_or_create(
|
|
user=user,
|
|
book=book,
|
|
defaults={'status': True}
|
|
)
|
|
|
|
# Increment the book's download count
|
|
book.download_count += 1
|
|
book.save(update_fields=['download_count'])
|
|
|
|
return download
|
|
|
|
|