Code

Opened 8 years ago

Closed 8 years ago

Last modified 8 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.

Attachments (0)

Change History (2)

comment:1 Changed 8 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 8 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()

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.