2020-06-07 12:15:44 +00:00
|
|
|
import logging
|
2022-05-21 19:33:08 +00:00
|
|
|
import time
|
|
|
|
from functools import lru_cache
|
2020-06-07 12:15:44 +00:00
|
|
|
|
2022-05-21 19:33:08 +00:00
|
|
|
import requests
|
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
|
2020-09-27 07:34:56 +00:00
|
|
|
from rest_framework.authtoken.models import Token
|
2019-07-05 08:04:52 +00:00
|
|
|
|
2022-07-23 21:20:27 +00:00
|
|
|
from bookmarks.models import UserProfileForm, FeedToken
|
2019-12-26 21:41:30 +00:00
|
|
|
from bookmarks.queries import query_bookmarks
|
2021-05-14 21:34:53 +00:00
|
|
|
from bookmarks.services import exporter
|
|
|
|
from bookmarks.services import importer
|
2019-07-05 08:04:52 +00:00
|
|
|
|
2020-06-07 12:15:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-08-24 17:47:58 +00:00
|
|
|
try:
|
|
|
|
with open("version.txt", "r") as f:
|
|
|
|
app_version = f.read().strip("\n")
|
2022-05-21 19:33:08 +00:00
|
|
|
except Exception as exc:
|
2021-08-24 17:47:58 +00:00
|
|
|
logging.exception(exc)
|
|
|
|
pass
|
2019-07-05 08:04:52 +00:00
|
|
|
|
2022-05-21 19:33:08 +00:00
|
|
|
|
2019-07-05 08:04:52 +00:00
|
|
|
@login_required
|
2021-03-28 10:11:56 +00:00
|
|
|
def general(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = UserProfileForm(request.POST, instance=request.user.profile)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
else:
|
|
|
|
form = UserProfileForm(instance=request.user.profile)
|
|
|
|
|
2020-06-07 12:15:44 +00:00
|
|
|
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')
|
2022-05-21 19:33:08 +00:00
|
|
|
version_info = get_version_info(get_ttl_hash())
|
2021-03-28 10:11:56 +00:00
|
|
|
return render(request, 'settings/general.html', {
|
|
|
|
'form': form,
|
2020-06-07 12:15:44 +00:00
|
|
|
'import_success_message': import_success_message,
|
|
|
|
'import_errors_message': import_errors_message,
|
2022-05-21 19:33:08 +00:00
|
|
|
'version_info': version_info,
|
2021-02-24 02:36:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2022-05-21 19:33:08 +00:00
|
|
|
# Cache API call response, for one hour when using get_ttl_hash with default params
|
|
|
|
@lru_cache(maxsize=1)
|
|
|
|
def get_version_info(ttl_hash=None):
|
|
|
|
latest_version = None
|
|
|
|
try:
|
|
|
|
latest_version_url = 'https://api.github.com/repos/sissbruecker/linkding/releases/latest'
|
|
|
|
response = requests.get(latest_version_url, timeout=5)
|
|
|
|
json = response.json()
|
|
|
|
latest_version = json['name'][1:]
|
|
|
|
except requests.exceptions.RequestException:
|
|
|
|
pass
|
|
|
|
|
|
|
|
latest_version_info = ''
|
|
|
|
if latest_version == app_version:
|
|
|
|
latest_version_info = ' (latest)'
|
|
|
|
elif latest_version is not None:
|
|
|
|
latest_version_info = f' (latest: {latest_version})'
|
|
|
|
|
|
|
|
return f'{app_version}{latest_version_info}'
|
|
|
|
|
|
|
|
|
|
|
|
def get_ttl_hash(seconds=3600):
|
|
|
|
"""Return the same value within `seconds` time period"""
|
|
|
|
return round(time.time() / seconds)
|
|
|
|
|
|
|
|
|
2021-02-24 02:36:27 +00:00
|
|
|
@login_required
|
|
|
|
def integrations(request):
|
|
|
|
application_url = request.build_absolute_uri("/bookmarks/new")
|
2021-10-03 13:13:45 +00:00
|
|
|
api_token = Token.objects.get_or_create(user=request.user)[0]
|
2022-07-23 21:20:27 +00:00
|
|
|
feed_token = FeedToken.objects.get_or_create(user=request.user)[0]
|
|
|
|
all_feed_url = request.build_absolute_uri(reverse('bookmarks:feeds.all', args=[feed_token.key]))
|
|
|
|
unread_feed_url = request.build_absolute_uri(reverse('bookmarks:feeds.unread', args=[feed_token.key]))
|
2021-02-24 02:36:27 +00:00
|
|
|
return render(request, 'settings/integrations.html', {
|
2021-02-14 17:00:22 +00:00
|
|
|
'application_url': application_url,
|
2022-07-23 21:20:27 +00:00
|
|
|
'api_token': api_token.key,
|
|
|
|
'all_feed_url': all_feed_url,
|
|
|
|
'unread_feed_url': unread_feed_url,
|
2019-07-05 08:04:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2019-07-06 06:14:13 +00:00
|
|
|
def bookmark_import(request):
|
2020-06-07 12:15:44 +00:00
|
|
|
import_file = request.FILES.get('import_file')
|
|
|
|
|
|
|
|
if import_file is None:
|
|
|
|
messages.error(request, 'Please select a file to import.', 'bookmark_import_errors')
|
2021-05-14 21:34:53 +00:00
|
|
|
return HttpResponseRedirect(reverse('bookmarks:settings.general'))
|
2020-06-07 12:15:44 +00:00
|
|
|
|
2019-07-05 08:04:52 +00:00
|
|
|
try:
|
2020-12-31 06:02:28 +00:00
|
|
|
content = import_file.read().decode()
|
2021-05-14 21:34:53 +00:00
|
|
|
result = importer.import_netscape_html(content, request.user)
|
2020-06-07 12:15:44 +00:00
|
|
|
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')
|
2019-07-05 08:04:52 +00:00
|
|
|
pass
|
|
|
|
|
2021-03-28 10:11:56 +00:00
|
|
|
return HttpResponseRedirect(reverse('bookmarks:settings.general'))
|
2019-07-05 08:04:52 +00:00
|
|
|
|
|
|
|
|
2019-12-26 12:45:12 +00:00
|
|
|
@login_required
|
|
|
|
def bookmark_export(request):
|
2020-06-07 12:15:44 +00:00
|
|
|
# noinspection PyBroadException
|
2019-12-26 12:45:12 +00:00
|
|
|
try:
|
2019-12-26 21:41:30 +00:00
|
|
|
bookmarks = query_bookmarks(request.user, '')
|
2021-05-14 21:34:53 +00:00
|
|
|
file_content = exporter.export_netscape_html(bookmarks)
|
2019-12-26 12:45:12 +00:00
|
|
|
|
|
|
|
response = HttpResponse(content_type='text/plain; charset=UTF-8')
|
|
|
|
response['Content-Disposition'] = 'attachment; filename="bookmarks.html"'
|
|
|
|
response.write(file_content)
|
|
|
|
|
|
|
|
return response
|
2020-06-07 12:15:44 +00:00
|
|
|
except:
|
2021-03-28 10:11:56 +00:00
|
|
|
return render(request, 'settings/general.html', {
|
2019-12-26 12:45:12 +00:00
|
|
|
'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
|