Abstraction for PlaceImageCreation (multiple images)

This commit is contained in:
reverend 2020-09-26 16:08:37 +02:00
parent b4bd86ed44
commit 263416a401
2 changed files with 38 additions and 26 deletions

View File

@ -1,18 +1,43 @@
from django.views import View
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, redirect
from lostplaces.views.base_views import PlaceAssetCreateView
from lostplaces.views.base_views import PlaceAssetCreateView, PlaceAssetDeleteView
from lostplaces.models import PlaceImage, Place
from lostplaces.forms import PlaceImageForm
class PlaceImageCreateView(PlaceAssetCreateView):
class MultiplePlaceImageUploadMixin:
def handle_place_images(self, request, place):
if request.FILES:
submitted_by = request.user.explorer
for image in request.FILES.getlist('filename'):
place_image = PlaceImage.objects.create(
filename=image,
place=place,
submitted_by=submitted_by
)
place_image.save()
class PlaceImageCreateView(MultiplePlaceImageUploadMixin, PlaceAssetCreateView):
model = PlaceImage
fields = ['filename']
form_class = PlaceImageForm
template_name = 'place_image/place_image_create.html'
success_message = 'Place Images submitted'
commit = False
def post(self, request, place_id, *args, **kwargs):
place = get_object_or_404(Place, pk=place_id)
super().post(request, place_id)
self.object.place = place
self.object = self.request.user
self.object.save()
self.place = get_object_or_404(Place, pk=place_id)
self.handle_place_images(request, self.place)
return redirect(self.get_success_url())
def form_valid(self, form):
form.instance.place = self.place
form.instance.submitted_by = self.request.user.explorer
return super().form_valid(form)
class PlaceImageDeleteView(PlaceAssetDeleteView):
model = PlaceImage
success_message = 'Images deleted successfully'
permission_denied_message = 'You\'r not allowed to delete this image'

View File

@ -13,7 +13,8 @@ from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse_lazy
from lostplaces.models import Place, PlaceImage
from lostplaces.views import IsAuthenticatedMixin, IsPlaceSubmitterMixin
from lostplaces.views.base_views import IsAuthenticatedMixin, IsPlaceSubmitterMixin
from lostplaces.views.place_image_views import MultiplePlaceImageUploadMixin
from lostplaces.forms import PlaceForm, PlaceImageForm, TagSubmitForm
from taggit.models import Tag
@ -64,7 +65,7 @@ class PlaceUpdateView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, SuccessMessag
def get_place(self):
return self.get_object()
class PlaceCreateView(IsAuthenticatedMixin, View):
class PlaceCreateView(MultiplePlaceImageUploadMixin, IsAuthenticatedMixin, View):
def get(self, request, *args, **kwargs):
place_image_form = PlaceImageForm()
@ -86,12 +87,7 @@ class PlaceCreateView(IsAuthenticatedMixin, View):
place.submitted_by = submitter
place.save()
if request.FILES:
self._apply_multipart_image_upload(
files=request.FILES.getlist('filename'),
place=place,
submitter=submitter
)
self.handle_place_images(request, place)
messages.success(
self.request,
@ -107,15 +103,6 @@ class PlaceCreateView(IsAuthenticatedMixin, View):
)
return render(request, 'place/place_create.html', context={'form': place_form})
def _apply_multipart_image_upload(self, files, place, submitter):
for image in files:
place_image = PlaceImage.objects.create(
filename=image,
place=place,
submitted_by=submitter
)
place_image.save()
class PlaceDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, DeleteView):
template_name = 'place/place_delete.html'
model = Place