linkding/bookmarks/views.py

49 lines
1.5 KiB
Python
Raw Normal View History

2019-06-28 17:37:41 +00:00
from django.http import HttpResponseRedirect, HttpRequest
2019-06-27 06:09:51 +00:00
from django.shortcuts import render
from django.urls import reverse
2019-06-28 22:27:20 +00:00
from bookmarks.services.bookmarks import create_bookmark, update_bookmark
2019-06-28 17:37:41 +00:00
from . import forms
2019-06-27 06:09:51 +00:00
from .models import Bookmark
def index(request):
context = {
'bookmarks': Bookmark.objects.all()
}
return render(request, 'bookmarks/index.html', context)
2019-06-28 17:37:41 +00:00
def new(request: HttpRequest):
if request.method == 'POST':
form = forms.BookmarkForm(request.POST)
if form.is_valid():
bookmark = form.save(commit=False)
current_user = request.user
create_bookmark(bookmark, current_user)
return HttpResponseRedirect(reverse('bookmarks:index'))
else:
form = forms.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
def edit(request, bookmark_id):
2019-06-28 22:27:20 +00:00
bookmark = Bookmark.objects.get(pk=bookmark_id)
if request.method == 'POST':
form = forms.BookmarkForm(request.POST, instance=bookmark)
if form.is_valid():
bookmark = form.save(commit=False)
update_bookmark(bookmark)
return HttpResponseRedirect(reverse('bookmarks:index'))
else:
form = forms.BookmarkForm(instance=bookmark)
return render(request, 'bookmarks/edit.html', {'form': form, 'bookmark_id': bookmark_id})
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'))