Great tip, Thanks!

Here my code, just in case someone is interested:

from django.db import models
from django.utils.translation import ugettext as _
import Image as PilImage
import settings as s
import os

class Gallery(models.Model):
     title = models.CharField(_("Name"), max_length=200)
     slug  = models.SlugField(help_text=_("Used for URLs"))
     def __unicode__(self):
         return unicode(self.title)

     def delete(self):
         for p in self.photo_set.all():
             p.delete()
         super(Gallery, self).delete()

     def get_absolute_url(self):
         return '/portfolio/'+self.slug

class Photo(models.Model):
     picture = models.ImageField(null=True, upload_to='./images/')
     caption = models.CharField(_("Optional  
caption"),max_length=100,null=True, blank=True)
     item = models.ForeignKey(Gallery)

     def __unicode__(self):
         return unicode(self.picture)

     def delete(self):
         for t in self.thumbnail_set.all():
             t.delete()
         super(Photo, self).delete()

class Thumbnail(models.Model):
     item = models.ForeignKey(Photo)
     dimensions = models.CharField(max_length=10)
     miniature_url = models.CharField(max_length=100)
     x,y = None, None

     def __unicode__(self):
         return "Thumbnail %s of %s" %(self.dimensions, self.item)#

     def delete(self):
         try:
             os.remove(self.miniature_filename)
         except OSError:
             pass
         super(Thumbnail, self).delete()
         if '.DS_Store' in os.listdir(self.miniature_folder):
             os.listdir(self.miniature_folder).remove('.DS_Store')

     def process(self):
         x,y=None,None
         if 'x' in self.dimensions:
             x, y = [int(x) for x in self.dimensions.split('x')]
         elif 'w' in self.dimensions:
             x = int(self.dimensions[1:])
         elif 'h' in self.dimensions:
             y = int(self.dimensions[1:])
         self.size = [x,y]

         # defining the filename and the miniature filename
         basename, format = self.item.picture.name.rsplit('.', 1)
         self.miniature_folder  = os.path.join(s.MEDIA_ROOT, basename 
+'/')
         if not os.path.exists(self.miniature_folder):
             os.mkdir(self.miniature_folder)
         miniature = basename + '/' +self.dimensions.replace(' ','') +  
'.' +  format
         self.miniature_filename = os.path.join(s.MEDIA_ROOT, miniature)
         self.miniature_url = os.path.join(s.MEDIA_URL, miniature)
         self.filename =  
os.path.join(s.MEDIA_ROOT,self.item.picture.name)

     def make_thumbnail(self):
         if not os.path.exists(self.miniature_filename):
             image = PilImage.open(self.filename)
             old_image_size=image.size
             if not self.x or not self.y:
                 self.x, self.y = image.size
             if not self.size[1]:
                 self.size[1]=int(self.y*(float(self.size[0])/self.x))
             elif not self.size[0]:
                 self.size[0]=int(self.x*(float(self.size[1])/self.y))

              
image.thumbnail([self.size[0],self.size[1]],PilImage.ANTIALIAS)
             image.save(self.miniature_filename, image.format)

### Signals instead of overwriting __init__(self)

from django.db.models.signals import post_init

def thumb_init(sender, **kwargs):
     kwargs['instance'].process()
     kwargs['instance'].make_thumbnail()

post_init.connect(thumb_init, sender=Thumbnail)




Am 01.10.2008 um 16:15 schrieb Manuel Meyer:

>
>
> Am 30.09.2008 um 20:21 schrieb Rajesh Dhawan:
>>
>> You are overriding __init__ but not making sure to call the base
>> class's __init__ method which does a whole bunch of stuff whenever a
>> new instance of a Model class is created. This is most definitely
>> causing your problem. It's best not to overload it with your own
>> __init__. A better option is to hook into the post_init signal with a
>> custom method and in that method do your process() and
>> make_thumbnail() calls.
>
> Thanks, I will have a look on it.
>>
>>
>> See:
>> - http://docs.djangoproject.com/en/dev//topics/signals/
>> - http://docs.djangoproject.com/en/dev/ref/signals/
>>
>>
>> -RD
>>>
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to