From 263416a4017588150ddb5e3725ec1588892a7718 Mon Sep 17 00:00:00 2001 From: reverend Date: Sat, 26 Sep 2020 16:08:37 +0200 Subject: [PATCH] Abstraction for PlaceImageCreation (multiple images) --- .../lostplaces/views/place_image_views.py | 43 +++++++++++++++---- .../lostplaces/views/place_views.py | 21 ++------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/django_lostplaces/lostplaces/views/place_image_views.py b/django_lostplaces/lostplaces/views/place_image_views.py index d9f7d3c..4b4ce58 100644 --- a/django_lostplaces/lostplaces/views/place_image_views.py +++ b/django_lostplaces/lostplaces/views/place_image_views.py @@ -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() \ No newline at end of file + 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' + + \ No newline at end of file diff --git a/django_lostplaces/lostplaces/views/place_views.py b/django_lostplaces/lostplaces/views/place_views.py index d3d99c3..3240e10 100644 --- a/django_lostplaces/lostplaces/views/place_views.py +++ b/django_lostplaces/lostplaces/views/place_views.py @@ -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