From 48d56b66f1c594e82d2bac577798e054fe57f305 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Sun, 2 Jul 2023 15:25:29 +0300 Subject: [PATCH 1/5] Improve help text --- blocks/blocks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocks/blocks.py b/blocks/blocks.py index 2e7e7b6c3..21f89af79 100644 --- a/blocks/blocks.py +++ b/blocks/blocks.py @@ -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"), From 0c280b703c5323022a3946e2101fc4471742527f Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Sun, 2 Jul 2023 15:25:44 +0300 Subject: [PATCH 2/5] Default image alignment None --- content_migration/management/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content_migration/management/constants.py b/content_migration/management/constants.py index 49a8668fb..452f43624 100644 --- a/content_migration/management/constants.py +++ b/content_migration/management/constants.py @@ -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/" From 863e61972f89d389de13f902c9a7b5d53b9b5c2c Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Sun, 2 Jul 2023 15:26:03 +0300 Subject: [PATCH 3/5] Set image alignment from style attribute --- content_migration/management/shared.py | 33 ++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/content_migration/management/shared.py b/content_migration/management/shared.py index 7a12c82eb..523f4adc6 100644 --- a/content_migration/management/shared.py +++ b/content_migration/management/shared.py @@ -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: @@ -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, } @@ -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") @@ -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.""" @@ -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( @@ -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( From cccc6141ab4ba85743b383d76ca4efcc217db288 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Sun, 2 Jul 2023 15:32:52 +0300 Subject: [PATCH 4/5] Fix tests --- content_migration/management/test_shared.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/content_migration/management/test_shared.py b/content_migration/management/test_shared.py index cf26be411..63cc56cd0 100644 --- a/content_migration/management/test_shared.py +++ b/content_migration/management/test_shared.py @@ -768,7 +768,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" @@ -780,7 +785,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" From c3aedfb0283f7162965053188c405f4602be961a Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Sun, 2 Jul 2023 15:38:15 +0300 Subject: [PATCH 5/5] Add test case --- content_migration/management/test_shared.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/content_migration/management/test_shared.py b/content_migration/management/test_shared.py index 63cc56cd0..2da7bf63b 100644 --- a/content_migration/management/test_shared.py +++ b/content_migration/management/test_shared.py @@ -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, @@ -882,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)