From a665578ef7b6b41a7b6a4476cc492e236ba0d0f9 Mon Sep 17 00:00:00 2001 From: fkulla Date: Wed, 26 Apr 2023 15:29:15 +0200 Subject: [PATCH 1/2] feat: Implement UserProfile serializer and add API endpoint per #457 --- bookmarks/api/routes.py | 12 +++++++++++- bookmarks/api/serializers.py | 14 +++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/bookmarks/api/routes.py b/bookmarks/api/routes.py index a4cae8a1..30b316c1 100644 --- a/bookmarks/api/routes.py +++ b/bookmarks/api/routes.py @@ -4,7 +4,7 @@ from rest_framework.routers import DefaultRouter from bookmarks import queries -from bookmarks.api.serializers import BookmarkSerializer, TagSerializer +from bookmarks.api.serializers import BookmarkSerializer, TagSerializer, UserProfileSerializer from bookmarks.models import Bookmark, BookmarkFilters, Tag, User from bookmarks.services.bookmarks import archive_bookmark, unarchive_bookmark, website_loader from bookmarks.services.website_loader import WebsiteMetadata @@ -94,7 +94,17 @@ def get_queryset(self): def get_serializer_context(self): return {'user': self.request.user} +class UserViewSet(viewsets.GenericViewSet): + def get_serializer_context(self): + return {'user': self.request.user} + + @action(methods=['get'], detail=False) + def profile(self, request): + return Response(UserProfileSerializer(request.user.profile).data) + + router = DefaultRouter() router.register(r'bookmarks', BookmarkViewSet, basename='bookmark') router.register(r'tags', TagViewSet, basename='tag') +router.register(r'user', UserViewSet, basename='user') diff --git a/bookmarks/api/serializers.py b/bookmarks/api/serializers.py index e9d4d112..3a320f14 100644 --- a/bookmarks/api/serializers.py +++ b/bookmarks/api/serializers.py @@ -2,7 +2,7 @@ from rest_framework import serializers from rest_framework.serializers import ListSerializer -from bookmarks.models import Bookmark, Tag, build_tag_string +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 @@ -86,3 +86,15 @@ class Meta: def create(self, validated_data): 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", + "enable_sharing", + "enable_favicons" + ] From e7deb44a31e042575236ebb7fe8627586c6cd436 Mon Sep 17 00:00:00 2001 From: fkulla Date: Wed, 26 Apr 2023 15:29:15 +0200 Subject: [PATCH 2/2] chore: Document API addition --- docs/API.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/API.md b/docs/API.md index 3e4f50b3..6e32edcb 100644 --- a/docs/API.md +++ b/docs/API.md @@ -234,3 +234,24 @@ Example payload: "name": "example" } ``` + +### User + +**Profile** + +``` +GET /api/user/profile/ +``` + +User preferences. + +```json +{ + "theme": "auto", + "bookmark_date_display": "relative", + "bookmark_link_target": "_blank", + "web_archive_integration": "disabled", + "enable_sharing": false, + "enable_favicons": false +} +```