Merge branch 'master' of mowoe.com:reverend/lostplaces-backend

This commit is contained in:
reverend 2020-08-12 17:51:24 +02:00
commit 9dfd389a5d

View File

@ -7,6 +7,8 @@ from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views import View from django.views import View
from django.http import Http404 from django.http import Http404
from django.contrib import messages
from django.contrib.auth.mixins import UserPassesTestMixin
from .forms import ( from .forms import (
ExplorerCreationForm, ExplorerCreationForm,
@ -95,9 +97,22 @@ class PlaceCreateView(View):
) )
place_image.save() place_image.save()
class PlaceDeleteView(DeleteView): class PlaceDeleteView(UserPassesTestMixin, DeleteView):
template_name = 'place/place_delete.html' template_name = 'place/place_delete.html'
model = Place model = Place
success_url = reverse_lazy('place_list')
def test_func(self):
""" Check if user is eligible to delete place. """
if self.request.user.is_superuser:
return True
place_obj = self.get_object()
def get_success_url(self): # Check if currently logged in user was the submitter
return reverse_lazy('place_list') if self.request.user == place_obj.submitted_by:
return True
messages.error(
self.request, 'You do not have permission to delete this place.')
return False