2019-07-05 08:04:52 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2019-12-26 12:45:12 +00:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
2019-07-05 08:04:52 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.urls import reverse
|
|
|
|
|
2019-12-26 21:41:30 +00:00
|
|
|
from bookmarks.queries import query_bookmarks
|
2019-12-26 12:45:12 +00:00
|
|
|
from bookmarks.services.exporter import export_netscape_html
|
2019-07-05 17:26:46 +00:00
|
|
|
from bookmarks.services.importer import import_netscape_html
|
2019-07-05 08:04:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2019-07-06 06:14:13 +00:00
|
|
|
def index(request):
|
2019-07-05 08:04:52 +00:00
|
|
|
import_message = _find_message_with_tag(messages.get_messages(request), 'bookmark_import')
|
|
|
|
return render(request, 'settings/index.html', {
|
|
|
|
'import_message': import_message
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2019-07-06 06:14:13 +00:00
|
|
|
def bookmark_import(request):
|
2019-07-05 08:04:52 +00:00
|
|
|
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')
|
2019-12-26 12:45:12 +00:00
|
|
|
except Exception:
|
2019-07-05 08:04:52 +00:00
|
|
|
messages.error(request, 'An error occurred during bookmark import.', 'bookmark_import')
|
|
|
|
pass
|
|
|
|
|
2019-07-06 15:17:14 +00:00
|
|
|
return HttpResponseRedirect(reverse('bookmarks:settings.index'))
|
2019-07-05 08:04:52 +00:00
|
|
|
|
|
|
|
|
2019-12-26 12:45:12 +00:00
|
|
|
@login_required
|
|
|
|
def bookmark_export(request):
|
|
|
|
try:
|
2019-12-26 21:41:30 +00:00
|
|
|
bookmarks = query_bookmarks(request.user, '')
|
2019-12-26 12:45:12 +00:00
|
|
|
file_content = export_netscape_html(bookmarks)
|
|
|
|
|
|
|
|
response = HttpResponse(content_type='text/plain; charset=UTF-8')
|
|
|
|
response['Content-Disposition'] = 'attachment; filename="bookmarks.html"'
|
|
|
|
response.write(file_content)
|
|
|
|
|
|
|
|
return response
|
|
|
|
except Exception:
|
|
|
|
return render(request, 'settings/index.html', {
|
|
|
|
'export_error': 'An error occurred during bookmark export.'
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2019-07-05 08:04:52 +00:00
|
|
|
def _find_message_with_tag(messages, tag):
|
|
|
|
for message in messages:
|
|
|
|
if message.extra_tags == tag:
|
|
|
|
return message
|