linkding/bookmarks/api/serializers.py

112 lines
4 KiB
Python
Raw Normal View History

from django.db.models import prefetch_related_objects
from rest_framework import serializers
from rest_framework.serializers import ListSerializer
from bookmarks.models import Bookmark, Tag, build_tag_string, UserProfile
from bookmarks.services.bookmarks import create_bookmark, update_bookmark
from bookmarks.services.tags import get_or_create_tag
class TagListField(serializers.ListField):
child = serializers.CharField()
class BookmarkListSerializer(ListSerializer):
def to_representation(self, data):
# Prefetch nested relations to avoid n+1 queries
2024-01-27 10:29:16 +00:00
prefetch_related_objects(data, "tags")
return super().to_representation(data)
class BookmarkSerializer(serializers.ModelSerializer):
class Meta:
model = Bookmark
fields = [
2024-01-27 10:29:16 +00:00
"id",
"url",
"title",
"description",
"notes",
"website_title",
"website_description",
"web_archive_snapshot_url",
2024-01-27 10:29:16 +00:00
"is_archived",
"unread",
"shared",
"tag_names",
"date_added",
"date_modified",
]
read_only_fields = [
2024-01-27 10:29:16 +00:00
"website_title",
"website_description",
"web_archive_snapshot_url",
2024-01-27 10:29:16 +00:00
"date_added",
"date_modified",
]
list_serializer_class = BookmarkListSerializer
# Override optional char fields to provide default value
2024-01-27 10:29:16 +00:00
title = serializers.CharField(required=False, allow_blank=True, default="")
description = serializers.CharField(required=False, allow_blank=True, default="")
notes = serializers.CharField(required=False, allow_blank=True, default="")
is_archived = serializers.BooleanField(required=False, default=False)
unread = serializers.BooleanField(required=False, default=False)
shared = serializers.BooleanField(required=False, default=False)
# Override readonly tag_names property to allow passing a list of tag names to create/update
tag_names = TagListField(required=False, default=[])
def create(self, validated_data):
bookmark = Bookmark()
2024-01-27 10:29:16 +00:00
bookmark.url = validated_data["url"]
bookmark.title = validated_data["title"]
bookmark.description = validated_data["description"]
bookmark.notes = validated_data["notes"]
bookmark.is_archived = validated_data["is_archived"]
bookmark.unread = validated_data["unread"]
bookmark.shared = validated_data["shared"]
tag_string = build_tag_string(validated_data["tag_names"])
return create_bookmark(bookmark, tag_string, self.context["user"])
def update(self, instance: Bookmark, validated_data):
# Update fields if they were provided in the payload
2024-01-27 10:29:16 +00:00
for key in ["url", "title", "description", "notes", "unread", "shared"]:
if key in validated_data:
setattr(instance, key, validated_data[key])
# Use tag string from payload, or use bookmark's current tags as fallback
tag_string = build_tag_string(instance.tag_names)
2024-01-27 10:29:16 +00:00
if "tag_names" in validated_data:
tag_string = build_tag_string(validated_data["tag_names"])
2024-01-27 10:29:16 +00:00
return update_bookmark(instance, tag_string, self.context["user"])
class TagSerializer(serializers.ModelSerializer):
class Meta:
model = Tag
2024-01-27 10:29:16 +00:00
fields = ["id", "name", "date_added"]
read_only_fields = ["date_added"]
def create(self, validated_data):
2024-01-27 10:29:16 +00:00
return get_or_create_tag(validated_data["name"], self.context["user"])
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = [
"theme",
"bookmark_date_display",
"bookmark_link_target",
"web_archive_integration",
"tag_search",
"enable_sharing",
"enable_public_sharing",
"enable_favicons",
"display_url",
"permanent_notes",
"search_preferences",
]