Merge pull request 'Fix image upload bugs' (#10) from dev into main
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/promote/production Build is passing

Reviewed-on: #10
This commit is contained in:
Jannis Portmann 2023-07-10 11:11:36 +02:00
commit 4de233ef1f
2 changed files with 12 additions and 6 deletions

View file

@ -52,9 +52,12 @@ def auto_delete_image_on_delete(sender, instance, **kwargs):
@receiver(models.signals.pre_save, sender=Offer)
def delete_old_image(sender, instance, **kwargs):
try:
old_image = sender.objects.get(pk=instance.pk).image
except sender.DoesNotExist:
return False
if instance.pk:
try:
old_image = sender.objects.get(pk=instance.pk).image
except sender.DoesNotExist:
return False
default_storage.delete(old_image.path)
new_image = instance.image
if old_image != new_image:
default_storage.delete(old_image.path)

View file

@ -87,7 +87,10 @@ def offer_edit(request, offer_id):
form = CreateOfferForm(request.POST, request.FILES, instance=offer)
if form.is_valid():
offer = form.save(commit=False)
offer.image.name = generate_unique_filename(form.cleaned_data['image'].name)
if 'image' in request.FILES:
offer.image.name = generate_unique_filename(form.cleaned_data['image'].name)
else:
offer.image = offer.image
offer.user = request.user
offer.save()
messages.success(request, _("Offer updated successfully!"))