Example #1
0
    def save_model(self, request, obj, form, change):
        if form.is_valid():
            album = form.save(commit=False)
            album.modified = datetime.now()
            album.save()

            if form.cleaned_data['zip'] != None:
                zip = zipfile.ZipFile(form.cleaned_data['zip'])
                for filename in sorted(zip.namelist()):

                    file_name = os.path.basename(filename)
                    if not file_name:
                        continue

                    data = zip.read(filename)
                    contentfile = ContentFile(data)

                    img = AlbumImage()
                    img.album = album
                    img.alt = filename
                    filename = '{0}{1}.jpg'.format(album.slug, str(uuid.uuid4())[-13:])
                    img.image.save(filename, contentfile)

                    filepath = '{0}/albums/{1}'.format(Own.settings.MEDIA_ROOT, filename)
                    with Image.open(filepath) as i:
                        img.width, img.height = i.size

                    img.thumb.save('thumb-{0}'.format(filename), contentfile)
                    img.save()
                zip.close()
            super(AlbumModelAdmin, self).save_model(request, obj, form, change)
Example #2
0
    def save_model(self, request, obj, form, change):
        if form.is_valid():
            album = form.save(commit=False)
            album.modified = datetime.now()
            album.save()

            file_upload_field = form.files['file_upload']
            files = file_upload_field.multi_file_upload



            def check_has(item):
                for file in AlbumImage.objects.all().values():
                    print(file)
                    if file['filename'] == item:
                        return True

            for file in files:
                file_content, filename = file[0], file[1]
                alt_name = f'{album.slug}{str(uuid.uuid4())[-13:]}.jpg'
                file_path = jux_photos.settings.MEDIA_ROOT + 'albums/' + filename


                img = AlbumImage(album=album, alt=alt_name)
                img.filename = filename
                try:
                    img.image.save(alt_name, file_content)
                except:
                    # Sometimes there's an error saving the image, something to do with seek of closed file....
                    # This will try to delete the object then the image. Even though from my testing it'll still successfully upload the images...?
                    try: img.delete()
                    except: pass

                    try: os.remove(file_path)
                    except: pass

            file_upload_field.multi_file_upload = []
            super(AlbumModelAdmin, self).save_model(request, obj, form, change)
Example #3
0
    def save_model(self, request, obj, form, change):
        if form.is_valid():  #confirming validation and saving the album
            album = form.save(commit=False)
            album.modified = datetime.now()
            album.save()

            if form.cleaned_data[
                    'zip'] != None:  #the fomat should be a zip file //zip file admin handler
                zip = zipfile.ZipFile(form.cleaned_data['zip'])
                for filename in sorted(zip.namelist()):

                    file_name = os.path.basename(filename)
                    if not file_name:
                        continue

                    data = zip.read(
                        filename)  #have the data as the read zip file
                    contentfile = ContentFile(
                        data
                    )  #reading the data in the zip file and have it as contentfile

                    img = AlbumImage(
                    )  #handling the image in the album containing also the zip file
                    img.album = album
                    img.alt = filename
                    filename = '{0}{1}.jpg'.format(
                        album.slug,
                        str(uuid.uuid4())[-13:])  #give it a unique identifier
                    img.image.save(filename, contentfile)

                    filepath = '{0}/albums/{1}'.format(
                        django_photo_gallery.settings.MEDIA_ROOT, filename
                    )  #points to settings in the media wherre all images are stored
                    with Image.open(filepath) as i:
                        img.width, img.height = i.size

                    img.thumb.save('thumb-{0}'.format(filename), contentfile)
                    img.save()
                zip.close()
            super(AlbumModelAdmin, self).save_model(request, obj, form, change)