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.
76 lines
2.2 KiB
76 lines
2.2 KiB
from rest_framework import serializers
|
|
from .models import Bookmark
|
|
|
|
|
|
class BookmarkSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Bookmark model.
|
|
"""
|
|
|
|
class Meta:
|
|
model = Bookmark
|
|
fields = ['id', 'user', 'service', 'content_id', 'status', 'created_at', 'updated_at']
|
|
read_only_fields = ['id', 'created_at', 'updated_at', 'status']
|
|
|
|
def validate(self, data):
|
|
"""
|
|
Validate that the content_id exists in the specified service.
|
|
"""
|
|
service = data.get('service')
|
|
content_id = data.get('content_id')
|
|
|
|
if not Bookmark.validate_content_exists(service, content_id):
|
|
raise serializers.ValidationError(
|
|
f"Content does not exist in service."
|
|
)
|
|
|
|
return data
|
|
|
|
|
|
class BookmarkStatusSerializer(serializers.Serializer):
|
|
"""
|
|
Serializer for bookmark status information.
|
|
This can be used as a SerializerMethodField in other serializers.
|
|
"""
|
|
is_bookmarked = serializers.BooleanField(default=False)
|
|
content_id = serializers.IntegerField()
|
|
|
|
|
|
bookmark_info = BookmarkSerializer(read_only=True)
|
|
|
|
@staticmethod
|
|
def get_bookmark_info(obj, user, service):
|
|
"""
|
|
Get bookmark information for a specific object.
|
|
|
|
Args:
|
|
obj: The object being serialized
|
|
user: The user to check bookmark status for
|
|
service: The service name (library, podcast, hadith, video)
|
|
|
|
Returns:
|
|
Dictionary with is_bookmarked and content_id
|
|
"""
|
|
if not user or user.is_anonymous:
|
|
return {
|
|
'is_bookmarked': False,
|
|
'content_id': getattr(obj, 'id', None)
|
|
}
|
|
|
|
content_id = getattr(obj, 'id', None)
|
|
if content_id is None:
|
|
return {
|
|
'is_bookmarked': False,
|
|
'content_id': None
|
|
}
|
|
|
|
is_bookmarked = Bookmark.is_bookmarked(
|
|
user=user,
|
|
service=service,
|
|
content_id=content_id
|
|
)
|
|
|
|
return {
|
|
'is_bookmarked': is_bookmarked,
|
|
'content_id': content_id
|
|
}
|