Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#2883 closed defect (worksforme)

path is blank in an image field when using a save hook

Reported by: adamduren@… Owned by: adrian
Component: contrib.admin Version:
Severity: major Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

def resizeThis(self):

img = pImage.open(self.path)
cImgSize = img.size
newH = int((500*cImgSize[1])/cImgSize[0])
img2 = img.resize((500, newH), pImage.ANTIALIAS)
img2.save(self.path)

def save(self):

super(Image, self).save()
self.resizeThis()

When I try this i get an IO Error and I'm not sure if it's a defect or not. It says self.path is empty for the image field in the model.

Change History (2)

comment:1 Changed 9 years ago by [530]

  • Resolution set to worksforme
  • Status changed from new to closed

This is because of the way the addmanipulator works, it saves the model twice.

    def resizeThis(self):

        img = pImage.open(self.path) cImgSize = img.size newH = int((500*cImgSize[1])/cImgSize[0]) img2 = img.resize((500, newH), pImage.ANTIALIAS) img2.save(self.path)

    saved_once=False

    def save(self):
        if self.saved_once:
            super(Image, self).save() self.resizeThis()
        else:
            self.saved_once=True


comment:2 Changed 9 years ago by anonymous

I mean

class Image(models.Model):

    def resizeThis(self):

        img = pImage.open(self.path)
        cImgSize = img.size
        newH = int((500*cImgSize[1])/cImgSize[0])
        img2 = img.resize((500, newH), pImage.ANTIALIAS)
        img2.save(self.path)

    saved_once=False

    def save(self):
        if self.saved_once:
             self.resizeThis()
        else:
            self.saved_once=True
        super(Image, self).save()
Note: See TracTickets for help on using tickets.
Back to Top