From 7e9db1588fd464fd965d0466258868458f926537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Fri, 5 Jul 2019 10:04:52 +0200 Subject: [PATCH] Implement bookmark import form --- bookmarks/styles/index.scss | 1 + bookmarks/styles/settings.scss | 9 +++++ bookmarks/templates/settings/index.html | 47 +++++++++++++++++++++++++ bookmarks/urls.py | 13 ++++--- bookmarks/views/__init__.py | 3 +- bookmarks/views/api.py | 2 +- bookmarks/views/bookmarks.py | 8 ++--- bookmarks/views/settings.py | 35 ++++++++++++++++++ 8 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 bookmarks/styles/settings.scss create mode 100644 bookmarks/templates/settings/index.html create mode 100644 bookmarks/views/settings.py diff --git a/bookmarks/styles/index.scss b/bookmarks/styles/index.scss index 986cf45..b86c22e 100644 --- a/bookmarks/styles/index.scss +++ b/bookmarks/styles/index.scss @@ -22,4 +22,5 @@ $alternative-color-dark: darken($alternative-color, 5%); @import "util"; @import "shared"; @import "bookmarks"; +@import "settings"; @import "login"; diff --git a/bookmarks/styles/settings.scss b/bookmarks/styles/settings.scss new file mode 100644 index 0000000..11415de --- /dev/null +++ b/bookmarks/styles/settings.scss @@ -0,0 +1,9 @@ +.settings-page { + section.content-area { + margin-bottom: 2rem; + } + + .input-group > input[type=submit] { + height: auto; + } +} diff --git a/bookmarks/templates/settings/index.html b/bookmarks/templates/settings/index.html new file mode 100644 index 0000000..f30bf70 --- /dev/null +++ b/bookmarks/templates/settings/index.html @@ -0,0 +1,47 @@ +{% extends "bookmarks/layout.html" %} + +{% block content %} +
+ + {# Import section #} +
+
+

Import

+
+

Import bookmarks and tags in the Netscape HTML format. This will execute a sync where new bookmarks are + added and existing ones are updated.

+
+ {% csrf_token %} +
+
+ + +
+ {% if import_message %} + {% if import_message.level == DEFAULT_MESSAGE_LEVELS.SUCCESS %} +
+ {% endif %} + {% if import_message.level == DEFAULT_MESSAGE_LEVELS.ERROR %} +
+ {% endif %} +

+ {{ import_message }} +

+
+ {% endif %} +
+ +
+ + {# Export section #} +
+
+

Export

+
+

Export all bookmarks in Netscape HTML format.

+ Download (.html) +
+ +
+ +{% endblock %} diff --git a/bookmarks/urls.py b/bookmarks/urls.py index 47c16aa..df08386 100644 --- a/bookmarks/urls.py +++ b/bookmarks/urls.py @@ -9,10 +9,13 @@ urlpatterns = [ # Redirect root to bookmarks index url(r'^$', RedirectView.as_view(pattern_name='bookmarks:index', permanent=False)), # Bookmarks - path('bookmarks', views.index, name='index'), - path('bookmarks/new', views.new, name='new'), - path('bookmarks//edit', views.edit, name='edit'), - path('bookmarks//remove', views.remove, name='remove'), + path('bookmarks', views.bookmarks_index, name='index'), + path('bookmarks/new', views.bookmarks_new, name='new'), + path('bookmarks//edit', views.bookmarks_edit, name='edit'), + path('bookmarks//remove', views.bookmarks_remove, name='remove'), + # Settings + path('settings', views.settings_index, name='settings_index'), + path('settings/import', views.settings_bookmark_import, name='settings_import'), # API - path('api/website_metadata', views.api.website_metadata, name='api.website_metadata'), + path('api/website_metadata', views.api.api_website_metadata, name='api.website_metadata'), ] diff --git a/bookmarks/views/__init__.py b/bookmarks/views/__init__.py index 5eba78a..488f848 100644 --- a/bookmarks/views/__init__.py +++ b/bookmarks/views/__init__.py @@ -1,2 +1,3 @@ -from .bookmarks import * from .api import * +from .bookmarks import * +from .settings import * diff --git a/bookmarks/views/api.py b/bookmarks/views/api.py index b4dd8fe..6534cec 100644 --- a/bookmarks/views/api.py +++ b/bookmarks/views/api.py @@ -5,7 +5,7 @@ from bookmarks.services.website_loader import load_website_metadata @login_required -def website_metadata(request): +def api_website_metadata(request): url = request.GET.get('url') metadata = load_website_metadata(url) return JsonResponse(metadata.to_dict()) diff --git a/bookmarks/views/bookmarks.py b/bookmarks/views/bookmarks.py index 153f107..9694e41 100644 --- a/bookmarks/views/bookmarks.py +++ b/bookmarks/views/bookmarks.py @@ -12,7 +12,7 @@ _default_page_size = 30 @login_required -def index(request): +def bookmarks_index(request): page = request.GET.get('page') query_string = request.GET.get('q') query_set = queries.query_bookmarks(request.user, query_string) @@ -34,7 +34,7 @@ def index(request): @login_required -def new(request): +def bookmarks_new(request): if request.method == 'POST': form = BookmarkForm(request.POST) if form.is_valid(): @@ -48,7 +48,7 @@ def new(request): @login_required -def edit(request, bookmark_id: int): +def bookmarks_edit(request, bookmark_id: int): bookmark = Bookmark.objects.get(pk=bookmark_id) if request.method == 'POST': form = BookmarkForm(request.POST, instance=bookmark) @@ -63,7 +63,7 @@ def edit(request, bookmark_id: int): @login_required -def remove(request, bookmark_id: int): +def bookmarks_remove(request, bookmark_id: int): bookmark = Bookmark.objects.get(pk=bookmark_id) bookmark.delete() return HttpResponseRedirect(reverse('bookmarks:index')) diff --git a/bookmarks/views/settings.py b/bookmarks/views/settings.py new file mode 100644 index 0000000..a851ddb --- /dev/null +++ b/bookmarks/views/settings.py @@ -0,0 +1,35 @@ +from django.contrib import messages +from django.contrib.auth.decorators import login_required +from django.http import HttpResponseRedirect +from django.shortcuts import render +from django.urls import reverse + +from services.importer import import_netscape_html + + +@login_required +def settings_index(request): + import_message = _find_message_with_tag(messages.get_messages(request), 'bookmark_import') + return render(request, 'settings/index.html', { + 'import_message': import_message + }) + + +@login_required +def settings_bookmark_import(request): + try: + import_file = request.FILES.get('import_file') + content = import_file.read() + import_netscape_html(content, request.user) + messages.success(request, 'Bookmarks were successfully imported.', 'bookmark_import') + except(): + messages.error(request, 'An error occurred during bookmark import.', 'bookmark_import') + pass + + return HttpResponseRedirect(reverse('bookmarks:settings_index')) + + +def _find_message_with_tag(messages, tag): + for message in messages: + if message.extra_tags == tag: + return message