Beispiel #1
0
    def save(self, delete_zip_import=True, *args, **kwargs):
        """
        If a zip file is uploaded, extract any images from it and add
        them to the gallery, before removing the zip file.
        """

        self.gen_description = False
        if not hasattr(self, 'image_collection'):
            new_image_collection = ImageCollection(title=self.title)
            new_image_collection.save()
            self.image_collection = new_image_collection
            # audio = [album_image.audio_file for album_image in self.images.all() if album_image.audio_file]
            # if(any(audio)):
            #     soundcloud_helper = SoundCloud()
            #     soundcloud_helper.create_playlist(self.title)
        super(Album, self).save(*args, **kwargs)
        if self.zip_import:
            zip_file = ZipFile(self.zip_import)
            # import PIL in either of the two ways it can end up installed.
            try:
                from PIL import Image
            except ImportError:
                import Image
            first = True
            for name in zip_file.namelist():
                data = zip_file.read(name)
                try:
                    image = Image.open(StringIO(data))
                    image.load()
                    image = Image.open(StringIO(data))
                    image.verify()
                except:
                    continue
                name = convert_filename(os.path.split(name)[1])
                path = os.path.join(ALBUMS_UPLOAD_DIR, self.slug,
                                    name.decode("utf-8"))
                try:
                    saved_path = default_storage.save(path, ContentFile(data))
                except UnicodeEncodeError:
                    from warnings import warn

                    warn("A file was saved that contains unicode "
                         "characters in its path, but somehow the current "
                         "locale does not support utf-8. You may need to set "
                         "'LC_ALL' to a correct value, eg: 'en_US.UTF-8'.")
                    path = os.path.join(ALBUMS_UPLOAD_DIR, self.slug,
                                        unicode(name, errors="ignore"))
                    saved_path = default_storage.save(path, ContentFile(data))
                album_image = AlbumImage(image_file=saved_path, location=self.location,
                                         photographer=self.photographer)
                if first and not self.has_cover:
                    album_image.is_cover = True
                    first = False
                self.images.add(album_image)
            if delete_zip_import:
                zip_file.close()
                self.zip_import.delete(save=True)
Beispiel #2
0
    def save(self, delete_zip_import=True, *args, **kwargs):
        """
        If a zip file is uploaded, extract any images from it and add
        them to the gallery, before removing the zip file.
        """

        # Update if a entry for district already exists
        face_exist = Face.objects.filter(district=self.district)
        face_exist = face_exist and face_exist[0]
        if not self.pk and face_exist:
            self.image_collection = face_exist.image_collection
            self.image_collection_id = face_exist.image_collection_id
            self.pk = face_exist.pk
            self.site_id = face_exist.site_id

        if not hasattr(self, 'image_collection'):
            new_image_collection = ImageCollection(title=self.district.district)
            new_image_collection.save()
            self.image_collection = new_image_collection
        super(Face, self).save(*args, **kwargs)
        if self.zip_import:
            zip_file = ZipFile(self.zip_import)
            # import PIL in either of the two ways it can end up installed.
            try:
                from PIL import Image
            except ImportError:
                import Image
            for name in zip_file.namelist():
                data = zip_file.read(name)
                try:
                    image = Image.open(StringIO(data))
                    image.load()
                    image = Image.open(StringIO(data))
                    image.verify()
                except:
                    continue
                name = convert_filename(os.path.split(name)[1])
                path = os.path.join(FACES_UPLOAD_DIR, self.slug,
                                    name.decode("utf-8"))
                try:
                    saved_path = default_storage.save(path, ContentFile(data))
                except UnicodeEncodeError:
                    from warnings import warn

                    warn("A file was saved that contains unicode "
                         "characters in its path, but somehow the current "
                         "locale does not support utf-8. You may need to set "
                         "'LC_ALL' to a correct value, eg: 'en_US.UTF-8'.")
                    path = os.path.join(FACES_UPLOAD_DIR, self.slug,
                                        unicode(name, errors="ignore"))
                    saved_path = default_storage.save(path, ContentFile(data))
                face_image = FaceImage(image_file=saved_path)
                self.images.add(face_image)
            if delete_zip_import:
                zip_file.close()
                self.zip_import.delete(save=True)