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
|
2019-06-28 22:27:20 +00:00
|
|
|
from bookmarks.services.bookmarks import create_bookmark, update_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-06-27 06:09:51 +00:00
|
|
|
def index(request):
|
2019-06-29 07:15:07 +00:00
|
|
|
page = request.GET.get('page')
|
2019-06-29 10:53:37 +00:00
|
|
|
query_string = request.GET.get('q')
|
|
|
|
query_set = queries.query_bookmarks(request.user, query_string)
|
|
|
|
paginator = Paginator(query_set, _default_page_size)
|
2019-06-29 07:15:07 +00:00
|
|
|
bookmarks = paginator.get_page(page)
|
2019-06-30 19:15:02 +00:00
|
|
|
tags = queries.query_tags(request.user, 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
|
|
|
|
|
2019-06-27 06:09:51 +00:00
|
|
|
context = {
|
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 '',
|
2019-06-27 06:09:51 +00:00
|
|
|
}
|
|
|
|
return render(request, 'bookmarks/index.html', context)
|
|
|
|
|
|
|
|
|
2019-07-02 07:34:12 +00:00
|
|
|
@login_required
|
2019-06-29 10:53:37 +00:00
|
|
|
def new(request):
|
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-06-28 17:37:41 +00:00
|
|
|
if form.is_valid():
|
|
|
|
current_user = request.user
|
2019-07-01 20:05:38 +00:00
|
|
|
create_bookmark(form, current_user)
|
2019-06-28 17:37:41 +00:00
|
|
|
return HttpResponseRedirect(reverse('bookmarks:index'))
|
|
|
|
else:
|
2019-06-28 23:08:22 +00:00
|
|
|
form = BookmarkForm()
|
2019-06-27 06:09:51 +00:00
|
|
|
|
2019-06-28 17:37:41 +00:00
|
|
|
return render(request, 'bookmarks/new.html', {'form': form})
|
2019-06-28 05:33:08 +00:00
|
|
|
|
|
|
|
|
2019-07-02 07:34:12 +00:00
|
|
|
@login_required
|
2019-06-29 10:53:37 +00:00
|
|
|
def edit(request, bookmark_id: int):
|
2019-06-28 22:27:20 +00:00
|
|
|
bookmark = Bookmark.objects.get(pk=bookmark_id)
|
|
|
|
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():
|
2019-07-01 20:05:38 +00:00
|
|
|
update_bookmark(form, request.user)
|
2019-06-28 22:27:20 +00:00
|
|
|
return HttpResponseRedirect(reverse('bookmarks:index'))
|
|
|
|
else:
|
2019-06-28 23:08:22 +00:00
|
|
|
form = BookmarkForm(instance=bookmark)
|
2019-06-28 22:27:20 +00:00
|
|
|
|
2019-07-01 20:05:38 +00:00
|
|
|
form.initial['tag_string'] = build_tag_string(bookmark.tag_names, ' ')
|
2019-06-28 22:27:20 +00:00
|
|
|
return render(request, 'bookmarks/edit.html', {'form': form, 'bookmark_id': bookmark_id})
|
2019-06-27 06:09:51 +00:00
|
|
|
|
|
|
|
|
2019-07-02 07:34:12 +00:00
|
|
|
@login_required
|
2019-06-27 06:09:51 +00:00
|
|
|
def remove(request, bookmark_id: int):
|
|
|
|
bookmark = Bookmark.objects.get(pk=bookmark_id)
|
|
|
|
bookmark.delete()
|
|
|
|
return HttpResponseRedirect(reverse('bookmarks:index'))
|