Merge pull request 'Fix image upload bugs' (#10) from dev into main
Reviewed-on: #10
This commit is contained in:
commit
4de233ef1f
2 changed files with 12 additions and 6 deletions
|
@ -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):
|
||||
if instance.pk:
|
||||
try:
|
||||
old_image = sender.objects.get(pk=instance.pk).image
|
||||
except sender.DoesNotExist:
|
||||
return False
|
||||
|
||||
new_image = instance.image
|
||||
if old_image != new_image:
|
||||
default_storage.delete(old_image.path)
|
||||
|
|
|
@ -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)
|
||||
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!"))
|
||||
|
|
Loading…
Reference in a new issue