kopia lustrzana https://github.com/wagtail/wagtail
Localize rich text links
rodzic
878f8a7fc6
commit
e78db08d24
|
@ -174,6 +174,6 @@ class PageLinkHandler:
|
|||
if parent_page:
|
||||
attrs += 'data-parent-id="%d" ' % parent_page.id
|
||||
|
||||
return '<a %shref="%s">' % (attrs, escape(page.specific.url))
|
||||
return '<a %shref="%s">' % (attrs, escape(page.localized.specific.url))
|
||||
except Page.DoesNotExist:
|
||||
return "<a>"
|
||||
|
|
|
@ -19,6 +19,6 @@ class PageLinkHandler(LinkHandler):
|
|||
def expand_db_attributes(cls, attrs):
|
||||
try:
|
||||
page = cls.get_instance(attrs)
|
||||
return '<a href="%s">' % escape(page.specific.url)
|
||||
return '<a href="%s">' % escape(page.localized.specific.url)
|
||||
except Page.DoesNotExist:
|
||||
return "<a>"
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
from django.test import TestCase, override_settings
|
||||
from django.utils import translation
|
||||
|
||||
from wagtail.core.models import Locale, Page
|
||||
from wagtail.core.rich_text import RichText, expand_db_html
|
||||
from wagtail.core.rich_text.feature_registry import FeatureRegistry
|
||||
from wagtail.core.rich_text.pages import PageLinkHandler
|
||||
|
@ -12,6 +14,10 @@ from wagtail.tests.testapp.models import EventPage
|
|||
class TestPageLinktypeHandler(TestCase):
|
||||
fixtures = ['test.json']
|
||||
|
||||
def test_expand_db_attributes(self):
|
||||
result = PageLinkHandler.expand_db_attributes({'id': Page.objects.get(url_path='/home/events/christmas/').id})
|
||||
self.assertEqual(result, '<a href="/events/christmas/">')
|
||||
|
||||
def test_expand_db_attributes_page_does_not_exist(self):
|
||||
result = PageLinkHandler.expand_db_attributes({'id': 0})
|
||||
self.assertEqual(result, '<a>')
|
||||
|
@ -21,6 +27,46 @@ class TestPageLinktypeHandler(TestCase):
|
|||
self.assertEqual(result, '<a href="None">')
|
||||
|
||||
|
||||
@override_settings(
|
||||
WAGTAIL_I18N_ENABLED=True,
|
||||
WAGTAIL_CONTENT_LANGUAGES=[
|
||||
('en', 'English'),
|
||||
('fr', 'French'),
|
||||
],
|
||||
ROOT_URLCONF='wagtail.tests.urls_multilang'
|
||||
)
|
||||
class TestPageLinktypeHandlerWithI18N(TestCase):
|
||||
fixtures = ['test.json']
|
||||
|
||||
def setUp(self):
|
||||
self.fr_locale = Locale.objects.create(language_code="fr")
|
||||
self.event_page = Page.objects.get(url_path='/home/events/christmas/')
|
||||
self.fr_event_page = self.event_page.copy_for_translation(self.fr_locale, copy_parents=True)
|
||||
self.fr_event_page.slug = 'noel'
|
||||
self.fr_event_page.save(update_fields=['slug'])
|
||||
self.fr_event_page.save_revision().publish()
|
||||
|
||||
def test_expand_db_attributes(self):
|
||||
result = PageLinkHandler.expand_db_attributes({'id': self.event_page.id})
|
||||
self.assertEqual(result, '<a href="/en/events/christmas/">')
|
||||
|
||||
def test_expand_db_attributes_autolocalizes(self):
|
||||
# Even though it's linked to the english page in rich text.
|
||||
# The link should be to the local language version if it's available
|
||||
with translation.override("fr"):
|
||||
result = PageLinkHandler.expand_db_attributes({'id': self.event_page.id})
|
||||
self.assertEqual(result, '<a href="/fr/events/noel/">')
|
||||
|
||||
def test_expand_db_attributes_doesnt_autolocalize_unpublished_page(self):
|
||||
# We shouldn't autolocalize if the translation is unpublished
|
||||
self.fr_event_page.unpublish()
|
||||
self.fr_event_page.save()
|
||||
|
||||
with translation.override("fr"):
|
||||
result = PageLinkHandler.expand_db_attributes({'id': self.event_page.id})
|
||||
self.assertEqual(result, '<a href="/en/events/christmas/">')
|
||||
|
||||
|
||||
class TestExtractAttrs(TestCase):
|
||||
def test_extract_attr(self):
|
||||
html = '<a foo="bar" baz="quux">snowman</a>'
|
||||
|
|
Ładowanie…
Reference in New Issue