Skip to content

Commit

Permalink
add override_settings import back in
Browse files Browse the repository at this point in the history
code review changes
  • Loading branch information
nickmoreton committed Nov 1, 2021
1 parent 4f470c0 commit abe9135
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 53 deletions.
2 changes: 1 addition & 1 deletion wagtail_wordpress_import/block_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def build(self):
"""
soup = self.soup.find("body").findChildren(recursive=False)
cached_fallback_value = (
"" # keep appending fall back content here, by default is Rich Text block
"" # append fall back content here, by default it's a Rich Text block
)
cached_fallback_function = import_string(
conf_fallback_block()
Expand Down
21 changes: 11 additions & 10 deletions wagtail_wordpress_import/block_builder_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,16 @@ def conf_fallback_block():
)


def build_none_block_content(cache, blocks):
def build_none_block_content(html, blocks):
"""
image_linker is called to link up and retrive the remote image
document_linker is called to link up and retrive the remote documents
"""
cache = image_linker(cache)
cache = document_linker(cache)
blocks.append({"type": "rich_text", "value": cache})
cache = ""
return cache
html = image_linker(html)
html = document_linker(html)
blocks.append({"type": "rich_text", "value": html})
html = ""
return html


"""Rich Text Functions"""
Expand Down Expand Up @@ -213,14 +214,14 @@ def image_linker(html):
string: the html with img tags modified
BS4 performs a find and replace on all img tags found in the HTML.
If the image can be retrived from the remote site and saved into a Wagtail ImageModel
If the image can be retrieved from the remote site and saved into a Wagtail ImageModel
the soup is modified.
"""
soup = BeautifulSoup(html, "html.parser")
images = soup.find_all("img")
for image in images:
if image.attrs and image.attrs.get("src"):
image_src = get_abolute_src(image.attrs["src"], conf_domain_prefix())
image_src = get_absolute_src(image.attrs["src"], conf_domain_prefix())
saved_image = get_or_save_image(image_src)
if saved_image:
image_embed = soup.new_tag("embed")
Expand Down Expand Up @@ -309,7 +310,7 @@ def fetch_url(src, r=None, status=False, content_type=None):
return r, status, content_type


def get_abolute_src(src, domain_prefix=None):
def get_absolute_src(src, domain_prefix=None):
src = src.lstrip("/")
if not src.startswith("http") and domain_prefix:
return domain_prefix + "/" + src
Expand Down Expand Up @@ -346,7 +347,7 @@ def document_linker(html):
anchors = soup.find_all("a")
for anchor in anchors:
if anchor.attrs and anchor.attrs.get("href"):
anchor_href = get_abolute_src(anchor.attrs["href"], conf_domain_prefix())
anchor_href = get_absolute_src(anchor.attrs["href"], conf_domain_prefix())
anchor_inner_content = anchor.text
saved_document = get_or_save_document(anchor_href)
if saved_document:
Expand Down
69 changes: 28 additions & 41 deletions wagtail_wordpress_import/test/tests/test_block_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
build_image_block,
build_table_block,
conf_domain_prefix,
get_abolute_src,
get_absolute_src,
get_alignment_class,
get_image_alt,
get_image_file_name,
Expand Down Expand Up @@ -220,69 +220,56 @@ def test_get_image_file_name(self):
self.assertEqual(get_image_file_name("fakeimage.jpg"), "fakeimage.jpg")
self.assertEqual(get_image_file_name("folder/fakeimage.jpg"), "fakeimage.jpg")
self.assertEqual(
get_image_file_name(
"http://www.example.com/folder1/folder2//fakeimage.jpg"
),
get_image_file_name("http://www.example.com/folder1/folder2/fakeimage.jpg"),
"fakeimage.jpg",
)

def test_get_abolute_src(self):
def test_get_absolute_src(self):
self.assertEqual(
get_abolute_src("fakeimage.jpg", "http://www.example.com"),
get_absolute_src("fakeimage.jpg", "http://www.example.com"),
"http://www.example.com/fakeimage.jpg",
)
self.assertEqual(
get_abolute_src("folder/fakeimage.jpg", "http://www.example.com"),
get_absolute_src("folder/fakeimage.jpg", "http://www.example.com"),
"http://www.example.com/folder/fakeimage.jpg",
)

def test_get_absolute_src_without_base_url(self):
self.assertEqual(
get_abolute_src("folder/fakeimage.jpg"),
get_absolute_src("folder/fakeimage.jpg"),
"folder/fakeimage.jpg",
) # the test settings has no BASE_URL setting so try having no domain prefix
)

def test_get_abolute_src_slashes_at_start(self):
self.assertEqual(
get_abolute_src("//folder/fakeimage.jpg", "http://www.example.com"),
get_absolute_src("//folder/fakeimage.jpg", "http://www.example.com"),
"http://www.example.com/folder/fakeimage.jpg",
)

def test_get_alignment_class(self):
input = get_soup(
def test_get_alignment_class_align_left(self):
soup = get_soup(
'<img src="fakeimage.jpg" alt="image alt" class="align-left" />',
"html.parser",
).find("img")
self.assertEqual(get_alignment_class(input), "left")
input = get_soup(
self.assertEqual(get_alignment_class(soup), "left")

def test_get_alignment_class_align_right(self):
soup = get_soup(
'<img src="fakeimage.jpg" alt="image alt" class="align-right" />',
"html.parser",
).find("img")
self.assertEqual(get_alignment_class(input), "right")
input = get_soup(
self.assertEqual(get_alignment_class(soup), "right")

def test_get_alignment_class_not_present(self):
soup = get_soup(
'<img src="fakeimage.jpg" alt="image alt" />',
"html.parser",
).find("img")
self.assertEqual(get_alignment_class(input), "fullwidth")

# work in progress
def test_with_real_image(self):
# but we need to test with mocked images if we can.
raw_html_file = """
<p>Lorem <img src="https://dummyimage.com/600x400/000/fff" alt=""></p>
"""
self.builder = BlockBuilder(raw_html_file, None, None)
self.builder.promote_child_tags()
self.blocks = self.builder.build()
self.assertTrue("<embed" in self.blocks[0]["value"])

# work in progress
def test_with_real_pdf(self):
# but we need to test with mocked files if we can.
raw_html_file = """
<p>
<a href="https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf">A pdf</a>
</p>
"""
self.builder = BlockBuilder(raw_html_file, None, None)
self.builder.promote_child_tags()
self.blocks = self.builder.build()
self.assertTrue('linktype="document"' in self.blocks[0]["value"])
self.assertEqual(get_alignment_class(soup), "fullwidth")

"""
TODO: Add some more tests
I need to include tests here for images and documents.
I'm not sure how this could be done at the moment.
Also applies to: test_images_linked_rich_text() above
"""
2 changes: 1 addition & 1 deletion wagtail_wordpress_import/test/tests/test_wordpress_item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import json
from django.test import TestCase
from django.test import TestCase, override_settings
from datetime import datetime
from wagtail_wordpress_import.importers.wordpress import WordpressItem
from wagtail_wordpress_import.logger import Logger
Expand Down

0 comments on commit abe9135

Please sign in to comment.