Make top-level *_linktype_handler / *_embedtype_handler functions into classes

pull/5211/head
Matt Westcott 2019-04-06 00:54:53 +01:00
rodzic a633eb31fc
commit 375ae00518
13 zmienionych plików z 80 dodań i 71 usunięć

Wyświetl plik

@ -24,7 +24,8 @@ def expand_db_html(html):
embed_rules = features.get_embed_types()
link_rules = features.get_link_types()
FRONTEND_REWRITER = MultiRuleRewriter([
LinkRewriter(link_rules), EmbedRewriter(embed_rules)
LinkRewriter({linktype: handler.expand_db_attributes for linktype, handler in link_rules.items()}),
EmbedRewriter({embedtype: handler.expand_db_attributes for embedtype, handler in embed_rules.items()})
])
return FRONTEND_REWRITER(html)

Wyświetl plik

@ -3,9 +3,11 @@ from django.utils.html import escape
from wagtail.core.models import Page
def page_linktype_handler(attrs):
try:
page = Page.objects.get(id=attrs['id'])
return '<a href="%s">' % escape(page.specific.url)
except Page.DoesNotExist:
return "<a>"
class PageLinkHandler:
@staticmethod
def expand_db_attributes(attrs):
try:
page = Page.objects.get(id=attrs['id'])
return '<a href="%s">' % escape(page.specific.url)
except Page.DoesNotExist:
return "<a>"

Wyświetl plik

@ -4,7 +4,7 @@ from django.test import TestCase
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 page_linktype_handler
from wagtail.core.rich_text.pages import PageLinkHandler
from wagtail.core.rich_text.rewriters import LinkRewriter, extract_attrs
@ -12,11 +12,11 @@ class TestPageLinktypeHandler(TestCase):
fixtures = ['test.json']
def test_expand_db_attributes_page_does_not_exist(self):
result = page_linktype_handler({'id': 0})
result = PageLinkHandler.expand_db_attributes({'id': 0})
self.assertEqual(result, '<a>')
def test_expand_db_attributes_not_for_editor(self):
result = page_linktype_handler({'id': 1})
result = PageLinkHandler.expand_db_attributes({'id': 1})
self.assertEqual(result, '<a href="None">')

Wyświetl plik

@ -5,7 +5,7 @@ from django.urls import reverse
from wagtail.core import hooks
from wagtail.core.models import PageViewRestriction
from wagtail.core.rich_text.pages import page_linktype_handler
from wagtail.core.rich_text.pages import PageLinkHandler
def require_wagtail_login(next):
@ -40,7 +40,7 @@ def register_core_features(features):
features.default_features.append('hr')
features.default_features.append('link')
features.register_link_type('page', page_linktype_handler)
features.register_link_type('page', PageLinkHandler)
features.default_features.append('bold')

Wyświetl plik

@ -5,10 +5,12 @@ from wagtail.documents.models import get_document_model
# Front-end conversion
def document_linktype_handler(attrs):
Document = get_document_model()
try:
doc = Document.objects.get(id=attrs['id'])
return '<a href="%s">' % escape(doc.url)
except (Document.DoesNotExist, KeyError):
return "<a>"
class DocumentLinkHandler:
@staticmethod
def expand_db_attributes(attrs):
Document = get_document_model()
try:
doc = Document.objects.get(id=attrs['id'])
return '<a href="%s">' % escape(doc.url)
except (Document.DoesNotExist, KeyError):
return "<a>"

Wyświetl plik

