2024-09-10 21:19:20 +02:00
|
|
|
import logging
|
|
|
|
|
2021-02-14 18:00:22 +01:00
|
|
|
from rest_framework import viewsets, mixins, status
|
|
|
|
from rest_framework.decorators import action
|
2023-08-15 00:20:52 +02:00
|
|
|
from rest_framework.permissions import AllowAny
|
2021-02-14 18:00:22 +01:00
|
|
|
from rest_framework.response import Response
|
2020-09-27 09:34:56 +02:00
|
|
|
from rest_framework.routers import DefaultRouter
|
|
|
|
|
|
|
|
from bookmarks import queries
|
2024-01-27 11:29:16 +01:00
|
|
|
from bookmarks.api.serializers import (
|
|
|
|
BookmarkSerializer,
|
|
|
|
TagSerializer,
|
|
|
|
UserProfileSerializer,
|
|
|
|
)
|
2023-09-01 22:48:21 +02:00
|
|
|
from bookmarks.models import Bookmark, BookmarkSearch, Tag, User
|
2024-06-16 10:04:38 +02:00
|
|
|
from bookmarks.services import auto_tagging
|
2024-01-27 11:29:16 +01:00
|
|
|
from bookmarks.services.bookmarks import (
|
|
|
|
archive_bookmark,
|
|
|
|
unarchive_bookmark,
|
|
|
|
website_loader,
|
|
|
|
)
|
2023-01-20 22:44:10 +01:00
|
|
|
from bookmarks.services.website_loader import WebsiteMetadata
|
2020-09-27 09:34:56 +02:00
|
|
|
|
2024-09-10 21:19:20 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-09-27 09:34:56 +02:00
|
|
|
|
2024-01-27 11:29:16 +01:00
|
|
|
class BookmarkViewSet(
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
):
|
2020-09-27 09:34:56 +02:00
|
|
|
serializer_class = BookmarkSerializer
|
|
|
|
|
2023-08-15 00:20:52 +02:00
|
|
|
def get_permissions(self):
|
|
|
|
# Allow unauthenticated access to shared bookmarks.
|
|
|
|
# The shared action should still filter bookmarks so that
|
|
|
|
# unauthenticated users only see bookmarks from users that have public
|
|
|
|
# sharing explicitly enabled
|
2024-01-27 11:29:16 +01:00
|
|
|
if self.action == "shared":
|
2023-08-15 00:20:52 +02:00
|
|
|
return [AllowAny()]
|
|
|
|
|
|
|
|
# Otherwise use default permissions which should require authentication
|
|
|
|
return super().get_permissions()
|
|
|
|
|
2020-09-27 09:34:56 +02:00
|
|
|
def get_queryset(self):
|
|
|
|
user = self.request.user
|
|
|
|
# For list action, use query set that applies search and tag projections
|
2024-01-27 11:29:16 +01:00
|
|
|
if self.action == "list":
|
2023-10-01 21:22:44 +02:00
|
|
|
search = BookmarkSearch.from_request(self.request.GET)
|
2023-09-01 22:48:21 +02:00
|
|
|
return queries.query_bookmarks(user, user.profile, search)
|
2020-09-27 09:34:56 +02:00
|
|
|
|
|
|
|
# For single entity actions use default query set without projections
|
|
|
|
return Bookmark.objects.all().filter(owner=user)
|
|
|
|
|
|
|
|
def get_serializer_context(self):
|
2024-09-22 07:52:00 +02:00
|
|
|
disable_scraping = "disable_scraping" in self.request.GET
|
|
|
|
return {
|
|
|
|
"request": self.request,
|
|
|
|
"user": self.request.user,
|
|
|
|
"disable_scraping": disable_scraping,
|
|
|
|
}
|
2020-09-27 09:34:56 +02:00
|
|
|
|
2024-01-27 11:29:16 +01:00
|
|
|
@action(methods=["get"], detail=False)
|
2021-02-14 18:00:22 +01:00
|
|
|
def archived(self, request):
|
|
|
|
user = request.user
|
2023-10-01 21:22:44 +02:00
|
|
|
search = BookmarkSearch.from_request(request.GET)
|
2023-09-01 22:48:21 +02:00
|
|
|
query_set = queries.query_archived_bookmarks(user, user.profile, search)
|
2021-02-14 18:00:22 +01:00
|
|
|
page = self.paginate_queryset(query_set)
|
2024-06-18 23:07:14 +02:00
|
|
|
serializer = self.get_serializer(page, many=True)
|
|
|
|
data = serializer.data
|
2021-02-14 18:00:22 +01:00
|
|
|
return self.get_paginated_response(data)
|
|
|
|
|
2024-01-27 11:29:16 +01:00
|
|
|
@action(methods=["get"], detail=False)
|
2022-08-04 19:37:16 +02:00
|
|
|
def shared(self, request):
|
2023-10-01 21:22:44 +02:00
|
|
|
search = BookmarkSearch.from_request(request.GET)
|
2023-09-01 22:48:21 +02:00
|
|
|
user = User.objects.filter(username=search.user).first()
|
2023-08-15 00:20:52 +02:00
|
|
|
public_only = not request.user.is_authenticated
|
2024-01-27 11:29:16 +01:00
|
|
|
query_set = queries.query_shared_bookmarks(
|
|
|
|
user, request.user_profile, search, public_only
|
|
|
|
)
|
2022-08-04 19:37:16 +02:00
|
|
|
page = self.paginate_queryset(query_set)
|
2024-06-18 23:07:14 +02:00
|
|
|
serializer = self.get_serializer(page, many=True)
|
|
|
|
data = serializer.data
|
2022-08-04 19:37:16 +02:00
|
|
|
return self.get_paginated_response(data)
|
|
|
|
|
2024-01-27 11:29:16 +01:00
|
|
|
@action(methods=["post"], detail=True)
|
2021-02-18 07:14:44 +01:00
|
|
|
def archive(self, request, pk):
|
2021-02-16 04:24:22 +01:00
|
|
|
bookmark = self.get_object()
|
|
|
|
archive_bookmark(bookmark)
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
2024-01-27 11:29:16 +01:00
|
|
|
@action(methods=["post"], detail=True)
|
2021-02-18 07:14:44 +01:00
|
|
|
def unarchive(self, request, pk):
|
2021-02-16 04:24:22 +01:00
|
|
|
bookmark = self.get_object()
|
|
|
|
unarchive_bookmark(bookmark)
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
2024-01-27 11:29:16 +01:00
|
|
|
@action(methods=["get"], detail=False)
|
2021-02-16 04:45:21 +01:00
|
|
|
def check(self, request):
|
2024-01-27 11:29:16 +01:00
|
|
|
url = request.GET.get("url")
|
2021-02-16 04:45:21 +01:00
|
|
|
bookmark = Bookmark.objects.filter(owner=request.user, url=url).first()
|
2024-01-27 11:29:16 +01:00
|
|
|
existing_bookmark_data = (
|
|
|
|
self.get_serializer(bookmark).data if bookmark else None
|
|
|
|
)
|
2021-02-16 04:45:21 +01:00
|
|
|
|
2024-09-22 07:52:00 +02:00
|
|
|
metadata = website_loader.load_website_metadata(url)
|
2021-02-16 04:45:21 +01:00
|
|
|
|
2024-06-16 10:04:38 +02:00
|
|
|
# Return tags that would be automatically applied to the bookmark
|
|
|
|
profile = request.user.profile
|
|
|
|
auto_tags = []
|
|
|
|
if profile.auto_tagging_rules:
|
2024-09-10 21:19:20 +02:00
|
|
|
try:
|
|
|
|
auto_tags = auto_tagging.get_tags(profile.auto_tagging_rules, url)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(
|
2024-09-22 07:52:00 +02:00
|
|
|
f"Failed to auto-tag bookmark. url={url}",
|
2024-09-10 21:19:20 +02:00
|
|
|
exc_info=e,
|
|
|
|
)
|
2024-06-16 10:04:38 +02:00
|
|
|
|
2024-01-27 11:29:16 +01:00
|
|
|
return Response(
|
2024-06-16 10:04:38 +02:00
|
|
|
{
|
|
|
|
"bookmark": existing_bookmark_data,
|
|
|
|
"metadata": metadata.to_dict(),
|
|
|
|
"auto_tags": auto_tags,
|
|
|
|
},
|
2024-01-27 11:29:16 +01:00
|
|
|
status=status.HTTP_200_OK,
|
|
|
|
)
|
2021-02-16 04:45:21 +01:00
|
|
|
|
2020-09-27 09:34:56 +02:00
|
|
|
|
2024-01-27 11:29:16 +01:00
|
|
|
class TagViewSet(
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
):
|
2020-09-27 09:34:56 +02:00
|
|
|
serializer_class = TagSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
user = self.request.user
|
|
|
|
return Tag.objects.all().filter(owner=user)
|
|
|
|
|
|
|
|
def get_serializer_context(self):
|
2024-01-27 11:29:16 +01:00
|
|
|
return {"user": self.request.user}
|
2020-09-27 09:34:56 +02:00
|
|
|
|
|
|
|
|
2023-10-01 21:57:32 +02:00
|
|
|
class UserViewSet(viewsets.GenericViewSet):
|
2024-01-27 11:29:16 +01:00
|
|
|
@action(methods=["get"], detail=False)
|
2023-10-01 21:57:32 +02:00
|
|
|
def profile(self, request):
|
|
|
|
return Response(UserProfileSerializer(request.user.profile).data)
|
|
|
|
|
|
|
|
|
2020-09-27 09:34:56 +02:00
|
|
|
router = DefaultRouter()
|
2024-01-27 11:29:16 +01:00
|
|
|
router.register(r"bookmarks", BookmarkViewSet, basename="bookmark")
|
|
|
|
router.register(r"tags", TagViewSet, basename="tag")
|
|
|
|
router.register(r"user", UserViewSet, basename="user")
|