From 04fe93c7bb185693b9dbb08c4e1cf876cbe763a0 Mon Sep 17 00:00:00 2001 From: reverend Date: Fri, 25 Dec 2020 12:57:05 +0100 Subject: [PATCH] #45 Redirect after asset deletion --- django_lostplaces/lostplaces/common.py | 16 +++++++++++++++- .../lostplaces/views/base_views.py | 3 ++- .../lostplaces/views/place_views.py | 19 ++++--------------- django_lostplaces/lostplaces/views/views.py | 6 ++++-- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/django_lostplaces/lostplaces/common.py b/django_lostplaces/lostplaces/common.py index 1b438f9..979091f 100644 --- a/django_lostplaces/lostplaces/common.py +++ b/django_lostplaces/lostplaces/common.py @@ -1,3 +1,5 @@ +from django.shortcuts import redirect + def get_all_subclasses(cls): ''' Gets all subclasses recursively, does not contain @@ -8,4 +10,16 @@ def get_all_subclasses(cls): if not subclass._meta.abstract: subclass_list.append(subclass) subclass_list += get_all_subclasses(subclass) - return subclass_list \ No newline at end of file + return subclass_list + +def redirect_referer_or(request, url='/'): + ''' + Returns a django redirect to the requests referer, + if there is no referer the redirect will poin to the given url + Default url is / + ''' + referer = request.META.get('HTTP_REFERER') + if referer is not None: + return redirect(referer) + else: + return redirect(url) \ No newline at end of file diff --git a/django_lostplaces/lostplaces/views/base_views.py b/django_lostplaces/lostplaces/views/base_views.py index 346f6d4..ee09075 100644 --- a/django_lostplaces/lostplaces/views/base_views.py +++ b/django_lostplaces/lostplaces/views/base_views.py @@ -14,6 +14,7 @@ from django.urls import reverse_lazy from django.utils.translation import ugettext_lazy as _ from lostplaces.models import Place +from lostplaces.common import redirect_referer_or class IsAuthenticatedMixin(LoginRequiredMixin, View): ''' @@ -108,4 +109,4 @@ class PlaceAssetDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, SingleOb place_id = self.get_object().place.id self.get_object().delete() messages.success(self.request, self.success_message) - return redirect(reverse_lazy('place_detail', kwargs={'pk': place_id})) + return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place_id})) diff --git a/django_lostplaces/lostplaces/views/place_views.py b/django_lostplaces/lostplaces/views/place_views.py index 0d3ecb4..11239d8 100644 --- a/django_lostplaces/lostplaces/views/place_views.py +++ b/django_lostplaces/lostplaces/views/place_views.py @@ -19,6 +19,7 @@ from lostplaces.models import Place, PlaceImage from lostplaces.views.base_views import IsAuthenticatedMixin, IsPlaceSubmitterMixin from lostplaces.views.place_image_views import MultiplePlaceImageUploadMixin from lostplaces.forms import PlaceForm, PlaceImageForm, TagSubmitForm +from lostplaces.common import redirect_referer_or from taggit.models import Tag @@ -134,14 +135,8 @@ class PlaceFavoriteView(IsAuthenticatedMixin, View): if request.user is not None: request.user.explorer.favorite_places.add(place) request.user.explorer.save() - - referer = request.META.get('HTTP_REFERER') - if referer is not None: - return redirect(referer) - else: - return redirect( - reverse_lazy('place_detail', kwargs={'pk': place.pk}) - ) + + return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place.pk})) class PlaceUnfavoriteView(IsAuthenticatedMixin, View): @@ -151,10 +146,4 @@ class PlaceUnfavoriteView(IsAuthenticatedMixin, View): request.user.explorer.favorite_places.remove(place) request.user.explorer.save() - referer = request.META.get('HTTP_REFERER') - if referer is not None: - return redirect(referer) - else: - return redirect( - reverse_lazy('place_detail', kwargs={'pk': place.pk}) - ) \ No newline at end of file + return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place.pk})) \ No newline at end of file diff --git a/django_lostplaces/lostplaces/views/views.py b/django_lostplaces/lostplaces/views/views.py index 03982ad..793a540 100644 --- a/django_lostplaces/lostplaces/views/views.py +++ b/django_lostplaces/lostplaces/views/views.py @@ -14,6 +14,7 @@ from django.utils.translation import ugettext_lazy as _ from lostplaces.forms import ExplorerCreationForm, TagSubmitForm from lostplaces.models import Place, PhotoAlbum from lostplaces.views.base_views import IsAuthenticatedMixin +from lostplaces.common import redirect_referer_or from lostplaces.views.base_views import ( PlaceAssetCreateView, @@ -79,8 +80,9 @@ class PlaceTagDeleteView(IsAuthenticatedMixin, View): place = get_object_or_404(Place, pk=tagged_id) tag = get_object_or_404(Tag, pk=tag_id) place.tags.remove(tag) - return redirect(reverse_lazy('place_detail', kwargs={'pk': tagged_id})) - + + return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': tagged_id})) + def FlatView(request, slug): if request.LANGUAGE_CODE == 'de': return render(request, 'flat/' + slug + '-de' + '.html')