diff options
author | Parth Buch | 2012-07-16 17:07:10 +0530 |
---|---|---|
committer | Parth Buch | 2012-07-16 17:07:10 +0530 |
commit | 257d7cd9780270d4ed5f0bf4683d822217e90077 (patch) | |
tree | aed27ec2c36f300c9a6fa7e247585a94d88652be /stapp | |
parent | e677e4659c726acc68fa75ec2d6318000db88811 (diff) | |
download | stproject-257d7cd9780270d4ed5f0bf4683d822217e90077.tar.gz stproject-257d7cd9780270d4ed5f0bf4683d822217e90077.tar.bz2 stproject-257d7cd9780270d4ed5f0bf4683d822217e90077.zip |
Changed the save function to generate automatic thumbnails
Diffstat (limited to 'stapp')
-rw-r--r-- | stapp/video/models.py | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/stapp/video/models.py b/stapp/video/models.py index 181299c..f181f1b 100644 --- a/stapp/video/models.py +++ b/stapp/video/models.py @@ -1,4 +1,7 @@ from django.db import models +from django.db.models.signals import post_delete +from signals import delete_files + import settings @@ -8,7 +11,7 @@ import Image, os def handle_thumb(image_obj, thumb_obj, width, height): # create thumbnail if image_obj and not thumb_obj: - thumb = image_obj.path + ('-t%sx%s.jpg' % (width, height)) + thumb = image_obj.path + ('-small.jpg') #try: t = Image.open(image_obj.path) @@ -18,11 +21,12 @@ def handle_thumb(image_obj, thumb_obj, width, height): else: t = t.resize((w*height/h, height), Image.ANTIALIAS) w, h = t.size - t = t.crop( ((w-width)/2, (h-height)/4, (w-width)/2+width, (h-height)/4+height) ) + t = t.crop( ((w-width)/2, (h-height)/4, (w-width)/2+width, \ + (h-height)/4+height) ) t.save(settings.MEDIA_ROOT + thumb, 'JPEG') os.chmod(settings.MEDIA_ROOT + thumb, 0666) - thumb_obj = image_obj.url + ('-t%sx%s.jpg' % (width, height)) + thumb_obj = image_obj.url + ('-small.jpg') #except: # pass return thumb_obj @@ -33,7 +37,8 @@ class Video(models.Model): name = models.CharField(max_length=128) filename = models.FileField(upload_to="uploads/videos") image = models.ImageField(upload_to="uploads/screenshots") - thumbnail = models.ImageField(upload_to='uploads/screenshots-thumbs', blank=True, null=True, editable=False) + thumbnail = models.ImageField(upload_to='uploads/screenshots', \ + blank=True, null=True, editable=False) description = models.TextField() created = models.DateTimeField(auto_now_add=True) @@ -42,10 +47,14 @@ class Video(models.Model): def save(self, *args, **kwargs): '''On save, generate thumbnails''' - self.thumbnail = handle_thumb(self.image, self.thumbnail, 100, 100) super(Video, self).save() + self.thumbnail = handle_thumb(self.image, self.thumbnail, 100, 100) + super(Video, self).save(force_update=True) + +#Call the delete_files signal to delete physical files on delete of record +post_delete.connect(delete_files, Video) - + class Module(models.Model): name = models.CharField(max_length=128) |