I have a project and i have idea to make wagtail
Page
where user can upload an zip
archive of images in the admin section. And then I want to unzip this archive and put each unzipped image to Image Gallery connected to this page:
import urllib.parse
import os
import zipfile
from django.db import models
from django.conf import settings
from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
from wagtail.wagtailsearch import index
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailimages.models import Image
from taggit.models import TaggedItemBase
from modelcluster.fields import ParentalKey
from modelcluster.contrib.taggit import ClusterTaggableManager
from .forms import PhotoEventPageForm
from utils import get_paginated_pages, get_all_tags
...
class PhotoEventPage(Page):
photos = models.FileField(upload_to=directory_path)
photos_unpacked = models.BooleanField(default=False)
...
base_form_class = PhotoEventPageForm
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
def save(self, *args, **kwargs):
if os.path.isfile(self.photos.path) and not self.photos_unpacked:
with zipfile.ZipFile(self.photos.path, "r") as zip_ref:
for file_name in zip_ref.namelist():
zip_ref.extract(
file_name,
path=os.path.dirname(self.photos.path))
# now i have image file, this line prints True:
# print(os.path.isfile(os.path.join(os.path.dirname(self.photos.path), file_name)))
# so now I have saved image and i want to create Image gallery,
# I've tried this code:
# img = Image()
# img.save()
# gallery_img = PhotoEventPageGalleryImage(
# image_id=img.id, page_id=self.id)
# gallery_img.save()
# but it's not working
self.photos_unpacked = True
return super().save(*args, **kwargs)
class PhotoEventPageGalleryImage(Orderable):
page = ParentalKey(PhotoEventPage, related_name='gallery_images')
image = models.ForeignKey(
'wagtailimages.Image', on_delete=models.CASCADE, related_name='+'
)
caption = models.CharField(blank=True, max_length=250)
panels = [
ImageChooserPanel('image'),
FieldPanel('caption'),
]
But It's not working, I dont know how to create wagtail.wagtailimages.models.Image
instance from my unzipped image, when i uncoment this piece of code:
img = Image()
img.save()
gallery_img = PhotoEventPageGalleryImage(
image_id=img.id, page_id=self.id)
gallery_img.save()
i'm getting NOT NULL constraint failed: wagtailimages_image.width
. So maybe there is a way to do it?
After some googling, thanks to Paulo Scardine, also checked: this I came up with following code:
from django.core.files.images import ImageFile
from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
from wagtail.wagtailsearch import index
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailimages.models import Image
from taggit.models import TaggedItemBase
...
class PhotoEventPage(Page):
...
def save(self, *args, **kwargs):
if os.path.isfile(self.photos.path) and not self.photos_unpacked:
with zipfile.ZipFile(self.photos.path, "r") as zip_ref:
for file_name in zip_ref.namelist():
zip_ref.extract(
file_name,
path=os.path.dirname(self.photos.path))
# print(os.path.isfile(os.path.join(os.path.dirname(self.photos.path), file_name)))
image_file = open(
os.path.join(
os.path.dirname(
self.photos.path), file_name), "rb")
image = Image(
title="Image title",
file=ImageFile(image_file, name=file_name),
)
image.save()
image_file.close()
gallery_img = PhotoEventPageGalleryImage(
image_id=image.id, page_id=self.id)
gallery_img.save()
self.photos_unpacked = True
return super().save(*args, **kwargs)
And it's strange, i mean it's indeed working without errors and I can save PhotoEventPage
without errors and images are extracted, but there is no any items inside self.gallery_images
after saving, don`t know why.