Add error handling and logging for importer

This commit is contained in:
Sascha Ißbrücker
2020-06-07 14:15:44 +02:00
parent 1ab4aa5045
commit 10fd3d89be
4 changed files with 66 additions and 21 deletions

View File

@@ -1,3 +1,5 @@
import logging
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse
@@ -8,24 +10,38 @@ from bookmarks.queries import query_bookmarks
from bookmarks.services.exporter import export_netscape_html
from bookmarks.services.importer import import_netscape_html
logger = logging.getLogger(__name__)
@login_required
def index(request):
import_message = _find_message_with_tag(messages.get_messages(request), 'bookmark_import')
import_success_message = _find_message_with_tag(messages.get_messages(request), 'bookmark_import_success')
import_errors_message = _find_message_with_tag(messages.get_messages(request), 'bookmark_import_errors')
return render(request, 'settings/index.html', {
'import_message': import_message
'import_success_message': import_success_message,
'import_errors_message': import_errors_message,
})
@login_required
def bookmark_import(request):
import_file = request.FILES.get('import_file')
if import_file is None:
messages.error(request, 'Please select a file to import.', 'bookmark_import_errors')
return HttpResponseRedirect(reverse('bookmarks:settings.index'))
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 Exception:
messages.error(request, 'An error occurred during bookmark import.', 'bookmark_import')
result = import_netscape_html(content, request.user)
success_msg = str(result.success) + ' bookmarks were successfully imported.'
messages.success(request, success_msg, 'bookmark_import_success')
if result.failed > 0:
err_msg = str(result.failed) + ' bookmarks could not be imported. Please check the logs for more details.'
messages.error(request, err_msg, 'bookmark_import_errors')
except:
logging.exception('Unexpected error during bookmark import')
messages.error(request, 'An error occurred during bookmark import.', 'bookmark_import_errors')
pass
return HttpResponseRedirect(reverse('bookmarks:settings.index'))
@@ -33,6 +49,7 @@ def bookmark_import(request):
@login_required
def bookmark_export(request):
# noinspection PyBroadException
try:
bookmarks = query_bookmarks(request.user, '')
file_content = export_netscape_html(bookmarks)
@@ -42,7 +59,7 @@ def bookmark_export(request):
response.write(file_content)
return response
except Exception:
except:
return render(request, 'settings/index.html', {
'export_error': 'An error occurred during bookmark export.'
})