Hello everyone,
I am writing a save handler in my models.py file to run a few
actions after a save. to do this I have a function called make_photos
and a connection to the post_save signal.
Here is my code:
class Album(models.Model):
title = models.CharField(max_length=200)
date = models.DateField(blank=True, null=True)
desc = models.TextField(blank=True, null=True)
comments = models.BooleanField()
published = models.BooleanField(default=True)
tags = models.CharField(max_length=200, blank=True, null=True)
def __unicode__(self):
return u'%s' % (self.title)
class Photo(models.Model):
title = models.CharField(max_length=200, default='Untitled Photo')
album = models.ForeignKey(Album, blank=True, null=True)
desc = models.TextField(blank=True, null=True)
date = models.DateField(blank=True, null=True)
comments = models.BooleanField()
published = models.BooleanField(default=True)
tags = models.CharField(max_length=200, blank=True, null=True)
thumb_photo = models.ImageField(upload_to='thumb_images',
blank=True, null=True)
large_photo = models.ImageField(upload_to='large_images',
blank=True, null=True)
original_photo = models.ImageField(upload_to='original_images',
blank=True, null=True)
def makeimages(self):
(dirName, fileName) = os.path.split(self.original_photo.path)
(fileBaseName, fileExtension)=os.path.splitext(fileName)
#Save the thumbnail
thumb_width = 300
thumb_height = 300
thumb = Image.open(self.original_photo.path)
thumb.resize((thumb_width,thumb_height), Image.ANTIALIAS)
thumb_path = "/tmp/" + str(fileBaseName) + "_thumb.jpg"
thumb.save(thumb_path, "JPEG")
thumb_img_file = open(thumb_path, 'r')
thumb_file = File(thumb_img_file)
self.thumb_photo.save(str(fileBaseName) + '_thumb.jpg',
thumb_file, save=True)
thumb_img_file.close()
#Save the large
large_width = 1024
large_height = 768
large = Image.open(self.original_photo.path)
large.resize((large_width,large_height), Image.ANTIALIAS)
large_path = "/tmp/" + str(fileBaseName) + "_large.jpg"
large.save(thumb_path, "JPEG")
large_img_file = open(thumb_path, 'r')
large_file = File(thumb_img_file)
self.thumb_photo.save(str(fileBaseName) + '_large.jpg',
large_file, save=True)
large_img_file.close()
post_save.connect(makeimages)
The only problem i have with this code is that it runs every save. So
it creates a infinite loop of sorts and I dont know how to make it
perform this action once. Is there another signal I should use? Any
ideas?
Regards,
Devin M
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.