2021-02-14 17:00:22 +00:00
|
|
|
from rest_framework import viewsets, mixins, status
|
|
|
|
from rest_framework.decorators import action
|
2023-08-14 22:20:52 +00:00
|
|
|
from rest_framework.permissions import AllowAny
|
2021-02-14 17:00:22 +00:00
|
|
|
from rest_framework.response import Response
|
2020-09-27 07:34:56 +00:00
|
|
|
from rest_framework.routers import DefaultRouter
|
|
|
|
|
|
|
|
from bookmarks import queries
|
|
|
|
from bookmarks.api.serializers import BookmarkSerializer, TagSerializer
|
2023-09-01 20:48:21 +00:00
|
|
|
from bookmarks.models import Bookmark, BookmarkSearch, Tag, User
|
2023-01-20 21:44:10 +00:00
|
|
|
from bookmarks.services.bookmarks import archive_bookmark, unarchive_bookmark, website_loader
|
|
|
|
from bookmarks.services.website_loader import WebsiteMetadata
|
2020-09-27 07:34:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BookmarkViewSet(viewsets.GenericViewSet,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
mixins.DestroyModelMixin):
|
|
|
|
serializer_class = BookmarkSerializer
|
|
|
|
|
2023-08-14 22:20:52 +00: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
|
|
|
|
if self.action == 'shared':
|
|
|
|
return [AllowAny()]
|
|
|
|
|
|
|
|
# Otherwise use default permissions which should require authentication
|
|
|
|
return super().get_permissions()
|
|
|
|
|
2020-09-27 07:34:56 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
user = self.request.user
|
|
|
|
# For list action, use query set that applies search and tag projections
|
|
|
|
if self.action == 'list':
|
2023-09-01 20:48:21 +00:00
|
|
|
search = BookmarkSearch.from_request(self.request)
|
|
|
|
return queries.query_bookmarks(user, user.profile, search)
|
2020-09-27 07:34:56 +00:00
|
|
|
|
|
|
|
# For single entity actions use default query set without projections
|
|
|
|
return Bookmark.objects.all().filter(owner=user)
|
|
|
|
|
|
|
|
def get_serializer_context(self):
|
|
|
|
return {'user': self.request.user}
|
|
|
|
|
2021-02-14 17:00:22 +00:00
|
|
|
@action(methods=['get'], detail=False)
|
|
|
|
def archived(self, request):
|
|
|
|
user = request.user
|
2023-09-01 20:48:21 +00:00
|
|
|
search = BookmarkSearch.from_request(request)
|
|
|
|
query_set = queries.query_archived_bookmarks(user, user.profile, search)
|
2021-02-14 17:00:22 +00:00
|
|
|
page = self.paginate_queryset(query_set)
|
|
|
|
serializer = self.get_serializer_class()
|
|
|
|
data = serializer(page, many=True).data
|
|
|
|
return self.get_paginated_response(data)
|
|
|
|
|
2022-08-04 17:37:16 +00:00
|
|
|
@action(methods=['get'], detail=False)
|
|
|
|
def shared(self, request):
|
2023-09-01 20:48:21 +00:00
|
|
|
search = BookmarkSearch.from_request(request)
|
|
|
|
user = User.objects.filter(username=search.user).first()
|
2023-08-14 22:20:52 +00:00
|
|
|
public_only = not request.user.is_authenticated
|
2023-09-01 20:48:21 +00:00
|
|
|
query_set = queries.query_shared_bookmarks(user, request.user_profile, search, public_only)
|
2022-08-04 17:37:16 +00:00
|
|
|
page = self.paginate_queryset(query_set)
|
|
|
|
serializer = self.get_serializer_class()
|
|
|
|
data = serializer(page, many=True).data
|
|
|
|
return self.get_paginated_response(data)
|
|
|
|
|
2021-02-16 03:24:22 +00:00
|
|
|
@action(methods=['post'], detail=True)
|
2021-02-18 06:14:44 +00:00
|
|
|
def archive(self, request, pk):
|
2021-02-16 03:24:22 +00:00
|
|
|
bookmark = self.get_object()
|
|
|
|
archive_bookmark(bookmark)
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
|
|
|
@action(methods=['post'], detail=True)
|
2021-02-18 06:14:44 +00:00
|
|
|
def unarchive(self, request, pk):
|
2021-02-16 03:24:22 +00:00
|
|
|
bookmark = self.get_object()
|
|
|
|
unarchive_bookmark(bookmark)
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
2021-02-16 03:45:21 +00:00
|
|
|
@action(methods=['get'], detail=False)
|
|
|
|
def check(self, request):
|
|
|
|
url = request.GET.get('url')
|
|
|
|
bookmark = Bookmark.objects.filter(owner=request.user, url=url).first()
|
2023-01-20 21:44:10 +00:00
|
|
|
existing_bookmark_data = self.get_serializer(bookmark).data if bookmark else None
|
2021-02-16 03:45:21 +00:00
|
|
|
|
2023-01-20 21:44:10 +00:00
|
|
|
# Either return metadata from existing bookmark, or scrape from URL
|
|
|
|
if bookmark:
|
|
|
|
metadata = WebsiteMetadata(url, bookmark.website_title, bookmark.website_description)
|
|
|
|
else:
|
|
|
|
metadata = website_loader.load_website_metadata(url)
|
2021-02-16 03:45:21 +00:00
|
|
|
|
|
|
|
return Response({
|
|
|
|
'bookmark': existing_bookmark_data,
|
|
|
|
'metadata': metadata.to_dict()
|
|
|
|
}, status=status.HTTP_200_OK)
|
|
|
|
|
2020-09-27 07:34:56 +00:00
|
|
|
|
|
|
|
class TagViewSet(viewsets.GenericViewSet,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
mixins.CreateModelMixin):
|
|
|
|
serializer_class = TagSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
user = self.request.user
|
|
|
|
return Tag.objects.all().filter(owner=user)
|
|
|
|
|
|
|
|
def get_serializer_context(self):
|
|
|
|
return {'user': self.request.user}
|
|
|
|
|
|
|
|
|
|
|
|
router = DefaultRouter()
|
|
|
|
router.register(r'bookmarks', BookmarkViewSet, basename='bookmark')
|
|
|
|
router.register(r'tags', TagViewSet, basename='tag')
|