Skip to content

Commit

Permalink
fix #17 Unicode characters in slugs (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: Loic Teixeira <hello@lta.me>
  • Loading branch information
ckljohn and loicteixeira authored Mar 18, 2023
1 parent 284510d commit ffd82ec
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Compatibility with Python 3.11 and Wagtail 4.2. [#79](https://github.com/wagtail/wagtail-bakery/pull/79)

### Fixed

- Allow unicode characters in slug [#80](https://github.com/wagtail-nest/wagtail-bakery/pull/80)

### Removed

- Drop support for Wagtail before 4.1 [#79](https://github.com/wagtail/wagtail-bakery/pull/79)
Expand Down
11 changes: 6 additions & 5 deletions src/wagtailbakery/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import os
from urllib.parse import urlparse
from urllib.parse import unquote, urlparse

from bakery.views import BuildableDetailView
from django.conf import settings
Expand Down Expand Up @@ -55,17 +55,18 @@ def get_build_path(self, obj):
if url.startswith('http'):
# Multisite has absolute urls
url_parsed = urlparse(url)
path = url_parsed.path
path = unquote(url_parsed.path[1:])
hostname = url_parsed.hostname

if getattr(settings, 'BAKERY_MULTISITE', False):
build_path = os.path.join(
settings.BUILD_DIR, hostname, path[1:])
settings.BUILD_DIR, hostname, path)
else:
build_path = os.path.join(settings.BUILD_DIR, path[1:])
build_path = os.path.join(settings.BUILD_DIR, path)
else:
# Single site has relative urls
build_path = os.path.join(settings.BUILD_DIR, url[1:])
path = unquote(url[1:])
build_path = os.path.join(settings.BUILD_DIR, path)

# Make sure the (deeply) directories are created
os.path.exists(build_path) or os.makedirs(build_path)
Expand Down
24 changes: 20 additions & 4 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,30 @@ def site():
def page_tree(page):
from tests.factories.page import PageFactory

PageFactory(depth=3, path='000100010001', slug='first', numchild=2)
# /first
first_page = PageFactory(depth=3, path='000100010001', slug='first', numchild=2)

# /first/first
PageFactory(depth=4, path=f'{first_page.path}0001', slug='first')
# /first/second
PageFactory(depth=4, path=f'{first_page.path}0002', slug='second')

# /second
PageFactory(depth=3, path='000100010002', slug='second')

# /third
PageFactory(depth=3, path='000100010003', slug='third')

PageFactory(depth=4, path='0001000100010001', slug='first')
PageFactory(depth=4, path='0001000100010002', slug='second')
# /unicode-children
unicode_page = PageFactory(depth=3, path='000100010004', slug='unicode-children', numchild=3)
# /unicode-children/latin-capital-letter-i-with-diaeresis-Ï
PageFactory(depth=4, path=f'{unicode_page.path}0001', slug='latin-capital-letter-i-with-diaeresis-Ï')
# /unicode-children/cyrillic-capital-letter-ya-Я
PageFactory(depth=4, path=f'{unicode_page.path}0002', slug='cyrillic-capital-letter-ya-Я')
# /unicode-children/cjk-fire-火
PageFactory(depth=4, path=f'{unicode_page.path}0003', slug='cjk-fire-火')

page.numchild = 3
page.numchild = 4
page.save()

return page
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ def test_wagtail_bakery_pages_api_detail_view(page_tree):
build_path = view.get_build_path(child_page)
assert build_path == 'api/pages/detail/3.json'

# Check child build path of the first child page
# Check child build path of the first grandchild page
child_page = child_page.get_descendants().first()
build_path = view.get_build_path(child_page)
assert build_path == 'api/pages/detail/6.json'
assert build_path == 'api/pages/detail/4.json'


@pytest.mark.django_db
Expand Down Expand Up @@ -75,7 +75,7 @@ def test_wagtail_bakery_pages_api_detail_view_content(page_tree):
content = json.loads(view.get_content(grandchild_page).decode('UTF-8'))
assert set(content.keys()) == DEFAULT_PAGE_FIELDS
assert set(content['meta'].keys()) == DEFAULT_PAGE_META_FIELDS.union({'parent'})
assert content['id'] == 6
assert content['id'] == 4
assert content['title'] == "Page"


Expand Down
24 changes: 18 additions & 6 deletions tests/integration/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,31 @@ def test_site(site):
@pytest.mark.django_db
def test_page_tree(page_tree):
children = page_tree.get_children()
assert children.count() == 3
assert children.count() == 4

# Check if children of homepage return correct urls
assert children[0].url == '/first/'
assert children[1].url == '/second/'
assert children[2].url == '/third/'

children = children[0].get_children()
assert children.count() == 2
assert children[3].url == '/unicode-children/'

# Check if children of first child return correct urls
assert children[0].url == '/first/first/'
assert children[1].url == '/first/second/'
grandchildren = children[0].get_children()
assert grandchildren.count() == 2
assert grandchildren[0].url == '/first/first/'
assert grandchildren[1].url == '/first/second/'

# Check if children of unicode-children return correct urls
grandchildren = children[3].get_children()
assert grandchildren.count() == 3

# Unicode characters are URL-encoded with `url` but not `url_path`.
# However manual testing shows that the page is indeed accessible with the non-encoded string.
assert grandchildren[0].url_path == (
'/home/unicode-children/latin-capital-letter-i-with-diaeresis-Ï/'
)
assert grandchildren[1].url_path == '/home/unicode-children/cyrillic-capital-letter-ya-Я/'
assert grandchildren[2].url_path == '/home/unicode-children/cjk-fire-火/'


@pytest.mark.django_db
Expand Down
35 changes: 28 additions & 7 deletions tests/integration/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_wagtail_bakery_view_get_url(page_tree):
url = view.get_url(child_page)
assert url == '/first/'

# Check child url of the first child page
# Check child url of the first grandchild page
child_page = child_page.get_descendants().first()
url = view.get_url(child_page)
assert url == '/first/first/'
Expand All @@ -31,16 +31,37 @@ def test_wagtail_bakery_view_build_path(page_tree):
build_path = view.get_build_path(page_tree)
assert build_path == settings.BUILD_DIR + '/index.html'

# Check child build path for first child page
child_page = page_tree.get_descendants().first()
# Check build path for first child page
child_page = page_tree.get_children().first()
build_path = view.get_build_path(child_page)
assert build_path == settings.BUILD_DIR + '/first/index.html'

# Check child build path of the first child page
child_page = child_page.get_descendants().first()
build_path = view.get_build_path(child_page)
# Check build path of the first grandchild page
grandchild_page = child_page.get_children().first()
build_path = view.get_build_path(grandchild_page)
assert build_path == settings.BUILD_DIR + '/first/first/index.html'

# Check build path for unicode-children child page
child_page = page_tree.get_children().last()
build_path = view.get_build_path(child_page)
assert build_path == settings.BUILD_DIR + '/unicode-children/index.html'

# Check build path for unicode-children grandchild pages
grandchild_pages = child_page.get_children().all()

build_path = view.get_build_path(grandchild_pages[0])
assert build_path == (
settings.BUILD_DIR + '/unicode-children/latin-capital-letter-i-with-diaeresis-Ï/index.html'
)

build_path = view.get_build_path(grandchild_pages[1])
assert build_path == (
settings.BUILD_DIR + '/unicode-children/cyrillic-capital-letter-ya-Я/index.html'
)

build_path = view.get_build_path(grandchild_pages[2])
assert build_path == settings.BUILD_DIR + '/unicode-children/cjk-fire-火/index.html'


@pytest.mark.django_db
def test_wagtail_bakery_view_build_path_for_multisite(multisite):
Expand Down Expand Up @@ -75,7 +96,7 @@ def test_all_published_pages_for_multiple_pages(page_tree):
qs = view.get_queryset()

# Check if all pages in page tree are returned
assert qs.count() == 6
assert qs.count() == 10


@pytest.mark.django_db
Expand Down

0 comments on commit ffd82ec

Please sign in to comment.