Added UserTestMixin to PlaceDeleteView.

This commit is contained in:
Marcus Scholz 2020-08-12 17:18:31 +02:00
parent 5bc92a5841
commit aff5babb20

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