Frage

In my project I have a model where I use Imagekit to process an image. When I save an image I have following requirements:

  • rename image and thumbnail to a unique name
  • when a new image is loaded, the old one should be removed (and the thumbnail in the cache should refresh to the new image).

To accomplish this, I use following code:

The model:

def generate_cache_filename(instance, path, specname, extension):
    extension = '.jpg'
    return 'cache/images_upload/%s_%s%s' % (instance.pk, specname, extension)

def generate_image_filename_1(instance, filename):
    filename = '1'
    extension = '.jpg'
    return 'images_upload/%s_%s%s' % (instance.pk, filename, extension)

class Model(models.Model):
    name = models.CharField(max_length=40)
    image_1 = ProcessedImageField([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(500, 370)], upload_to=generate_image_filename_1, format='JPEG', options={'quality': 90})
    thumbnail_1 = ImageSpec([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(83, 78)], image_field='image_1', cache_to=generate_cache_filename, format='JPEG', options={'quality': 90})

The form (to delete the image when it is replaced by a new one):

if form.is_valid():
    form_image = form.cleaned_data['image_1']
        try:
            details = Model.objects.get(pk=pk)
            if details.image_1 != form_image:
                details.image_1.delete(save=False)
        except Model.DoesNotExist: 
            pass
        form.save()

The part of renaming the images and replacing image_1 (= loading new and deleting old) works just fine. But for some reason the thumbnail_1 in the cache does not refresh (= is still the thumbnail of the old image).

I think it has something to do with the deletion code in the form, but I can't figure out why and how to solve it. Someone with suggestions?

UPDATE 1: it has also something to do with the 'renaming'. I did some extra tests: when I don't rename the image_1 file, then everything works fine (also refreshing of the thumbnail). But when I load another image with the same name, then I have the same problem: image_1 is updated, but thumbnail_1 is still the thumbnail of the old image.

UPDATE 2: did some more tests and when uploading a new image with the same filename, I definitely enter the if statement in try. So the old image is deleted. According to the documentation of Imagekit, the thumbnail should also be deleted. But this is not the case.

Many thanks!

War es hilfreich?

Lösung

In the mean time I found a working solution. The main reason why the above code didn't work was because of the thumbnail in the cache that was not deleted after deleting the original image, especially in cases where the new image had the same filename as the previous one => then the thumbnail was never deleted. => I still don't know why..., because I expected that the cached image is always deleted when the original is deleted.

Using the following code everything works as expected: Basically I made sure the new uploaded image has always another filename:

Model.py

def generate_cache_filename(instance, path, specname, extension):
    extension = '.jpg'
    return 'cache/images_upload/%s_%s%s' % (instance.pk, specname, extension)
# generate random string of 10 characters
def id_generator(size=10, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for x in range(size))

def generate_random_filename(instance, filename):
    filename = id_generator()
    extension = '.jpg'
return 'images_upload/%s_%s%s' % (instance.pk, filename, extension)

class Model(models.Model):
    name = models.CharField(max_length=20)
    image_1 = ProcessedImageField([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(500, 370)], upload_to=generate_random_filename, format='JPEG', options={'quality': 90})
    thumbnail_1 = ImageSpec([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(83, 78)], image_field='image_1', cache_to=generate_cache_filename, format='JPEG', options={'quality': 90})

View.py:

# thanks to jdi for the help in the for-loop
if form.is_valid():
        # A for loop is used here, because my database contains 5 images and 5              thumbnails
        image_list = ['image_%d' % i for i in xrange(1,6)]
        for image_name in image_list:
            form_image = form.cleaned_data[image_name]
            try:
                details = Model.objects.get(pk=pk)
                if getattr(details, image_name, None) != form_image:
                    getattr(details, image_name, None).delete(save=False)
            except Model.DoesNotExist: 
                pass

Hopefully this can help out others as well.

Kind Regards

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top