2019-07-02 07:34:12 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2023-08-25 11:54:23 +00:00
|
|
|
from django.db.models import QuerySet
|
|
|
|
from django.http import HttpResponseRedirect, Http404, HttpResponseBadRequest
|
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
|
2023-08-21 21:12:00 +00:00
|
|
|
from bookmarks.models import Bookmark, BookmarkForm, BookmarkFilters, build_tag_string
|
2021-03-28 22:43:50 +00:00
|
|
|
from bookmarks.services.bookmarks import create_bookmark, update_bookmark, archive_bookmark, archive_bookmarks, \
|
2023-08-25 11:54:23 +00:00
|
|
|
unarchive_bookmark, unarchive_bookmarks, delete_bookmarks, tag_bookmarks, untag_bookmarks, mark_bookmarks_as_read, \
|
|
|
|
mark_bookmarks_as_unread, share_bookmarks, unshare_bookmarks
|
2022-03-25 17:29:54 +00:00
|
|
|
from bookmarks.utils import get_safe_return_url
|
2023-08-21 21:12:00 +00:00
|
|
|
from bookmarks.views.partials import contexts
|
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):
|
2023-08-21 21:12:00 +00:00
|
|
|
bookmark_list = contexts.ActiveBookmarkListContext(request)
|
|
|
|
tag_cloud = contexts.ActiveTagCloudContext(request)
|
|
|
|
return render(request, 'bookmarks/index.html', {
|
|
|
|
'bookmark_list': bookmark_list,
|
|
|
|
'tag_cloud': tag_cloud,
|
|
|
|
})
|
2021-02-14 17:00:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def archived(request):
|
2023-08-21 21:12:00 +00:00
|
|
|
bookmark_list = contexts.ArchivedBookmarkListContext(request)
|
|
|
|
tag_cloud = contexts.ArchivedTagCloudContext(request)
|
|
|
|
return render(request, 'bookmarks/archive.html', {
|
|
|
|
'bookmark_list': bookmark_list,
|
|
|
|
'tag_cloud': tag_cloud,
|
|
|
|
})
|
2021-02-14 17:00:22 +00:00
|
|
|
|
|
|
|
|
2022-08-04 17:37:16 +00:00
|
|
|
def shared(request):
|
|
|
|
filters = BookmarkFilters(request)
|
2023-08-21 21:12:00 +00:00
|
|
|
bookmark_list = contexts.SharedBookmarkListContext(request)
|
|
|
|
tag_cloud = contexts.SharedTagCloudContext(request)
|
2023-08-14 22:20:52 +00:00
|
|
|
public_only = not request.user.is_authenticated
|
|
|
|
users = queries.query_shared_bookmark_users(request.user_profile, filters.query, public_only)
|
2023-08-21 21:12:00 +00:00
|
|
|
return render(request, 'bookmarks/shared.html', {
|
|
|
|
'bookmark_list': bookmark_list,
|
|
|
|
'tag_cloud': tag_cloud,
|
|
|
|
'users': users
|
|
|
|
})
|
2020-09-13 08:56:03 +00:00
|
|
|
|
|
|
|
|
2021-12-12 21:54:22 +00:00
|
|
|
def convert_tag_string(tag_string: str):
|
|
|
|
# Tag strings coming from inputs are space-separated, however services.bookmarks functions expect comma-separated
|
|
|
|
# strings
|
|
|
|
return tag_string.replace(' ', ',')
|
|
|
|
|
|
|
|
|
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')
|
2023-05-30 07:19:17 +00:00
|
|
|
initial_title = request.GET.get('title')
|
|
|
|
initial_description = request.GET.get('description')
|
2019-07-05 20:29:21 +00:00
|
|
|
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
|
2021-12-12 21:54:22 +00:00
|
|
|
tag_string = convert_tag_string(form.data['tag_string'])
|
|
|
|
create_bookmark(form.save(commit=False), 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
|
2023-05-30 07:19:17 +00:00
|
|
|
if initial_title:
|
|
|
|
form.initial['title'] = initial_title
|
|
|
|
if initial_description:
|
|
|
|
form.initial['description'] = initial_description
|
2019-07-05 20:29:21 +00:00
|
|
|
if initial_auto_close:
|
|
|
|
form.initial['auto_close'] = 'true'
|
2019-06-27 06:09:51 +00:00
|
|
|
|
2020-09-13 08:56:03 +00:00
|
|
|
context = {
|
|
|
|
'form': form,
|
|
|
|
'auto_close': initial_auto_close,
|
|
|
|
'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):
|
2022-03-22 01:24:21 +00:00
|
|
|
try:
|
|
|
|
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
|
|
except Bookmark.DoesNotExist:
|
|
|
|
raise Http404('Bookmark does not exist')
|
2022-03-25 17:29:54 +00:00
|
|
|
return_url = get_safe_return_url(request.GET.get('return_url'), reverse('bookmarks:index'))
|
2020-09-13 08:56:03 +00:00
|
|
|
|
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)
|
2019-06-28 22:27:20 +00:00
|
|
|
if form.is_valid():
|
2021-12-12 21:54:22 +00:00
|
|
|
tag_string = convert_tag_string(form.data['tag_string'])
|
|
|
|
update_bookmark(form.save(commit=False), tag_string, request.user)
|
2020-09-13 08:56:03 +00:00
|
|
|
return HttpResponseRedirect(return_url)
|
2019-06-28 22:27:20 +00:00
|
|
|
else:
|
2019-06-28 23:08:22 +00:00
|
|
|
form = BookmarkForm(instance=bookmark)
|
2019-06-28 22:27:20 +00:00
|
|
|
|
2020-09-13 08:56:03 +00:00
|
|
|
form.initial['tag_string'] = build_tag_string(bookmark.tag_names, ' ')
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'form': form,
|
|
|
|
'bookmark_id': bookmark_id,
|
|
|
|
'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-06 06:14:13 +00:00
|
|
|
def remove(request, bookmark_id: int):
|
2022-03-22 01:24:21 +00:00
|
|
|
try:
|
|
|
|
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
|
|
except Bookmark.DoesNotExist:
|
|
|
|
raise Http404('Bookmark does not exist')
|
|
|
|
|
2019-06-27 06:09:51 +00:00
|
|
|
bookmark.delete()
|
2019-07-05 20:29:21 +00:00
|
|
|
|
|
|
|
|
2021-02-14 17:00:22 +00:00
|
|
|
def archive(request, bookmark_id: int):
|
2022-03-22 01:24:21 +00:00
|
|
|
try:
|
|
|
|
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
|
|
except Bookmark.DoesNotExist:
|
|
|
|
raise Http404('Bookmark does not exist')
|
|
|
|
|
2021-02-14 17:00:22 +00:00
|
|
|
archive_bookmark(bookmark)
|
|
|
|
|
|
|
|
|
|
|
|
def unarchive(request, bookmark_id: int):
|
2022-03-22 01:24:21 +00:00
|
|
|
try:
|
|
|
|
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
|
|
except Bookmark.DoesNotExist:
|
|
|
|
raise Http404('Bookmark does not exist')
|
|
|
|
|
2021-02-14 17:00:22 +00:00
|
|
|
unarchive_bookmark(bookmark)
|
2019-07-05 20:29:21 +00:00
|
|
|
|
|
|
|
|
2023-08-24 17:11:36 +00:00
|
|
|
def unshare(request, bookmark_id: int):
|
|
|
|
try:
|
|
|
|
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
|
|
except Bookmark.DoesNotExist:
|
|
|
|
raise Http404('Bookmark does not exist')
|
|
|
|
|
|
|
|
bookmark.shared = False
|
|
|
|
bookmark.save()
|
|
|
|
|
|
|
|
|
2022-07-23 20:17:20 +00:00
|
|
|
def mark_as_read(request, bookmark_id: int):
|
|
|
|
try:
|
|
|
|
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
|
|
except Bookmark.DoesNotExist:
|
|
|
|
raise Http404('Bookmark does not exist')
|
|
|
|
|
|
|
|
bookmark.unread = False
|
|
|
|
bookmark.save()
|
|
|
|
|
|
|
|
|
2021-03-28 22:43:50 +00:00
|
|
|
@login_required
|
2023-08-25 11:54:23 +00:00
|
|
|
def index_action(request):
|
|
|
|
filters = BookmarkFilters(request)
|
|
|
|
query = queries.query_bookmarks(request.user, request.user_profile, filters.query)
|
|
|
|
return action(request, query)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def archived_action(request):
|
|
|
|
filters = BookmarkFilters(request)
|
|
|
|
query = queries.query_archived_bookmarks(request.user, request.user_profile, filters.query)
|
|
|
|
return action(request, query)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def shared_action(request):
|
|
|
|
return action(request)
|
|
|
|
|
|
|
|
|
|
|
|
def action(request, query: QuerySet[Bookmark] = None):
|
|
|
|
# Single bookmark actions
|
2022-03-27 08:56:09 +00:00
|
|
|
if 'archive' in request.POST:
|
|
|
|
archive(request, request.POST['archive'])
|
|
|
|
if 'unarchive' in request.POST:
|
|
|
|
unarchive(request, request.POST['unarchive'])
|
|
|
|
if 'remove' in request.POST:
|
|
|
|
remove(request, request.POST['remove'])
|
2022-07-23 20:17:20 +00:00
|
|
|
if 'mark_as_read' in request.POST:
|
|
|
|
mark_as_read(request, request.POST['mark_as_read'])
|
2023-08-24 17:11:36 +00:00
|
|
|
if 'unshare' in request.POST:
|
|
|
|
unshare(request, request.POST['unshare'])
|
2023-08-25 11:54:23 +00:00
|
|
|
|
|
|
|
# Bulk actions
|
|
|
|
if 'bulk_execute' in request.POST:
|
|
|
|
if query is None:
|
|
|
|
return HttpResponseBadRequest('View does not support bulk actions')
|
|
|
|
|
|
|
|
bulk_action = request.POST['bulk_action']
|
|
|
|
|
|
|
|
# Determine set of bookmarks
|
|
|
|
if request.POST.get('bulk_select_across') == 'on':
|
|
|
|
# Query full list of bookmarks across all pages
|
|
|
|
bookmark_ids = query.only('id').values_list('id', flat=True)
|
|
|
|
else:
|
|
|
|
# Use only selected bookmarks
|
|
|
|
bookmark_ids = request.POST.getlist('bookmark_id')
|
|
|
|
|
|
|
|
if 'bulk_archive' == bulk_action:
|
|
|
|
archive_bookmarks(bookmark_ids, request.user)
|
|
|
|
if 'bulk_unarchive' == bulk_action:
|
|
|
|
unarchive_bookmarks(bookmark_ids, request.user)
|
|
|
|
if 'bulk_delete' == bulk_action:
|
|
|
|
delete_bookmarks(bookmark_ids, request.user)
|
|
|
|
if 'bulk_tag' == bulk_action:
|
|
|
|
tag_string = convert_tag_string(request.POST['bulk_tag_string'])
|
|
|
|
tag_bookmarks(bookmark_ids, tag_string, request.user)
|
|
|
|
if 'bulk_untag' == bulk_action:
|
|
|
|
tag_string = convert_tag_string(request.POST['bulk_tag_string'])
|
|
|
|
untag_bookmarks(bookmark_ids, tag_string, request.user)
|
|
|
|
if 'bulk_read' == bulk_action:
|
|
|
|
mark_bookmarks_as_read(bookmark_ids, request.user)
|
|
|
|
if 'bulk_unread' == bulk_action:
|
|
|
|
mark_bookmarks_as_unread(bookmark_ids, request.user)
|
|
|
|
if 'bulk_share' == bulk_action:
|
|
|
|
share_bookmarks(bookmark_ids, request.user)
|
|
|
|
if 'bulk_unshare' == bulk_action:
|
|
|
|
unshare_bookmarks(bookmark_ids, request.user)
|
2021-03-28 22:43:50 +00:00
|
|
|
|
2022-03-25 17:29:54 +00:00
|
|
|
return_url = get_safe_return_url(request.GET.get('return_url'), reverse('bookmarks:index'))
|
2021-03-28 22:43:50 +00:00
|
|
|
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')
|