diff options
Diffstat (limited to 'chimere/models.py')
| -rw-r--r-- | chimere/models.py | 51 | 
1 files changed, 48 insertions, 3 deletions
| diff --git a/chimere/models.py b/chimere/models.py index 936353d..e4469b4 100644 --- a/chimere/models.py +++ b/chimere/models.py @@ -20,10 +20,10 @@  """  Models description  """ -import os, string, re +import os, datetime, re, string  import simplejson as json  from lxml import etree -import datetime +from PIL import Image  from subprocess import Popen, PIPE  from django.conf import settings @@ -540,6 +540,14 @@ class PictureFile(models.Model):      width = models.IntegerField(_(u"Width"))      miniature = models.BooleanField(_(u"Display inside the description?"),                                    default=settings.CHIMERE_MINIATURE_BY_DEFAULT) +    thumbnailfile = models.ImageField(_(u"Thumbnail"), upload_to='pictures', +                        blank=True, null=True, +                        height_field='thumbnailfile_height', +                        width_field='thumbnailfile_width') +    thumbnailfile_height = models.IntegerField(_(u"Thumbnail height"), +                                               blank=True, null=True) +    thumbnailfile_width = models.IntegerField(_(u"Thumbnail width"), +                                              blank=True, null=True)      order = models.IntegerField(_(u"Order"), default=1)      marker = models.ForeignKey(Marker, related_name='pictures') @@ -550,10 +558,47 @@ class PictureFile(models.Model):          verbose_name = _(u"Picture file")          verbose_name_plural = _(u"Picture files") +def scale_image(max_x, pair): +    x, y = pair +    new_y = (float(max_x) / x) * y +    return (int(max_x), int(new_y)) +  def picturefile_post_save(sender, **kwargs): -    if not kwargs['instance'] or not kwargs['created']: +    if not kwargs['instance']:          return      picturefile = kwargs['instance'] + +    if not picturefile.thumbnailfile: +        file = picturefile.picture +        # defining the filename and the thumbnail filename +        filehead, filetail = os.path.split(os.path.abspath(file.path)) +        basename, format = os.path.splitext(filetail) +        miniature = basename + '_thumb' + format +        filename = file.path +        miniature_filename = os.path.join(filehead, miniature) + +        image = Image.open(filename) +        image_x, image_y = image.size +        if settings.CHIMERE_THUMBS_SCALE_HEIGHT: +            image_y, image_x = scale_image(settings.CHIMERE_THUMBS_SCALE_HEIGHT, +                                           (image_y, image_x)) +        elif settings.CHIMERE_THUMBS_SCALE_WIDTH: +            image_x, image_y = scale_image(settings.CHIMERE_THUMBS_SCALE_WIDTH, +                                           (image_x, image_y)) +        image.thumbnail([image_x, image_y], Image.ANTIALIAS) + +        temp_image = open(miniature_filename, 'w') +        try: +            image.save(temp_image, image.format, quality=90, optimize=1) +        except: +            image.save(temp_image, image.format, quality=90) + +        short_name = miniature_filename[len(settings.MEDIA_ROOT):] +        picturefile.thumbnailfile = short_name +        picturefile.save() + +    if not kwargs['instance'] or not kwargs['created']: +        return      pfs = PictureFile.objects.filter(marker=picturefile.marker).exclude(                                       pk=picturefile.pk).order_by('order')      for idx, pf in enumerate(pfs.all()): | 
