Exemplo n.º 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)
Exemplo n.º 2
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)