@ -1,8 +1,8 @@
from bs4 import BeautifulSoup
from django.test import TestCase
from wagtail.documents.rich_text import document_linktype_handler
from wagtail.documents.rich_text.editor_html import DocumentLinkHandler
from wagtail.documents.rich_text import DocumentLinkHandler as DocumentFrontendHandler
from wagtail.documents.rich_text.editor_html import DocumentLinkHandler as DocumentEditorHTMLHandler
class TestDocumentRichTextLinkHandler(TestCase):
@ -11,33 +11,33 @@ class TestDocumentRichTextLinkHandler(TestCase):
def test_get_db_attributes(self):
soup = BeautifulSoup('<a data-id="test-id">foo</a>', 'html5lib')
tag = soup.a
result = DocumentLinkHandler.get_db_attributes(tag)
result = DocumentEditorHTMLHandler.get_db_attributes(tag)
self.assertEqual(result,
{'id': 'test-id'})
def test_expand_db_attributes(self):
result = document_linktype_handler({'id': 1})
result = DocumentFrontendHandler.expand_db_attributes({'id': 1})
self.assertEqual(result,
'<a href="/documents/1/test.pdf">')
def test_expand_db_attributes_document_does_not_exist(self):
result = document_linktype_handler({'id': 0})
result = DocumentFrontendHandler.expand_db_attributes({'id': 0})
self.assertEqual(result, '<a>')
def test_expand_db_attributes_with_missing_id(self):
result = document_linktype_handler({})
result = DocumentFrontendHandler.expand_db_attributes({})
self.assertEqual(result, '<a>')
def test_expand_db_attributes_for_editor(self):
result = DocumentLinkHandler.expand_db_attributes({'id': 1})
result = DocumentEditorHTMLHandler.expand_db_attributes({'id': 1})
self.assertEqual(result,
'<a data-linktype="document" data-id="1" href="/documents/1/test.pdf">')
def test_expand_db_attributes_for_editor_preserves_id_of_nonexistent_document(self):
result = DocumentLinkHandler.expand_db_attributes({'id': 0})
result = DocumentEditorHTMLHandler.expand_db_attributes({'id': 0})
self.assertEqual(result,
'<a data-linktype="document" data-id="0">')
def test_expand_db_attributes_for_editor_with_missing_id(self):
result = DocumentLinkHandler.expand_db_attributes({})
result = DocumentEditorHTMLHandler.expand_db_attributes({})
self.assertEqual(result, '<a data-linktype="document">')

Wyświetl plik

