Ejemplo n.º 1
0
    def save(self, force_insert=False, force_update=False, **kwargs):
        """Overrides models.Model.save.

        - Generates slug.
        - Saves image file.
        """

        # prefill the slug with the ID, it requires double save
        if not self.id:
            if isinstance(self.image, UploadedFile):
                # due to PIL has read several bytes from image, position in file has to be reset
                self.image.seek(0)
            super(Photo, self).save(force_insert, force_update)
            self.width, self.height = get_image_dimensions(self.image.path)
            self.slug = str(self.id) + '-' + self.slug
            force_insert, force_update = False, True
            image_changed = False
        else:
            old = Photo.objects.get(pk = self.pk)
            image_changed = old.image != self.image
        # rename image by slug
        imageType = detect_img_type(self.image.path)
        if imageType is not None:
            self.image = file_rename(self.image.name, self.slug, PHOTOS_TYPE_EXTENSION[ imageType ])
        # delete formatedphotos if new image was uploaded
        if image_changed:
            super(Photo, self).save(force_insert=force_insert, force_update=force_update, **kwargs)
            self.width, self.height = get_image_dimensions(self.image.path)
            force_insert, force_update = False, True
            for f_photo in self.formatedphoto_set.all():
                f_photo.delete()
        super(Photo, self).save(force_insert=force_insert, force_update=force_update, **kwargs)
Ejemplo n.º 2
0
    def thumb_url(self):
        """
        Generates thumbnail for admin site and returns its url
        """
        type = detect_img_type(self.image.path)
        if not type:
            return None

        # cache thumbnail for future use to avoid hitting storage.exists() every time
        # and to allow thumbnail detection after instance has been deleted
        self.thumbnail_path = self.get_thumbnail_path()
        if IMAGE_URL_PREFIX:    
            # custom URL prefix (debugging purposes)
            return IMAGE_URL_PREFIX.rstrip('/') + '/' + self.thumbnail_path

        storage = self.image.storage

        if not storage.exists(self.thumbnail_path):
            try:
                im = Image.open(self.image.path)
                im.thumbnail(PHOTOS_THUMB_DIMENSION, Image.ANTIALIAS)
                im.save(storage.path(self.thumbnail_path), type)
            except IOError:
                # TODO Logging something wrong
                return None
        return storage.url(self.thumbnail_path)
Ejemplo n.º 3
0
Archivo: models.py Proyecto: whit/ella
    def thumb_url(self):
        """
        Generates thumbnail for admin site and returns its url
        """
        # cache thumbnail for future use to avoid hitting storage.exists() every time
        # and to allow thumbnail detection after instance has been deleted
        self.thumbnail_path = self.get_thumbnail_path()
        if photos_settings.IMAGE_URL_PREFIX and not path.exists(
                self.image.path):
            # custom URL prefix (debugging purposes)
            return photos_settings.IMAGE_URL_PREFIX.rstrip(
                '/') + '/' + self.thumbnail_path

        type = detect_img_type(self.image.path)
        if not type:
            return None

        storage = self.image.storage

        if not storage.exists(self.thumbnail_path):
            try:
                im = Image.open(self.image.path)
                im.thumbnail(photos_settings.THUMB_DIMENSION, Image.ANTIALIAS)
                im.save(storage.path(self.thumbnail_path), type)
            except IOError:
                # TODO Logging something wrong
                return None
        return storage.url(self.thumbnail_path)
Ejemplo n.º 4
0
Archivo: models.py Proyecto: whit/ella
    def save(self, force_insert=False, force_update=False, **kwargs):
        """Overrides models.Model.save.

        - Generates slug.
        - Saves image file.
        """

        # prefill the slug with the ID, it requires double save
        if not self.id:
            if isinstance(self.image, UploadedFile):
                # due to PIL has read several bytes from image, position in file has to be reset
                self.image.seek(0)
            # FIXME: better unique identifier, supercalifragilisticexpialidocious?
            self.slug = ''
            super(Photo, self).save(force_insert, force_update)
            self.width, self.height = get_image_dimensions(self.image.path)
            self.slug = str(self.id) + '-' + slugify(self.title)
            # truncate slug in order to fit in an ImageField and/or paths in Redirects
            self.slug = self.slug[:64]
            force_insert, force_update = False, True
            image_changed = False
        else:
            old = Photo.objects.get(pk=self.pk)
            image_changed = old.image != self.image
        # rename image by slug
        imageType = detect_img_type(self.image.path)
        if imageType is not None:
            self.image = file_rename(self.image.name.encode('utf-8'),
                                     self.slug,
                                     photos_settings.TYPE_EXTENSION[imageType])
        # delete formatedphotos if new image was uploaded
        if image_changed:
            super(Photo, self).save(force_insert=force_insert,
                                    force_update=force_update,
                                    **kwargs)
            self.width, self.height = get_image_dimensions(self.image.path)
            force_insert, force_update = False, True
            for f_photo in self.formatedphoto_set.all():
                f_photo.delete()
        super(Photo, self).save(force_insert=force_insert,
                                force_update=force_update,
                                **kwargs)
Ejemplo n.º 5
0
    def thumb_url(self):
        """
        Generates thumbnail for admin site and returns its url
        """
        type = detect_img_type(self.image.path)
        if not type:
            return None

        # photos/2008/12/31/foo.jpg => photos/2008/12/31/thumb-foo.jpg
        thumb_name = path.dirname(self.image.name) + "/" + 'thumb-%s' % path.basename(self.image.name)

        storage = self.image.storage
        if not storage.exists(thumb_name):
            try:
                im = Image.open(self.image.path)
                im.thumbnail(PHOTOS_THUMB_DIMENSION, Image.ANTIALIAS)
                im.save(storage.path(thumb_name), type)
            except IOError:
                # TODO Logging something wrong
                return None
        return storage.url(thumb_name)
Ejemplo n.º 6
0
    def save(self, force_insert=False, force_update=False, **kwargs):
        """Overrides models.Model.save.

        - Generates slug.
        - Saves image file.
        """

        # prefill the slug with the ID, it requires double save
        if not self.id:
            if isinstance(self.image, UploadedFile):
                # due to PIL has read several bytes from image, position in file has to be reset
                self.image.seek(0)
            # FIXME: better unique identifier, supercalifragilisticexpialidocious?
            self.slug = ''
            super(Photo, self).save(force_insert, force_update)
            self.width, self.height = get_image_dimensions(self.image.path)
            self.slug = str(self.id) + '-' + slugify(self.title)
            # truncate slug in order to fit in an ImageField and/or paths in Redirects
            self.slug = self.slug[:64]
            force_insert, force_update = False, True
            image_changed = False
        else:
            old = Photo.objects.get(pk = self.pk)
            image_changed = old.image != self.image
        # rename image by slug
        imageType = detect_img_type(self.image.path)
        if imageType is not None:
            self.image = file_rename(self.image.name.encode('utf-8'), self.slug, photos_settings.TYPE_EXTENSION[ imageType ])
        # delete formatedphotos if new image was uploaded
        if image_changed:
            super(Photo, self).save(force_insert=force_insert, force_update=force_update, **kwargs)
            self.width, self.height = get_image_dimensions(self.image.path)
            force_insert, force_update = False, True
            for f_photo in self.formatedphoto_set.all():
                f_photo.delete()
        super(Photo, self).save(force_insert=force_insert, force_update=force_update, **kwargs)