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