Favorite/Unfavorite Views

This commit is contained in:
reverend 2020-12-24 15:56:33 +01:00
parent f974469996
commit 8f048369bf
2 changed files with 40 additions and 1 deletions

View File

@ -11,6 +11,8 @@ from lostplaces.views import (
PlaceDeleteView,
PlaceTagDeleteView,
PlaceTagSubmitView,
PlaceFavoriteView,
PlaceUnfavoriteView,
PhotoAlbumCreateView,
PhotoAlbumDeleteView,
PlaceImageCreateView,
@ -36,5 +38,10 @@ urlpatterns = [
path('place/tag/<int:tagged_id>', PlaceTagSubmitView.as_view(), name='place_tag_submit'),
path('place/tag/delete/<int:tagged_id>/<int:tag_id>', PlaceTagDeleteView.as_view(), name='place_tag_delete'),
path('explorer/<int:explorer_id>/', ExplorerProfileView.as_view(), name='explorer_profile')
path('explorer/<int:explorer_id>/', ExplorerProfileView.as_view(), name='explorer_profile'),
path('explorer/favorite/<int:place_id>/', PlaceFavoriteView.as_view(), name='place_favorite'),
path('explorer/unfavorite/<int:place_id>/', PlaceUnfavoriteView.as_view(), name='place_favorite')
]

View File

@ -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})
)