Refactoring

This commit is contained in:
Leonhard Strohmidel 2020-07-30 14:26:52 +02:00
parent 4e9f916169
commit 29245d1e5f

View File

@ -26,11 +26,13 @@ class PlaceEditView(View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
place_image_form = PlaceImageCreateForm() place_image_form = PlaceImageCreateForm()
if 'pk' in kwargs: if 'pk' in kwargs:
place = get_object_or_404(Place,pk=kwargs['pk']) place = get_object_or_404(Place,pk=kwargs['pk'])
place_form = PlaceForm(instance=place) place_form = PlaceForm(instance=place)
else: else:
place_form = PlaceForm() place_form = PlaceForm()
context = { context = {
'place_form': place_form, 'place_form': place_form,
'place_image_form': place_image_form 'place_image_form': place_image_form
@ -42,22 +44,20 @@ class PlaceEditView(View):
if place_form.is_valid(): if place_form.is_valid():
submitter = request.user submitter = request.user
instance = place_form.save(commit=False) place = place_form.save(commit=False)
# Save logged in user as "submitted_by" # Save logged in user as "submitted_by"
instance.submitted_by = submitter place.submitted_by = submitter
instance.save() place.save()
if request.FILES: if request.FILES:
for image in request.FILES.getlist('filename'): self._apply_multipart_image_upload(
place_image = PlaceImage.objects.create( request.FILES.getlist('filename'),
filename=image, place,
place=instance, submitter
submitted_by=submitter
) )
place_image.save()
kwargs_to_pass = { kwargs_to_pass = {
'pk': instance.pk 'pk': place.pk
} }
return redirect(reverse_lazy('place_detail', kwargs=kwargs_to_pass)) return redirect(reverse_lazy('place_detail', kwargs=kwargs_to_pass))
else: else:
@ -65,3 +65,12 @@ class PlaceEditView(View):
'form': form_place 'form': form_place
} }
return render(request, 'create_place.html', context) return render(request, 'create_place.html', context)
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()