From c80f26dd34d8d1d0bd695eba60e0f1165cbe07cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Sun, 13 Sep 2020 10:56:03 +0200 Subject: [PATCH] #26 Return to same page after editing or deleting bookmark --- bookmarks/models.py | 4 +- .../templates/bookmarks/bookmark_list.html | 4 +- bookmarks/templates/bookmarks/edit.html | 2 +- bookmarks/templates/bookmarks/form.html | 3 +- bookmarks/templates/bookmarks/index.html | 2 +- bookmarks/templates/bookmarks/new.html | 2 +- bookmarks/templatetags/bookmarks.py | 8 ++-- bookmarks/views/bookmarks.py | 46 ++++++++++++++++--- 8 files changed, 55 insertions(+), 16 deletions(-) diff --git a/bookmarks/models.py b/bookmarks/models.py index e3305e6..3906572 100644 --- a/bookmarks/models.py +++ b/bookmarks/models.py @@ -77,7 +77,9 @@ class BookmarkForm(forms.ModelForm): widget=forms.Textarea()) # Hidden field that determines whether to close window/tab after saving the bookmark 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: model = Bookmark - fields = ['url', 'tag_string', 'title', 'description', 'auto_close'] + fields = ['url', 'tag_string', 'title', 'description', 'auto_close', 'return_url'] diff --git a/bookmarks/templates/bookmarks/bookmark_list.html b/bookmarks/templates/bookmarks/bookmark_list.html index b9578bb..a60c8f0 100644 --- a/bookmarks/templates/bookmarks/bookmark_list.html +++ b/bookmarks/templates/bookmarks/bookmark_list.html @@ -21,9 +21,9 @@ {% endif %}
- Edit - Remove
diff --git a/bookmarks/templates/bookmarks/edit.html b/bookmarks/templates/bookmarks/edit.html index 6ebad75..875769e 100644 --- a/bookmarks/templates/bookmarks/edit.html +++ b/bookmarks/templates/bookmarks/edit.html @@ -8,7 +8,7 @@

Edit bookmark

- {% bookmark_form form all_tags bookmark_id %} + {% bookmark_form form all_tags return_url bookmark_id %}
diff --git a/bookmarks/templates/bookmarks/form.html b/bookmarks/templates/bookmarks/form.html index aaa7bbc..c521b0b 100644 --- a/bookmarks/templates/bookmarks/form.html +++ b/bookmarks/templates/bookmarks/form.html @@ -4,6 +4,7 @@
{% csrf_token %} {{ form.auto_close|attr:"type:hidden" }} + {{ form.return_url|attr:"type:hidden" }}
{{ form.url|add_class:"form-input"|attr:"autofocus" }} @@ -55,7 +56,7 @@ {% else %} {% endif %} - Nevermind + Nevermind
{# Replace tag input with auto-complete component #} diff --git a/bookmarks/templates/bookmarks/index.html b/bookmarks/templates/bookmarks/index.html index c24ac4b..e4136b8 100644 --- a/bookmarks/templates/bookmarks/index.html +++ b/bookmarks/templates/bookmarks/index.html @@ -23,7 +23,7 @@ {% if empty %} {% include 'bookmarks/empty_bookmarks.html' %} {% else %} - {% bookmark_list bookmarks %} + {% bookmark_list bookmarks return_url %} {% endif %} diff --git a/bookmarks/templates/bookmarks/new.html b/bookmarks/templates/bookmarks/new.html index 9399fff..67aa91d 100644 --- a/bookmarks/templates/bookmarks/new.html +++ b/bookmarks/templates/bookmarks/new.html @@ -8,7 +8,7 @@

New bookmark

- {% bookmark_form form all_tags auto_close=auto_close %} + {% bookmark_form form all_tags return_url auto_close=auto_close %}
diff --git a/bookmarks/templatetags/bookmarks.py b/bookmarks/templatetags/bookmarks.py index 98ea43b..80d76f4 100644 --- a/bookmarks/templatetags/bookmarks.py +++ b/bookmarks/templatetags/bookmarks.py @@ -9,7 +9,7 @@ register = template.Library() @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_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, 'auto_close': auto_close, '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) -def bookmark_list(context, bookmarks: Page): +def bookmark_list(context, bookmarks: Page, return_url: str): return { 'bookmarks': bookmarks, + 'return_url': return_url } diff --git a/bookmarks/views/bookmarks.py b/bookmarks/views/bookmarks.py index 44341c2..1a43a9a 100644 --- a/bookmarks/views/bookmarks.py +++ b/bookmarks/views/bookmarks.py @@ -1,3 +1,5 @@ +import urllib.parse + from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator from django.http import HttpResponseRedirect @@ -20,6 +22,7 @@ def index(request): paginator = Paginator(query_set, _default_page_size) bookmarks = paginator.get_page(page) tags = queries.query_tags(request.user, query_string) + return_url = generate_index_return_url(page, query_string) if request.GET.get('tag'): mod = request.GET.copy() @@ -30,11 +33,24 @@ def index(request): 'bookmarks': bookmarks, 'tags': tags, '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) +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 def new(request): initial_url = request.GET.get('url') @@ -58,7 +74,12 @@ def new(request): form.initial['auto_close'] = 'true' 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) @@ -66,18 +87,29 @@ def new(request): @login_required def edit(request, bookmark_id: int): bookmark = Bookmark.objects.get(pk=bookmark_id) + if request.method == 'POST': form = BookmarkForm(request.POST, instance=bookmark) + return_url = form.data['return_url'] if form.is_valid(): update_bookmark(form, request.user) - return HttpResponseRedirect(reverse('bookmarks:index')) + return HttpResponseRedirect(return_url) else: + return_url = request.GET.get('return_url') 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) - 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) @@ -86,7 +118,9 @@ def edit(request, bookmark_id: int): def remove(request, bookmark_id: int): bookmark = Bookmark.objects.get(pk=bookmark_id) 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