base views for place assets

This commit is contained in:
reverend 2020-08-30 17:26:43 +02:00
parent 538b43c2a1
commit 7a3b8529f8
2 changed files with 69 additions and 46 deletions

View File

@ -1,7 +1,15 @@
from django.views import View from django.views import View
from django.views.generic.edit import CreateView
from django.views.generic.detail import SingleObjectMixin
from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.shortcuts import redirect
from django.urls import reverse_lazy
from lostplaces_app.models import Place
class IsAuthenticated(LoginRequiredMixin, View): class IsAuthenticated(LoginRequiredMixin, View):
redirect_field_name = 'redirect_to' redirect_field_name = 'redirect_to'
@ -35,3 +43,56 @@ class IsPlaceSubmitter(UserPassesTestMixin, View):
if self.place_submitter_error_message: if self.place_submitter_error_message:
messages.error(self.request, self.place_submitter_error_message) messages.error(self.request, self.place_submitter_error_message)
return False return False
class PlaceAssetCreateView(IsAuthenticated, SuccessMessageMixin, CreateView):
model = None
fields = []
template_name = ''
success_message = ''
def get(self, request, place_id, *args, **kwargs):
self.place = Place.objects.get(pk=place_id)
return super().get(request, *args, **kwargs)
def post(self, request, place_id, *args, **kwargs):
self.place = Place.objects.get(pk=place_id)
response = super().post(request, *args, **kwargs)
self.object.place = self.place
self.object.submitted_by = request.user
self.object.save()
return response
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['place'] = self.place
return context
def get_success_url(self):
return reverse_lazy('place_detail', kwargs={'pk': self.place.id})
class PlaceAssetDeleteView(IsAuthenticated, IsPlaceSubmitter, SingleObjectMixin, View):
model = None
pk_url_kwarg = 'pk'
success_message = ''
permission_denied_message = ''
def get_place(self):
place_id = self.get_object().place.id
return Place.objects.get(pk=place_id)
def test_func(self):
can_edit_place = super().test_func()
if can_edit_place:
return True
if self.get_object().submitted_by == self.request.user:
return True
messages.error(self.request, self.permission_denied_message)
return False
def get(self, request, *args, **kwargs):
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}))

View File

@ -1,6 +1,5 @@
from django.views import View from django.views import View
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView
from django.views.generic.detail import SingleObjectMixin
from django.contrib.messages.views import SuccessMessageMixin from django.contrib.messages.views import SuccessMessageMixin
from django.contrib import messages from django.contrib import messages
@ -10,7 +9,10 @@ from django.shortcuts import render, redirect
from lostplaces_app.forms import ExplorerCreationForm from lostplaces_app.forms import ExplorerCreationForm
from lostplaces_app.models import Place, PhotoAlbum from lostplaces_app.models import Place, PhotoAlbum
from lostplaces_app.views.base_views import IsAuthenticated, IsPlaceSubmitter from lostplaces_app.views.base_views import (
PlaceAssetCreateView,
PlaceAssetDeleteView
)
class SignUpView(SuccessMessageMixin, CreateView): class SignUpView(SuccessMessageMixin, CreateView):
form_class = ExplorerCreationForm form_class = ExplorerCreationForm
success_url = reverse_lazy('login') success_url = reverse_lazy('login')
@ -27,54 +29,14 @@ class HomeView(View):
} }
return render(request, 'home.html', context) return render(request, 'home.html', context)
class PhotoAlbumCreateView(IsAuthenticated, SuccessMessageMixin, CreateView): class PhotoAlbumCreateView(PlaceAssetCreateView):
model = PhotoAlbum model = PhotoAlbum
fields = ['url', 'label'] fields = ['url', 'label']
template_name = 'photo_album/photo_album_create.html' template_name = 'photo_album/photo_album_create.html'
success_message = 'Photo Album submitted' success_message = 'Photo Album submitted'
def get(self, request, place_id, *args, **kwargs): class PhotoAlbumDeleteView(PlaceAssetDeleteView):
self.place = Place.objects.get(pk=place_id)
return super().get(request, *args, **kwargs)
def post(self, request, place_id, *args, **kwargs):
self.place = Place.objects.get(pk=place_id)
response = super().post(request, *args, **kwargs)
self.object.place = self.place
self.object.submitted_by = request.user
self.object.save()
return response
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['place'] = self.place
return context
def get_success_url(self):
return reverse_lazy('place_detail', kwargs={'pk': self.place.id})
class PhotoAlbumDeleteView(IsAuthenticated, IsPlaceSubmitter, SingleObjectMixin, View):
model = PhotoAlbum model = PhotoAlbum
pk_url_kwarg = 'pk' pk_url_kwarg = 'pk'
success_message = 'Photo Album deleted' success_message = 'Photo Album deleted'
permission_denied_messsage = 'You do not have permissions to alter this photo album'
def get_place(self):
place_id = self.get_object().place.id
return Place.objects.get(pk=place_id)
def test_func(self):
can_edit_place = super().test_func()
if can_edit_place:
return True
if self.get_object().submitted_by == self.request.user:
return True
messages.error(self.request, 'You do not have permissions to alter this photo album')
return False
def get(self, request, *args, **kwargs):
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}))