#26 Return to same page after editing or deleting bookmark

This commit is contained in:
Sascha Ißbrücker 2020-09-13 10:56:03 +02:00
parent 3373667c72
commit c80f26dd34
8 changed files with 55 additions and 16 deletions

View File

@ -77,7 +77,9 @@ class BookmarkForm(forms.ModelForm):
widget=forms.Textarea()) widget=forms.Textarea())
# Hidden field that determines whether to close window/tab after saving the bookmark # Hidden field that determines whether to close window/tab after saving the bookmark
auto_close = forms.CharField(required=False) auto_close = forms.CharField(required=False)
# Hidden field that determines where to redirect after saving the form
return_url = forms.CharField(required=False)
class Meta: class Meta:
model = Bookmark model = Bookmark
fields = ['url', 'tag_string', 'title', 'description', 'auto_close'] fields = ['url', 'tag_string', 'title', 'description', 'auto_close', 'return_url']

View File

@ -21,9 +21,9 @@
{% endif %} {% endif %}
</div> </div>
<div class="actions"> <div class="actions">
<a href="{% url 'bookmarks:edit' bookmark.id %}" <a href="{% url 'bookmarks:edit' bookmark.id %}?return_url={{ return_url }}"
class="btn btn-link btn-sm">Edit</a> class="btn btn-link btn-sm">Edit</a>
<a href="{% url 'bookmarks:remove' bookmark.id %}" <a href="{% url 'bookmarks:remove' bookmark.id %}?return_url={{ return_url }}"
class="btn btn-link btn-sm" class="btn btn-link btn-sm"
onclick="return confirm('Do you really want to delete this bookmark?')">Remove</a> onclick="return confirm('Do you really want to delete this bookmark?')">Remove</a>
</div> </div>

View File

@ -8,7 +8,7 @@
<h2>Edit bookmark</h2> <h2>Edit bookmark</h2>
</div> </div>
<form action="{% url 'bookmarks:edit' bookmark_id %}" method="post" class="col-6 col-md-12" novalidate> <form action="{% url 'bookmarks:edit' bookmark_id %}" method="post" class="col-6 col-md-12" novalidate>
{% bookmark_form form all_tags bookmark_id %} {% bookmark_form form all_tags return_url bookmark_id %}
</form> </form>
</section> </section>
</div> </div>

View File

@ -4,6 +4,7 @@
<div class="bookmarks-form"> <div class="bookmarks-form">
{% csrf_token %} {% csrf_token %}
{{ form.auto_close|attr:"type:hidden" }} {{ form.auto_close|attr:"type:hidden" }}
{{ form.return_url|attr:"type:hidden" }}
<div class="form-group {% if form.url.errors %}has-error{% endif %}"> <div class="form-group {% if form.url.errors %}has-error{% endif %}">
<label for="{{ form.url.id_for_label }}" class="form-label">URL</label> <label for="{{ form.url.id_for_label }}" class="form-label">URL</label>
{{ form.url|add_class:"form-input"|attr:"autofocus" }} {{ form.url|add_class:"form-input"|attr:"autofocus" }}
@ -55,7 +56,7 @@
{% else %} {% else %}
<input type="submit" value="Save" class="btn btn-primary mr-2"> <input type="submit" value="Save" class="btn btn-primary mr-2">
{% endif %} {% endif %}
<a href="{% url 'bookmarks:index' %}" class="btn">Nevermind</a> <a href="{{ cancel_url }}" class="btn">Nevermind</a>
</div> </div>
{# Replace tag input with auto-complete component #} {# Replace tag input with auto-complete component #}

View File

@ -23,7 +23,7 @@
{% if empty %} {% if empty %}
{% include 'bookmarks/empty_bookmarks.html' %} {% include 'bookmarks/empty_bookmarks.html' %}
{% else %} {% else %}
{% bookmark_list bookmarks %} {% bookmark_list bookmarks return_url %}
{% endif %} {% endif %}
</section> </section>

View File

@ -8,7 +8,7 @@
<h2>New bookmark</h2> <h2>New bookmark</h2>
</div> </div>
<form action="{% url 'bookmarks:new' %}" method="post" class="col-6 col-md-12" novalidate> <form action="{% url 'bookmarks:new' %}" method="post" class="col-6 col-md-12" novalidate>
{% bookmark_form form all_tags auto_close=auto_close %} {% bookmark_form form all_tags return_url auto_close=auto_close %}
</form> </form>
</section> </section>
</div> </div>

View File

@ -9,7 +9,7 @@ register = template.Library()
@register.inclusion_tag('bookmarks/form.html', name='bookmark_form') @register.inclusion_tag('bookmarks/form.html', name='bookmark_form')
def bookmark_form(form: BookmarkForm, all_tags: List[Tag], bookmark_id: int = 0, auto_close: bool = False): def bookmark_form(form: BookmarkForm, all_tags: List[Tag], cancel_url: str, bookmark_id: int = 0, auto_close: bool = False):
all_tag_names = [tag.name for tag in all_tags] all_tag_names = [tag.name for tag in all_tags]
all_tags_string = build_tag_string(all_tag_names, ' ') all_tags_string = build_tag_string(all_tag_names, ' ')
@ -18,7 +18,8 @@ def bookmark_form(form: BookmarkForm, all_tags: List[Tag], bookmark_id: int = 0,
'form': form, 'form': form,
'auto_close': auto_close, 'auto_close': auto_close,
'all_tags': all_tags_string, 'all_tags': all_tags_string,
'bookmark_id': bookmark_id 'bookmark_id': bookmark_id,
'cancel_url': cancel_url
} }
@ -55,7 +56,8 @@ def tag_cloud(context, tags: List[Tag]):
@register.inclusion_tag('bookmarks/bookmark_list.html', name='bookmark_list', takes_context=True) @register.inclusion_tag('bookmarks/bookmark_list.html', name='bookmark_list', takes_context=True)
def bookmark_list(context, bookmarks: Page): def bookmark_list(context, bookmarks: Page, return_url: str):
return { return {
'bookmarks': bookmarks, 'bookmarks': bookmarks,
'return_url': return_url
} }

