Question

Hay, i have a model which saves 2 images

class Picture(models.Model):
    picture = models.ImageField(upload_to=make_filename)
    thumbnail = models.ImageField(upload_to=make_thumb_filename)
    car = models.ForeignKey('Car')
    created_on = models.DateField(auto_now_add=True)
    updated_on = models.DateField(auto_now=True)

    def save(self):
        super(Picture, self).save()
        size = 200, 200
        filename = str(self.thumbnail.path)
        image = Image.open(filename)
        image.thumbnail(size, Image.ANTIALIAS)
        image.save(filename)

As you can see i have overwrote the save() method I'n my view i have a simple try, except block, which checks for IOErrors (which are raised if a file other than an image is uploaded)

def upload(request):
    car = Car.objects.get(pk=1)
    try:
        picture = Picture(picture=request.FILES['image'], thumbnail=request.FILES['image'], car=car)
        picture.save()
    except IOError:
        return HttpResponseRedirect("/test/")

However, the exception is raised, but the files are still written to the server (and db)

Any ideas how to make sure files dont get written if the IOError is raised?

EDIT

Fixed by writing a custom method

def is_accectable_file(filename):
    extension = filename.split('.')[-1]
    acceptable_filetypes = ['jpeg','jpeg','gif','png']
    if extension in acceptable_filetypes:
        return True
    else:
        return False

Then exiting my model to

class Picture(models.Model):
    picture = models.ImageField(upload_to=make_filename)
    thumbnail = models.ImageField(upload_to=make_thumb_filename)
    car = models.ForeignKey('Car')
    created_on = models.DateField(auto_now_add=True)
    updated_on = models.DateField(auto_now=True)

    def save(self, *args, **kwargs):
        if is_accectable_file(self.picture.name):
            super(Picture, self).save(*args,**kwargs)
            size = 200, 200
            filename = str(self.thumbnail.path)
            image = Image.open(filename)
            image.thumbnail(size, Image.ANTIALIAS)
            image.save(filename)
            return True
        else:
            return False

and my view to

def upload(request):
    car = Car.objects.get(pk=1)
    try:
        picture = Picture(picture=request.FILES['image'], thumbnail=request.FILES['image'], car=car)
        if picture.save():
            return HttpResponse("fine")
        else:
            return HttpResponse("invalid type")
    except:
        return HttpResponse("no file")
Was it helpful?

Solution

The code that (I assume) throws the IOError is being run after you call the super(Picture,self).save() method. Because of this, the picture getting written to the database even if the exception is thrown.

You just need to move the super call to after the setup code.

As an aside, if you're overriding save I'd recommend doing it as follows:

def save(self,*args,**kwargs):
    ...
    super(Picture, self).save(*args,**kwargs)

Otherwise you'll get an exception in any case where Django is passing in arguments to save (and I believe there are a few cases where it does, at least in the admin).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top