diff --git a/CHANGELOG.md b/CHANGELOG.md index 9161251..450623a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.3.5 + +- For truncated images, do not import instead of allow truncated image. Wagtail can't support them so it breaks later in Wagtail usage. + # 2.3.4 - Fix import of categories and tags diff --git a/blog/management/commands/import_wordpress.py b/blog/management/commands/import_wordpress.py index 6da895d..29eb830 100644 --- a/blog/management/commands/import_wordpress.py +++ b/blog/management/commands/import_wordpress.py @@ -1,25 +1,32 @@ -from PIL import ImageFile from django.core.management.base import BaseCommand from blog.wordpress_import import WordpressImport -# https://stackoverflow.com/questions/12984426/python-pil-ioerror-image-file-truncated-with-big-images -ImageFile.LOAD_TRUNCATED_IMAGES = True - - class Command(BaseCommand): def add_arguments(self, parser): - parser.add_argument('blog_index', help="Title of blog index page to attach blogs") - parser.add_argument('--url', help="Base url of wordpress instance") - parser.add_argument('--convert-images', default=False, type=bool, help="Find and convert images to Wagtail Images") - parser.add_argument('--create-users', default=False, type=bool, help="Create users out of found Authors") + parser.add_argument( + "blog_index", help="Title of blog index page to attach blogs" + ) + parser.add_argument("--url", help="Base url of wordpress instance") + parser.add_argument( + "--convert-images", + default=False, + type=bool, + help="Find and convert images to Wagtail Images", + ) + parser.add_argument( + "--create-users", + default=False, + type=bool, + help="Create users out of found Authors", + ) def handle(self, *args, **options): - url = options.get('url') - url = url.rstrip('/') + url = options.get("url") + url = url.rstrip("/") wordpress_import = WordpressImport( url, - convert_images=options.get('convert_images'), - create_users=options.get('create_users') + convert_images=options.get("convert_images"), + create_users=options.get("create_users"), ) wordpress_import.get_posts() diff --git a/blog/wordpress_import.py b/blog/wordpress_import.py index b873f82..4d3e940 100644 --- a/blog/wordpress_import.py +++ b/blog/wordpress_import.py @@ -15,6 +15,8 @@ User = get_user_model() +logger = logging.getLogger(__name__) + class WordpressImport: url = "" @@ -66,8 +68,8 @@ def get_posts(self): self.process_post(post) def process_post(self, post): - logging.debug(post["content"]["rendered"]) - logging.info(".") + logger.debug(post["content"]["rendered"]) + logger.info(".") try: page = BlogPage.objects.descendant_of(self.blog_index).get( slug=post["slug"] @@ -217,7 +219,12 @@ def create_images_from_urls_in_content(self, body): image.save() if img.has_attr("srcset"): img["srcset"] = "" - new_url = image.get_rendition("original").url - img["src"] = new_url + try: + new_url = image.get_rendition("original").url + img["src"] = new_url + except OSError: + # Avoid https://github.com/wagtail/wagtail/issues/1326 by not importing it + logger.warning(f"image {image} is unable to be imported") + image.delete() soup.body.hidden = True return soup.body diff --git a/pyproject.toml b/pyproject.toml index 6b11ddd..ad8fb36 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "wagtail_blog" -version = "2.3.4" +version = "2.3.5" description = "A wordpress like blog app implemented in wagtail" authors = ["David Burke"] license = "Apache License"