From 8f048369bfe2031859ad7e6616fee70264e84bcf Mon Sep 17 00:00:00 2001 From: reverend Date: Thu, 24 Dec 2020 15:56:33 +0100 Subject: [PATCH] Favorite/Unfavorite Views --- django_lostplaces/lostplaces/urls.py | 9 +++++- .../lostplaces/views/place_views.py | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/django_lostplaces/lostplaces/urls.py b/django_lostplaces/lostplaces/urls.py index 2796ed9..80138ce 100644 --- a/django_lostplaces/lostplaces/urls.py +++ b/django_lostplaces/lostplaces/urls.py @@ -11,6 +11,8 @@ from lostplaces.views import ( PlaceDeleteView, PlaceTagDeleteView, PlaceTagSubmitView, + PlaceFavoriteView, + PlaceUnfavoriteView, PhotoAlbumCreateView, PhotoAlbumDeleteView, PlaceImageCreateView, @@ -36,5 +38,10 @@ urlpatterns = [ path('place/tag/', PlaceTagSubmitView.as_view(), name='place_tag_submit'), path('place/tag/delete//', PlaceTagDeleteView.as_view(), name='place_tag_delete'), - path('explorer//', ExplorerProfileView.as_view(), name='explorer_profile') + path('explorer//', ExplorerProfileView.as_view(), name='explorer_profile'), + + path('explorer/favorite//', PlaceFavoriteView.as_view(), name='place_favorite'), + path('explorer/unfavorite//', PlaceUnfavoriteView.as_view(), name='place_favorite') + + ] diff --git a/django_lostplaces/lostplaces/views/place_views.py b/django_lostplaces/lostplaces/views/place_views.py index 137f706..ea078bf 100644 --- a/django_lostplaces/lostplaces/views/place_views.py +++ b/django_lostplaces/lostplaces/views/place_views.py @@ -119,3 +119,35 @@ class PlaceDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, DeleteView): def get_place(self): return self.get_object() + +class PlaceFavoriteView(IsAuthenticatedMixin, View): + + def get(self, request, place_id): + place = get_object_or_404(Place, id=place_id) + 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}) + ) + +class PlaceUnfavoriteView(IsAuthenticatedMixin, View): + + def get(self, request, place_id): + place = get_object_or_404(Place, id=place_id) + if request.user is not None: + 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