View File

@ -1,3 +1,5 @@
import urllib.parse
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
@ -20,6 +22,7 @@ def index(request):
paginator = Paginator(query_set, _default_page_size) paginator = Paginator(query_set, _default_page_size)
bookmarks = paginator.get_page(page) bookmarks = paginator.get_page(page)
tags = queries.query_tags(request.user, query_string) tags = queries.query_tags(request.user, query_string)
return_url = generate_index_return_url(page, query_string)
if request.GET.get('tag'): if request.GET.get('tag'):
mod = request.GET.copy() mod = request.GET.copy()
@ -30,11 +33,24 @@ def index(request):
'bookmarks': bookmarks, 'bookmarks': bookmarks,
'tags': tags, 'tags': tags,
'query': query_string if query_string else '', 'query': query_string if query_string else '',
'empty': paginator.count == 0 'empty': paginator.count == 0,
'return_url': return_url
} }
return render(request, 'bookmarks/index.html', context) return render(request, 'bookmarks/index.html', context)
def generate_index_return_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
base_url = reverse('bookmarks:index')
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)
@login_required @login_required
def new(request): def new(request):
initial_url = request.GET.get('url') initial_url = request.GET.get('url')
@ -58,7 +74,12 @@ def new(request):
form.initial['auto_close'] = 'true' form.initial['auto_close'] = 'true'
all_tags = get_user_tags(request.user) all_tags = get_user_tags(request.user)
context = {'form': form, 'auto_close': initial_auto_close, 'all_tags': all_tags} context = {
'form': form,
'auto_close': initial_auto_close,
'all_tags': all_tags,
'return_url': reverse('bookmarks:index')
}
return render(request, 'bookmarks/new.html', context) return render(request, 'bookmarks/new.html', context)
@ -66,18 +87,29 @@ def new(request):
@login_required @login_required
def edit(request, bookmark_id: int): def edit(request, bookmark_id: int):
bookmark = Bookmark.objects.get(pk=bookmark_id) bookmark = Bookmark.objects.get(pk=bookmark_id)
if request.method == 'POST': if request.method == 'POST':
form = BookmarkForm(request.POST, instance=bookmark) form = BookmarkForm(request.POST, instance=bookmark)
return_url = form.data['return_url']
if form.is_valid(): if form.is_valid():
update_bookmark(form, request.user) update_bookmark(form, request.user)
return HttpResponseRedirect(reverse('bookmarks:index')) return HttpResponseRedirect(return_url)
else: else:
return_url = request.GET.get('return_url')
form = BookmarkForm(instance=bookmark) form = BookmarkForm(instance=bookmark)
form.initial['tag_string'] = build_tag_string(bookmark.tag_names, ' ') return_url = return_url if return_url else reverse('bookmarks:index')
form.initial['tag_string'] = build_tag_string(bookmark.tag_names, ' ')
form.initial['return_url'] = return_url
all_tags = get_user_tags(request.user) all_tags = get_user_tags(request.user)
context = {'form': form, 'bookmark_id': bookmark_id, 'all_tags': all_tags}
context = {
'form': form,
'bookmark_id': bookmark_id,
'all_tags': all_tags,
'return_url': return_url
}
return render(request, 'bookmarks/edit.html', context) return render(request, 'bookmarks/edit.html', context)
@ -86,7 +118,9 @@ def edit(request, bookmark_id: int):
def remove(request, bookmark_id: int): def remove(request, bookmark_id: int):
bookmark = Bookmark.objects.get(pk=bookmark_id) bookmark = Bookmark.objects.get(pk=bookmark_id)
bookmark.delete() bookmark.delete()
return HttpResponseRedirect(reverse('bookmarks:index')) return_url = request.GET.get('return_url')
return_url = return_url if return_url else reverse('bookmarks:index')
return HttpResponseRedirect(return_url)
@login_required @login_required