@ -19,7 +19,7 @@ from wagtail.documents.api.admin.endpoints import DocumentsAdminAPIEndpoint
from wagtail.documents.forms import GroupDocumentPermissionFormSet
from wagtail.documents.models import get_document_model
from wagtail.documents.permissions import permission_policy
from wagtail.documents.rich_text import document_linktype_handler
from wagtail.documents.rich_text import DocumentLinkHandler
from wagtail.documents.rich_text.contentstate import ContentstateDocumentLinkConversionRule
from wagtail.documents.rich_text.editor_html import EditorHTMLDocumentLinkConversionRule
@ -68,7 +68,7 @@ def editor_js():
@hooks.register('register_rich_text_features')
def register_document_feature(features):
features.register_link_type('document', document_linktype_handler)
features.register_link_type('document', DocumentLinkHandler)
features.register_editor_plugin(
'hallo', 'document-link',

Wyświetl plik

@ -3,9 +3,11 @@ from wagtail.embeds import format
# Front-end conversion
def media_embedtype_handler(attrs):
"""
Given a dict of attributes from the <embed> tag, return the real HTML
representation for use on the front-end.
"""
return format.embed_to_frontend_html(attrs['url'])
class MediaEmbedHandler:
@staticmethod
def expand_db_attributes(attrs):
"""
Given a dict of attributes from the <embed> tag, return the real HTML
representation for use on the front-end.
"""
return format.embed_to_frontend_html(attrs['url'])

Wyświetl plik

@ -21,8 +21,8 @@ from wagtail.embeds.finders.embedly import EmbedlyFinder as EmbedlyFinder
from wagtail.embeds.finders.embedly import AccessDeniedEmbedlyException, EmbedlyException
from wagtail.embeds.finders.oembed import OEmbedFinder as OEmbedFinder
from wagtail.embeds.models import Embed
from wagtail.embeds.rich_text import media_embedtype_handler
from wagtail.embeds.rich_text.editor_html import MediaEmbedHandler
from wagtail.embeds.rich_text import MediaEmbedHandler as MediaFrontendHandler
from wagtail.embeds.rich_text.editor_html import MediaEmbedHandler as MediaEditorHTMLHandler
from wagtail.embeds.templatetags.wagtailembeds_tags import embed_tag
from wagtail.tests.utils import WagtailTestUtils
@ -566,11 +566,11 @@ class TestEmbedBlock(TestCase):
EmbedValue('http://no-oembed-here.com/something'))
class TestMediaEmbedHandler(TestCase):
class TestMediaEmbedHandlers(TestCase):
def test_get_db_attributes(self):
soup = BeautifulSoup('<b data-url="test-url">foo</b>', 'html5lib')
tag = soup.b
result = MediaEmbedHandler.get_db_attributes(tag)
result = MediaEditorHTMLHandler.get_db_attributes(tag)
self.assertEqual(result,
{'url': 'test-url'})
@ -589,7 +589,7 @@ class TestMediaEmbedHandler(TestCase):
height=1000,
)
result = MediaEmbedHandler.expand_db_attributes(
result = MediaEditorHTMLHandler.expand_db_attributes(
{'url': 'http://www.youtube.com/watch/'}
)
self.assertIn(
@ -609,7 +609,7 @@ class TestMediaEmbedHandler(TestCase):
def test_test_expand_db_attributes_for_editor_catches_embed_not_found(self, get_embed):
get_embed.side_effect = EmbedNotFoundException
result = MediaEmbedHandler.expand_db_attributes(
result = MediaEditorHTMLHandler.expand_db_attributes(
{'url': 'http://www.youtube.com/watch/'},
)
@ -630,7 +630,7 @@ class TestMediaEmbedHandler(TestCase):
height=1000,
)
result = media_embedtype_handler(
result = MediaFrontendHandler.expand_db_attributes(
{'url': 'http://www.youtube.com/watch/'}
)
self.assertIn('test html', result)
@ -639,7 +639,7 @@ class TestMediaEmbedHandler(TestCase):
def test_expand_db_attributes_catches_embed_not_found(self, get_embed):
get_embed.side_effect = EmbedNotFoundException
result = media_embedtype_handler(
result = MediaFrontendHandler.expand_db_attributes(
{'url': 'http://www.youtube.com/watch/'}
)

Wyświetl plik

@ -7,7 +7,7 @@ import wagtail.admin.rich_text.editors.draftail.features as draftail_features
from wagtail.admin.rich_text import HalloPlugin
from wagtail.core import hooks
from wagtail.embeds import urls
from wagtail.embeds.rich_text import media_embedtype_handler
from wagtail.embeds.rich_text import MediaEmbedHandler
from wagtail.embeds.rich_text.contentstate import ContentstateMediaConversionRule
from wagtail.embeds.rich_text.editor_html import EditorHTMLEmbedConversionRule
@ -34,7 +34,7 @@ def editor_js():
@hooks.register('register_rich_text_features')
def register_embed_feature(features):
# define a handler for converting <embed embedtype="media"> tags into frontend HTML
features.register_embed_type('media', media_embedtype_handler)
features.register_embed_type('media', MediaEmbedHandler)
# define a hallo.js plugin to use when the 'embed' feature is active
features.register_editor_plugin(

Wyświetl plik

@ -4,16 +4,18 @@ from wagtail.images.formats import get_image_format
# Front-end conversion
def image_embedtype_handler(attrs):
"""
Given a dict of attributes from the <embed> tag, return the real HTML
representation for use on the front-end.
"""
Image = get_image_model()
try:
image = Image.objects.get(id=attrs['id'])
except Image.DoesNotExist:
return "<img>"
class ImageEmbedHandler:
@staticmethod
def expand_db_attributes(attrs):
"""
Given a dict of attributes from the <embed> tag, return the real HTML
representation for use on the front-end.
"""
Image = get_image_model()
try:
image = Image.objects.get(id=attrs['id'])
except Image.DoesNotExist:
return "<img>"
image_format = get_image_format(attrs['format'])
return image_format.image_to_html(image, attrs.get('alt', ''))
image_format = get_image_format(attrs['format'])
return image_format.image_to_html(image, attrs.get('alt', ''))

Wyświetl plik

@ -1,33 +1,33 @@
from bs4 import BeautifulSoup
from django.test import TestCase
from wagtail.images.rich_text import image_embedtype_handler
from wagtail.images.rich_text.editor_html import ImageEmbedHandler
from wagtail.images.rich_text import ImageEmbedHandler as ImageFrontendHandler
from wagtail.images.rich_text.editor_html import ImageEmbedHandler as ImageEditorHTMLHandler
from wagtail.tests.utils import WagtailTestUtils
from .utils import Image, get_test_image_file
class TestImageEmbedHandler(TestCase, WagtailTestUtils):
class TestImageEmbedHandlers(TestCase, WagtailTestUtils):
def test_get_db_attributes(self):
soup = BeautifulSoup(
'<b data-id="test-id" data-format="test-format" data-alt="test-alt">foo</b>',
'html5lib'
)
tag = soup.b
result = ImageEmbedHandler.get_db_attributes(tag)
result = ImageEditorHTMLHandler.get_db_attributes(tag)
self.assertEqual(result,
{'alt': 'test-alt',
'id': 'test-id',
'format': 'test-format'})
def test_expand_db_attributes_image_does_not_exist(self):
result = image_embedtype_handler({'id': 0})
result = ImageFrontendHandler.expand_db_attributes({'id': 0})
self.assertEqual(result, '<img>')
def test_expand_db_attributes_not_for_editor(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = image_embedtype_handler(
result = ImageFrontendHandler.expand_db_attributes(
{'id': 1,
'alt': 'test-alt',
'format': 'left'}
@ -36,7 +36,7 @@ class TestImageEmbedHandler(TestCase, WagtailTestUtils):
def test_expand_db_attributes_escapes_alt_text(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = image_embedtype_handler(
result = ImageFrontendHandler.expand_db_attributes(
{'id': 1,
'alt': 'Arthur "two sheds" Jackson',
'format': 'left'},
@ -45,7 +45,7 @@ class TestImageEmbedHandler(TestCase, WagtailTestUtils):
def test_expand_db_attributes_with_missing_alt(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = image_embedtype_handler(
result = ImageFrontendHandler.expand_db_attributes(
{'id': 1,
'format': 'left'},
)
@ -53,7 +53,7 @@ class TestImageEmbedHandler(TestCase, WagtailTestUtils):
def test_expand_db_attributes_for_editor(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = ImageEmbedHandler.expand_db_attributes(
result = ImageEditorHTMLHandler.expand_db_attributes(
{'id': 1,
'alt': 'test-alt',
'format': 'left'},
@ -64,7 +64,7 @@ class TestImageEmbedHandler(TestCase, WagtailTestUtils):
def test_expand_db_attributes_for_editor_escapes_alt_text(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = ImageEmbedHandler.expand_db_attributes(
result = ImageEditorHTMLHandler.expand_db_attributes(
{'id': 1,
'alt': 'Arthur "two sheds" Jackson',
'format': 'left'},
@ -78,7 +78,7 @@ class TestImageEmbedHandler(TestCase, WagtailTestUtils):
def test_expand_db_attributes_for_editor_with_missing_alt(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
result = ImageEmbedHandler.expand_db_attributes(
result = ImageEditorHTMLHandler.expand_db_attributes(
{'id': 1,
'format': 'left'},
)

Wyświetl plik

@ -14,7 +14,7 @@ from wagtail.images import admin_urls, get_image_model, image_operations
from wagtail.images.api.admin.endpoints import ImagesAdminAPIEndpoint
from wagtail.images.forms import GroupImagePermissionFormSet
from wagtail.images.permissions import permission_policy
from wagtail.images.rich_text import image_embedtype_handler
from wagtail.images.rich_text import ImageEmbedHandler
from wagtail.images.rich_text.contentstate import ContentstateImageConversionRule
from wagtail.images.rich_text.editor_html import EditorHTMLImageConversionRule
@ -61,7 +61,7 @@ def editor_js():
@hooks.register('register_rich_text_features')
def register_image_feature(features):
# define a handler for converting <embed embedtype="image"> tags into frontend HTML
features.register_embed_type('image', image_embedtype_handler)
features.register_embed_type('image', ImageEmbedHandler)
# define a hallo.js plugin to use when the 'image' feature is active
features.register_editor_plugin(