From 348a536aa3cce8cc4cc8fa2458870e9caa1c9304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Sun, 13 Sep 2020 08:46:07 +0200 Subject: [PATCH] #23 Prevent bookmark duplicates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Show hint if URL is already bookmarked * Remove hint if URL belongs to edited bookmark * Fix query param encoding * Update bookmark instead of duplicating it Co-authored-by: Sascha Ißbrücker --- bookmarks/services/bookmarks.py | 8 +++++++ bookmarks/styles/bookmarks.scss | 11 +++++++++ bookmarks/templates/bookmarks/edit.html | 2 +- bookmarks/templates/bookmarks/form.html | 31 +++++++++++++++++++------ bookmarks/templates/bookmarks/new.html | 2 +- bookmarks/templatetags/bookmarks.py | 5 ++-- bookmarks/urls.py | 2 +- bookmarks/views/api.py | 20 ++++++++++++++-- 8 files changed, 67 insertions(+), 14 deletions(-) diff --git a/bookmarks/services/bookmarks.py b/bookmarks/services/bookmarks.py index 0453dfc..441620b 100644 --- a/bookmarks/services/bookmarks.py +++ b/bookmarks/services/bookmarks.py @@ -7,6 +7,14 @@ from bookmarks.services.website_loader import load_website_metadata def create_bookmark(form: BookmarkForm, current_user: User): + # If URL is already bookmarked, then update it + existing_bookmark = Bookmark.objects.filter(owner=current_user, url=form.data['url']).first() + + if existing_bookmark is not None: + update_form = BookmarkForm(data=form.data, instance=existing_bookmark) + update_bookmark(update_form, current_user) + return + bookmark = form.save(commit=False) # Update website info _update_website_metadata(bookmark) diff --git a/bookmarks/styles/bookmarks.scss b/bookmarks/styles/bookmarks.scss index 06f789e..b245727 100644 --- a/bookmarks/styles/bookmarks.scss +++ b/bookmarks/styles/bookmarks.scss @@ -55,4 +55,15 @@ ul.bookmark-list { .form-icon.loading { visibility: hidden; } + + .form-input-hint.bookmark-exists { + visibility: hidden; + color: $warning-color; + + a { + color: $warning-color; + text-decoration: underline; + font-weight: bold; + } + } } diff --git a/bookmarks/templates/bookmarks/edit.html b/bookmarks/templates/bookmarks/edit.html index d5e6f54..6ebad75 100644 --- a/bookmarks/templates/bookmarks/edit.html +++ b/bookmarks/templates/bookmarks/edit.html @@ -8,7 +8,7 @@

Edit bookmark

- {% bookmark_form form all_tags %} + {% bookmark_form form all_tags bookmark_id %}
diff --git a/bookmarks/templates/bookmarks/form.html b/bookmarks/templates/bookmarks/form.html index 377c4c6..aaa7bbc 100644 --- a/bookmarks/templates/bookmarks/form.html +++ b/bookmarks/templates/bookmarks/form.html @@ -12,6 +12,9 @@ {{ form.url.errors }} {% endif %} +
+ This URL is already bookmarked. You can edit it or you can overwrite the existing bookmark by saving this form. +
@@ -77,28 +80,42 @@
diff --git a/bookmarks/templates/bookmarks/new.html b/bookmarks/templates/bookmarks/new.html index 0ee2f6a..9399fff 100644 --- a/bookmarks/templates/bookmarks/new.html +++ b/bookmarks/templates/bookmarks/new.html @@ -8,7 +8,7 @@

New bookmark

- {% bookmark_form form all_tags auto_close %} + {% bookmark_form form all_tags auto_close=auto_close %}
diff --git a/bookmarks/templatetags/bookmarks.py b/bookmarks/templatetags/bookmarks.py index dfaaefb..98ea43b 100644 --- a/bookmarks/templatetags/bookmarks.py +++ b/bookmarks/templatetags/bookmarks.py @@ -9,7 +9,7 @@ register = template.Library() @register.inclusion_tag('bookmarks/form.html', name='bookmark_form') -def bookmark_form(form: BookmarkForm, all_tags: List[Tag], auto_close: bool = False): +def bookmark_form(form: BookmarkForm, all_tags: List[Tag], bookmark_id: int = 0, auto_close: bool = False): all_tag_names = [tag.name for tag in all_tags] all_tags_string = build_tag_string(all_tag_names, ' ') @@ -17,7 +17,8 @@ def bookmark_form(form: BookmarkForm, all_tags: List[Tag], auto_close: bool = Fa return { 'form': form, 'auto_close': auto_close, - 'all_tags': all_tags_string + 'all_tags': all_tags_string, + 'bookmark_id': bookmark_id } diff --git a/bookmarks/urls.py b/bookmarks/urls.py index 90b8487..ef265a3 100644 --- a/bookmarks/urls.py +++ b/bookmarks/urls.py @@ -20,5 +20,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/website_metadata', views.api.website_metadata, name='api.website_metadata'), + path('api/check_url', views.api.check_url, name='api.check_url'), ] diff --git a/bookmarks/views/api.py b/bookmarks/views/api.py index b4dd8fe..f8a0620 100644 --- a/bookmarks/views/api.py +++ b/bookmarks/views/api.py @@ -1,11 +1,27 @@ 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 website_metadata(request): +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(metadata.to_dict()) + + return JsonResponse({ + 'bookmark': existing_bookmark_data, + 'metadata': metadata.to_dict() + })