Implement bookmark import form

This commit is contained in:
Sascha Ißbrücker 2019-07-05 10:04:52 +02:00
parent 70f9fb9818
commit 7e9db1588f
8 changed files with 107 additions and 11 deletions

View File

@ -22,4 +22,5 @@ $alternative-color-dark: darken($alternative-color, 5%);
@import "util";
@import "shared";
@import "bookmarks";
@import "settings";
@import "login";

View File

@ -0,0 +1,9 @@
.settings-page {
section.content-area {
margin-bottom: 2rem;
}
.input-group > input[type=submit] {
height: auto;
}
}

View File

@ -0,0 +1,47 @@
{% extends "bookmarks/layout.html" %}
{% block content %}
<div class="settings-page">
{# Import section #}
<section class="content-area">
<div class="content-area-header">
<h2>Import</h2>
</div>
<p>Import bookmarks and tags in the Netscape HTML format. This will execute a sync where new bookmarks are
added and existing ones are updated.</p>
<form method="post" enctype="multipart/form-data" action="{% url 'bookmarks:settings_import' %}">
{% csrf_token %}
<div class="form-group">
<div class="input-group col-8 col-sm-12">
<input class="form-input" type="file" name="import_file">
<input type="submit" class="input-group-btn col-2 btn btn-primary" value="Upload">
</div>
{% if import_message %}
{% if import_message.level == DEFAULT_MESSAGE_LEVELS.SUCCESS %}
<div class="has-success">
{% endif %}
{% if import_message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}
<div class="has-error">
{% endif %}
<p class="form-input-hint">
{{ import_message }}
</p>
</div>
{% endif %}
</div>
</form>
</section>
{# Export section #}
<section class="content-area">
<div class="content-area-header">
<h2>Export</h2>
</div>
<p>Export all bookmarks in Netscape HTML format.</p>
<a class="btn btn-primary">Download (.html)</a>
</section>
</div>
{% endblock %}

View File

@ -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/<int:bookmark_id>/edit', views.edit, name='edit'),
path('bookmarks/<int:bookmark_id>/remove', views.remove, name='remove'),
path('bookmarks', views.bookmarks_index, name='index'),
path('bookmarks/new', views.bookmarks_new, name='new'),
path('bookmarks/<int:bookmark_id>/edit', views.bookmarks_edit, name='edit'),
path('bookmarks/<int:bookmark_id>/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'),
]

View File

@ -1,2 +1,3 @@
from .bookmarks import *
from .api import *
from .bookmarks import *
from .settings import *

View File

@ -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())

View File

@ -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'))

View File

@ -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