Skip to content

Commit

Permalink
alternative workaround for wagtail bug wagtail/wagtail#1326
Browse files Browse the repository at this point in the history
  • Loading branch information
bufke committed Feb 20, 2020
1 parent ed2d077 commit 71fe3ac
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 20 additions & 13 deletions blog/management/commands/import_wordpress.py
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 11 additions & 4 deletions blog/wordpress_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

User = get_user_model()

logger = logging.getLogger(__name__)


class WordpressImport:
url = ""
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 71fe3ac

Please sign in to comment.