linkding/bookmarks/views/bookmarks.py

161 lines
5.2 KiB
Python
Raw Normal View History

import urllib.parse
2019-07-02 07:34:12 +00:00
from django.contrib.auth.decorators import login_required
2019-06-29 07:15:07 +00:00
from django.core.paginator import Paginator
2019-06-29 10:53:37 +00:00
from django.http import HttpResponseRedirect
2019-06-27 06:09:51 +00:00
from django.shortcuts import render
from django.urls import reverse
2019-06-29 10:53:37 +00:00
from bookmarks import queries
2019-07-01 20:05:38 +00:00
from bookmarks.models import Bookmark, BookmarkForm, build_tag_string
from bookmarks.services.bookmarks import create_bookmark, update_bookmark, archive_bookmark, unarchive_bookmark
2019-06-27 06:09:51 +00:00
2019-06-29 07:15:07 +00:00
_default_page_size = 30
2019-06-27 06:09:51 +00:00
2019-07-02 07:34:12 +00:00
@login_required
2019-07-06 06:14:13 +00:00
def index(request):
2019-06-29 10:53:37 +00:00
query_string = request.GET.get('q')
query_set = queries.query_bookmarks(request.user, query_string)
tags = queries.query_bookmark_tags(request.user, query_string)
base_url = reverse('bookmarks:index')
context = get_bookmark_view_context(request, query_set, tags, base_url)
return render(request, 'bookmarks/index.html', context)
@login_required
def archived(request):
query_string = request.GET.get('q')
query_set = queries.query_archived_bookmarks(request.user, query_string)
tags = queries.query_archived_bookmark_tags(request.user, query_string)
base_url = reverse('bookmarks:archived')
context = get_bookmark_view_context(request, query_set, tags, base_url)
return render(request, 'bookmarks/archive.html', context)
def get_bookmark_view_context(request, query_set, tags, base_url):
page = request.GET.get('page')
query_string = request.GET.get('q')
2019-06-29 10:53:37 +00:00
paginator = Paginator(query_set, _default_page_size)
2019-06-29 07:15:07 +00:00
bookmarks = paginator.get_page(page)
return_url = generate_return_url(base_url, page, query_string)
2019-06-29 07:15:07 +00:00
2019-06-30 06:24:21 +00:00
if request.GET.get('tag'):
mod = request.GET.copy()
mod.pop('tag')
request.GET = mod
return {
2019-06-29 10:53:37 +00:00
'bookmarks': bookmarks,
2019-06-30 17:31:32 +00:00
'tags': tags,
2019-06-29 10:53:37 +00:00
'query': query_string if query_string else '',
'empty': paginator.count == 0,
'return_url': return_url
2019-06-27 06:09:51 +00:00
}
def generate_return_url(base_url, page, query_string):
url_query = {}
if query_string is not None:
url_query['q'] = query_string
if page is not None:
url_query['page'] = page
url_params = urllib.parse.urlencode(url_query)
return_url = base_url if url_params == '' else base_url + '?' + url_params
return urllib.parse.quote_plus(return_url)
2019-07-02 07:34:12 +00:00
@login_required
2019-07-06 06:14:13 +00:00
def new(request):
2019-07-05 20:29:21 +00:00
initial_url = request.GET.get('url')
initial_auto_close = 'auto_close' in request.GET
2019-06-28 17:37:41 +00:00
if request.method == 'POST':
2019-06-28 23:08:22 +00:00
form = BookmarkForm(request.POST)
2019-07-05 20:29:21 +00:00
auto_close = form.data['auto_close']
2019-06-28 17:37:41 +00:00
if form.is_valid():
current_user = request.user
create_bookmark(form.save(commit=False), form.data['tag_string'], current_user)
2019-07-05 20:29:21 +00:00
if auto_close:
return HttpResponseRedirect(reverse('bookmarks:close'))
else:
return HttpResponseRedirect(reverse('bookmarks:index'))
2019-06-28 17:37:41 +00:00
else:
2019-06-28 23:08:22 +00:00
form = BookmarkForm()
2019-07-05 20:29:21 +00:00
if initial_url:
form.initial['url'] = initial_url
if initial_auto_close:
form.initial['auto_close'] = 'true'
2019-06-27 06:09:51 +00:00
all_tags = queries.get_user_tags(request.user)
context = {
'form': form,
'auto_close': initial_auto_close,
'all_tags': all_tags,
'return_url': reverse('bookmarks:index')
}
2019-12-27 11:32:44 +00:00
return render(request, 'bookmarks/new.html', context)
2019-06-28 05:33:08 +00:00
2019-07-02 07:34:12 +00:00
@login_required
2019-07-06 06:14:13 +00:00
def edit(request, bookmark_id: int):
2019-06-28 22:27:20 +00:00
bookmark = Bookmark.objects.get(pk=bookmark_id)
2019-06-28 22:27:20 +00:00
if request.method == 'POST':
2019-06-28 23:08:22 +00:00
form = BookmarkForm(request.POST, instance=bookmark)
return_url = form.data['return_url']
2019-06-28 22:27:20 +00:00
if form.is_valid():
update_bookmark(form.save(commit=False), form.data['tag_string'], request.user)
return HttpResponseRedirect(return_url)
2019-06-28 22:27:20 +00:00
else:
return_url = request.GET.get('return_url')
2019-06-28 23:08:22 +00:00
form = BookmarkForm(instance=bookmark)
2019-06-28 22:27:20 +00:00
return_url = return_url if return_url else reverse('bookmarks:index')
2019-12-27 11:32:44 +00:00
form.initial['tag_string'] = build_tag_string(bookmark.tag_names, ' ')
form.initial['return_url'] = return_url
all_tags = queries.get_user_tags(request.user)
context = {
'form': form,
'bookmark_id': bookmark_id,
'all_tags': all_tags,
'return_url': return_url
}
2019-12-27 11:32:44 +00:00
return render(request, 'bookmarks/edit.html', context)
2019-06-27 06:09:51 +00:00
2019-07-02 07:34:12 +00:00
@login_required
2019-07-06 06:14:13 +00:00
def remove(request, bookmark_id: int):
2019-06-27 06:09:51 +00:00
bookmark = Bookmark.objects.get(pk=bookmark_id)
bookmark.delete()
return_url = request.GET.get('return_url')
return_url = return_url if return_url else reverse('bookmarks:index')
return HttpResponseRedirect(return_url)
2019-07-05 20:29:21 +00:00
@login_required
def archive(request, bookmark_id: int):
bookmark = Bookmark.objects.get(pk=bookmark_id)
archive_bookmark(bookmark)
return_url = request.GET.get('return_url')
return_url = return_url if return_url else reverse('bookmarks:index')
return HttpResponseRedirect(return_url)
@login_required
def unarchive(request, bookmark_id: int):
bookmark = Bookmark.objects.get(pk=bookmark_id)
unarchive_bookmark(bookmark)
return_url = request.GET.get('return_url')
return_url = return_url if return_url else reverse('bookmarks:archived')
return HttpResponseRedirect(return_url)
2019-07-05 20:29:21 +00:00
@login_required
2019-07-06 06:14:13 +00:00
def close(request):
2019-07-05 20:29:21 +00:00
return render(request, 'bookmarks/close.html')