Skip to content

Commit

Permalink
Merge pull request #749 from WesternFriend/import-images-alignment
Browse files Browse the repository at this point in the history
Import images alignment
  • Loading branch information
brylie committed Jul 2, 2023
2 parents 77f94e2 + c3aedfb commit 3399ca1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion blocks/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class FormattedImageChooserStructBlock(wagtail_blocks.StructBlock):
help_text="Enter the desired image width value in pixels up to 800 max.",
)
align = wagtail_blocks.ChoiceBlock(
help_test="Optionally align image left or right",
help_test="Optionally align image left or right. Will default to block alignment.", # noqa: E501
choices=(
("left", "Left"),
("right", "Right"),
Expand Down
2 changes: 1 addition & 1 deletion content_migration/management/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DEFAULT_IMAGE_WIDTH = 350
DEFAULT_IMAGE_ALIGN = "left"
DEFAULT_IMAGE_ALIGN = None

LOCAL_MIGRATION_DATA_DIRECTORY = "migration_data/"
SITE_BASE_URL = "https://westernfriend.org/"
Expand Down
33 changes: 31 additions & 2 deletions content_migration/management/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ class GenericFormattedImageBlock:
link_url: str | None


def create_image_block_from_url(image_url: str, link_url: str | None = None) -> dict:
def create_image_block_from_url(
image_url: str,
link_url: str | None = None,
align: str | None = None,
) -> dict:
"""Create a Wagtial image block from an image URL."""

try:
Expand Down Expand Up @@ -159,7 +163,7 @@ def create_image_block_from_url(image_url: str, link_url: str | None = None) ->
image_chooser_block = {
"image": image,
"width": DEFAULT_IMAGE_WIDTH,
"align": DEFAULT_IMAGE_ALIGN,
"align": align,
"link": link_url,
}

Expand All @@ -183,10 +187,16 @@ def create_block(generic_block: GenericBlock) -> tuple[str, str | dict]:
if generic_block.block_content["link"] is not None # type: ignore
else None
)
align: str = (
generic_block.block_content["align"] # type: ignore
if generic_block.block_content["align"] is not None # type: ignore
else DEFAULT_IMAGE_ALIGN
)
try:
image_block = create_image_block_from_url(
image_url=image_url,
link_url=link_url, # type: ignore
align=align, # type: ignore
)
except requests.exceptions.MissingSchema:
raise BlockFactoryError("Invalid image URL: missing schema")
Expand Down Expand Up @@ -225,6 +235,17 @@ def remove_pullquote_tags(item_string: str) -> str:
return str(soup)


def get_image_align_from_style(style_string: str) -> str | None:
"""Get the image alignment from the style string."""

if "float:left" in style_string or "float: left" in style_string:
return "left"
elif "float:right" in style_string or "float: right" in style_string:
return "right"
else:
return DEFAULT_IMAGE_ALIGN


def adapt_html_to_generic_blocks(html_string: str) -> list[GenericBlock]:
"""Adapt HTML string to a list of generic blocks."""

Expand Down Expand Up @@ -293,6 +314,13 @@ def adapt_html_to_generic_blocks(html_string: str) -> list[GenericBlock]:
image_url = image_tag["src"]
image_url = ensure_absolute_url(image_url)

# get image alignment from style attribute float property
if "style" in image_tag.attrs:
image_style = image_tag["style"]
image_align = get_image_align_from_style(image_style)
else:
image_align = DEFAULT_IMAGE_ALIGN

# make sure the URL contains westernfriend.org
if "westernfriend.org" not in image_url:
raise ValueError(
Expand All @@ -308,6 +336,7 @@ def adapt_html_to_generic_blocks(html_string: str) -> list[GenericBlock]:
image_chooser_block_content = {
"image": image_url,
"link": image_link_url,
"align": image_align,
}

generic_blocks.append(
Expand Down
34 changes: 32 additions & 2 deletions content_migration/management/test_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
extract_pullquotes,
fetch_file_bytes,
get_existing_magazine_author_from_db,
get_image_align_from_style,
parse_body_blocks,
parse_csv_file,
parse_media_blocks,
Expand Down Expand Up @@ -768,7 +769,12 @@ def test_block_factory_with_invalid_block_type(self) -> None:

def test_create_block_invalid_image_url_missing_schema(self) -> None:
invalid_url_block = GenericBlock(
"image", {"image": "invalid_url", "link": None}
"image",
{
"image": "invalid_url",
"link": None,
"align": None,
},
)
with patch(
"content_migration.management.shared.create_image_block_from_url"
Expand All @@ -780,7 +786,12 @@ def test_create_block_invalid_image_url_missing_schema(self) -> None:

def test_create_block_invalid_image_url_invalid_schema(self) -> None:
invalid_url_block = GenericBlock(
"image", {"image": "invalid_url", "link": None}
"image",
{
"image": "invalid_url",
"link": None,
"align": None,
},
)
with patch(
"content_migration.management.shared.create_image_block_from_url"
Expand Down Expand Up @@ -872,3 +883,22 @@ def test_create_media_block_from_file_bytes(self) -> None:
)

self.assertEqual(media_block[0], "media")


class GetImageAlignFromStyleSimpleTestCase(SimpleTestCase):
def test_get_image_align_from_style(self) -> None:
# create a media file from a file in the test data directory
style = "float: left; margin-right: 10px; margin-bottom: 10px;"
image_align = get_image_align_from_style(style)

self.assertEqual(image_align, "left")

style_right = "float: right; margin-left: 10px; margin-bottom: 10px;"
image_align_right = get_image_align_from_style(style_right)

self.assertEqual(image_align_right, "right")

style_none = "margin-left: 10px; margin-bottom: 10px;"
image_align_none = get_image_align_from_style(style_none)

self.assertEqual(image_align_none, None)

0 comments on commit 3399ca1

Please sign in to comment.