From 778f1b2ff33b69b5f768b4253bedfa910ebf7718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Tue, 16 Feb 2021 04:45:21 +0100 Subject: [PATCH] Remove legacy API (#55) --- bookmarks/api/routes.py | 25 +++++++++++++++++++++-- bookmarks/templates/bookmarks/form.html | 2 +- bookmarks/urls.py | 1 - bookmarks/views/__init__.py | 1 - bookmarks/views/api.py | 27 ------------------------- 5 files changed, 24 insertions(+), 32 deletions(-) delete mode 100644 bookmarks/views/api.py diff --git a/bookmarks/api/routes.py b/bookmarks/api/routes.py index 61ea8a0..abd147a 100644 --- a/bookmarks/api/routes.py +++ b/bookmarks/api/routes.py @@ -1,3 +1,4 @@ +from django.urls import reverse from rest_framework import viewsets, mixins, status from rest_framework.decorators import action from rest_framework.response import Response @@ -7,6 +8,7 @@ from bookmarks import queries from bookmarks.api.serializers import BookmarkSerializer, TagSerializer from bookmarks.models import Bookmark, Tag from bookmarks.services.bookmarks import archive_bookmark, unarchive_bookmark +from bookmarks.services.website_loader import load_website_metadata class BookmarkViewSet(viewsets.GenericViewSet, @@ -41,17 +43,36 @@ class BookmarkViewSet(viewsets.GenericViewSet, return self.get_paginated_response(data) @action(methods=['post'], detail=True) - def archive(self, request, pk): + def archive(self, _request, _pk): bookmark = self.get_object() archive_bookmark(bookmark) return Response(status=status.HTTP_204_NO_CONTENT) @action(methods=['post'], detail=True) - def unarchive(self, request, pk): + def unarchive(self, _request, _pk): bookmark = self.get_object() unarchive_bookmark(bookmark) return Response(status=status.HTTP_204_NO_CONTENT) + @action(methods=['get'], detail=False) + def check(self, request): + url = request.GET.get('url') + bookmark = Bookmark.objects.filter(owner=request.user, url=url).first() + existing_bookmark_data = None + + if bookmark is not None: + existing_bookmark_data = { + 'id': bookmark.id, + 'edit_url': reverse('bookmarks:edit', args=[bookmark.id]) + } + + metadata = load_website_metadata(url) + + return Response({ + 'bookmark': existing_bookmark_data, + 'metadata': metadata.to_dict() + }, status=status.HTTP_200_OK) + class TagViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, diff --git a/bookmarks/templates/bookmarks/form.html b/bookmarks/templates/bookmarks/form.html index c521b0b..b9496b9 100644 --- a/bookmarks/templates/bookmarks/form.html +++ b/bookmarks/templates/bookmarks/form.html @@ -97,7 +97,7 @@ toggleIcon(descriptionInput, true); const websiteUrl = encodeURIComponent(urlInput.value); - const requestUrl = `{% url 'bookmarks:api.check_url' %}?url=${websiteUrl}`; + const requestUrl = `{% url 'bookmarks:api-root' %}bookmarks/check?url=${websiteUrl}`; fetch(requestUrl) .then(response => response.json()) .then(data => { diff --git a/bookmarks/urls.py b/bookmarks/urls.py index 236f86d..2821905 100644 --- a/bookmarks/urls.py +++ b/bookmarks/urls.py @@ -23,6 +23,5 @@ urlpatterns = [ path('settings/import', views.settings.bookmark_import, name='settings.import'), path('settings/export', views.settings.bookmark_export, name='settings.export'), # API - path('api/check_url', views.api.check_url, name='api.check_url'), path('api/', include(router.urls), name='api') ] diff --git a/bookmarks/views/__init__.py b/bookmarks/views/__init__.py index 488f848..5a0f826 100644 --- a/bookmarks/views/__init__.py +++ b/bookmarks/views/__init__.py @@ -1,3 +1,2 @@ -from .api import * from .bookmarks import * from .settings import * diff --git a/bookmarks/views/api.py b/bookmarks/views/api.py deleted file mode 100644 index f8a0620..0000000 --- a/bookmarks/views/api.py +++ /dev/null @@ -1,27 +0,0 @@ -from django.contrib.auth.decorators import login_required -from django.forms import model_to_dict -from django.http import JsonResponse -from django.urls import reverse - -from bookmarks.services.website_loader import load_website_metadata -from bookmarks.models import Bookmark - - -@login_required -def check_url(request): - url = request.GET.get('url') - bookmark = Bookmark.objects.filter(owner=request.user, url=url).first() - existing_bookmark_data = None - - if bookmark is not None: - existing_bookmark_data = { - 'id': bookmark.id, - 'edit_url': reverse('bookmarks:edit', args=[bookmark.id]) - } - - metadata = load_website_metadata(url) - - return JsonResponse({ - 'bookmark': existing_bookmark_data, - 'metadata': metadata.to_dict() - })