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
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 %}
+
@@ -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
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()
+ })