kopia lustrzana https://github.com/wagtail/wagtail
Split expand_db_attributes methods of link/embed handlers into for_editor and non-editor methods
rodzic
81f7fc5968
commit
a6eb279652
|
@ -168,12 +168,18 @@ def expand_db_html(html, for_editor=False):
|
|||
# return unchanged
|
||||
return m.group(0)
|
||||
handler = get_link_handler(attrs['linktype'])
|
||||
return handler.expand_db_attributes(attrs, for_editor)
|
||||
if for_editor:
|
||||
return handler.expand_db_attributes_for_editor(attrs)
|
||||
else:
|
||||
return handler.expand_db_attributes(attrs)
|
||||
|
||||
def replace_embed_tag(m):
|
||||
attrs = extract_attrs(m.group(1))
|
||||
handler = get_embed_handler(attrs['embedtype'])
|
||||
return handler.expand_db_attributes(attrs, for_editor)
|
||||
if for_editor:
|
||||
return handler.expand_db_attributes_for_editor(attrs)
|
||||
else:
|
||||
return handler.expand_db_attributes(attrs)
|
||||
|
||||
html = FIND_A_TAG.sub(replace_a_tag, html)
|
||||
html = FIND_EMBED_TAG.sub(replace_embed_tag, html)
|
||||
|
|
|
@ -20,18 +20,23 @@ class PageLinkHandler:
|
|||
return {'id': tag['data-id']}
|
||||
|
||||
@staticmethod
|
||||
def expand_db_attributes(attrs, for_editor):
|
||||
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>"
|
||||
|
||||
@staticmethod
|
||||
def expand_db_attributes_for_editor(attrs):
|
||||
try:
|
||||
page = Page.objects.get(id=attrs['id'])
|
||||
|
||||
if for_editor:
|
||||
editor_attrs = 'data-linktype="page" data-id="%d" ' % page.id
|
||||
parent_page = page.get_parent()
|
||||
if parent_page:
|
||||
editor_attrs += 'data-parent-id="%d" ' % parent_page.id
|
||||
else:
|
||||
editor_attrs = ''
|
||||
attrs = 'data-linktype="page" data-id="%d" ' % page.id
|
||||
parent_page = page.get_parent()
|
||||
if parent_page:
|
||||
attrs += 'data-parent-id="%d" ' % parent_page.id
|
||||
|
||||
return '<a %shref="%s">' % (editor_attrs, escape(page.specific.url))
|
||||
return '<a %shref="%s">' % (attrs, escape(page.specific.url))
|
||||
except Page.DoesNotExist:
|
||||
return "<a>"
|
||||
|
|
|
@ -20,37 +20,25 @@ class TestPageLinkHandler(TestCase):
|
|||
{'id': 'test-id'})
|
||||
|
||||
def test_expand_db_attributes_page_does_not_exist(self):
|
||||
result = PageLinkHandler.expand_db_attributes(
|
||||
{'id': 0},
|
||||
False
|
||||
)
|
||||
result = PageLinkHandler.expand_db_attributes({'id': 0})
|
||||
self.assertEqual(result, '<a>')
|
||||
|
||||
def test_expand_db_attributes_for_editor(self):
|
||||
result = PageLinkHandler.expand_db_attributes(
|
||||
{'id': 1},
|
||||
True
|
||||
)
|
||||
result = PageLinkHandler.expand_db_attributes_for_editor({'id': 1})
|
||||
self.assertEqual(
|
||||
result,
|
||||
'<a data-linktype="page" data-id="1" href="None">'
|
||||
)
|
||||
|
||||
events_page_id = Page.objects.get(url_path='/home/events/').pk
|
||||
result = PageLinkHandler.expand_db_attributes(
|
||||
{'id': events_page_id},
|
||||
True
|
||||
)
|
||||
result = PageLinkHandler.expand_db_attributes_for_editor({'id': events_page_id})
|
||||
self.assertEqual(
|
||||
result,
|
||||
'<a data-linktype="page" data-id="%d" data-parent-id="2" href="/events/">' % events_page_id
|
||||
)
|
||||
|
||||
def test_expand_db_attributes_not_for_editor(self):
|
||||
result = PageLinkHandler.expand_db_attributes(
|
||||
{'id': 1},
|
||||
False
|
||||
)
|
||||
result = PageLinkHandler.expand_db_attributes({'id': 1})
|
||||
self.assertEqual(result, '<a href="None">')
|
||||
|
||||
|
||||
|
|
|
@ -9,16 +9,19 @@ class DocumentLinkHandler:
|
|||
return {'id': tag['data-id']}
|
||||
|
||||
@staticmethod
|
||||
def expand_db_attributes(attrs, for_editor):
|
||||
def expand_db_attributes(attrs):
|
||||
Document = get_document_model()
|
||||
try:
|
||||
doc = Document.objects.get(id=attrs['id'])
|
||||
|
||||
if for_editor:
|
||||
editor_attrs = 'data-linktype="document" data-id="%d" ' % doc.id
|
||||
else:
|
||||
editor_attrs = ''
|
||||
|
||||
return '<a %shref="%s">' % (editor_attrs, escape(doc.url))
|
||||
return '<a href="%s">' % escape(doc.url)
|
||||
except Document.DoesNotExist:
|
||||
return "<a>"
|
||||
|
||||
@staticmethod
|
||||
def expand_db_attributes_for_editor(attrs):
|
||||
Document = get_document_model()
|
||||
try:
|
||||
doc = Document.objects.get(id=attrs['id'])
|
||||
return '<a data-linktype="document" data-id="%d" href="%s">' % (doc.id, escape(doc.url))
|
||||
except Document.DoesNotExist:
|
||||
return "<a>"
|
||||
|
|
|
@ -15,24 +15,15 @@ class TestDocumentRichTextLinkHandler(TestCase):
|
|||
{'id': 'test-id'})
|
||||
|
||||
def test_expand_db_attributes_document_does_not_exist(self):
|
||||
result = DocumentLinkHandler.expand_db_attributes(
|
||||
{'id': 0},
|
||||
False
|
||||
)
|
||||
result = DocumentLinkHandler.expand_db_attributes({'id': 0})
|
||||
self.assertEqual(result, '<a>')
|
||||
|
||||
def test_expand_db_attributes_for_editor(self):
|
||||
result = DocumentLinkHandler.expand_db_attributes(
|
||||
{'id': 1},
|
||||
True
|
||||
)
|
||||
result = DocumentLinkHandler.expand_db_attributes_for_editor({'id': 1})
|
||||
self.assertEqual(result,
|
||||
'<a data-linktype="document" data-id="1" href="/documents/1/test.pdf">')
|
||||
|
||||
def test_expand_db_attributes_not_for_editor(self):
|
||||
result = DocumentLinkHandler.expand_db_attributes(
|
||||
{'id': 1},
|
||||
False
|
||||
)
|
||||
result = DocumentLinkHandler.expand_db_attributes({'id': 1})
|
||||
self.assertEqual(result,
|
||||
'<a href="/documents/1/test.pdf">')
|
||||
|
|
|
@ -21,16 +21,21 @@ class MediaEmbedHandler:
|
|||
}
|
||||
|
||||
@staticmethod
|
||||
def expand_db_attributes(attrs, for_editor):
|
||||
def expand_db_attributes(attrs):
|
||||
"""
|
||||
Given a dict of attributes from the <embed> tag, return the real HTML
|
||||
representation.
|
||||
representation for use on the front-end.
|
||||
"""
|
||||
if for_editor:
|
||||
try:
|
||||
return format.embed_to_editor_html(attrs['url'])
|
||||
except EmbedException:
|
||||
# Could be replaced with a nice error message
|
||||
return ''
|
||||
else:
|
||||
return format.embed_to_frontend_html(attrs['url'])
|
||||
return format.embed_to_frontend_html(attrs['url'])
|
||||
|
||||
@staticmethod
|
||||
def expand_db_attributes_for_editor(attrs):
|
||||
"""
|
||||
Given a dict of attributes from the <embed> tag, return the real HTML
|
||||
representation for use within the editor.
|
||||
"""
|
||||
try:
|
||||
return format.embed_to_editor_html(attrs['url'])
|
||||
except EmbedException:
|
||||
# Could be replaced with a nice error message
|
||||
return ''
|
||||
|
|
|
@ -557,9 +557,8 @@ class TestMediaEmbedHandler(TestCase):
|
|||
height=1000,
|
||||
)
|
||||
|
||||
result = MediaEmbedHandler.expand_db_attributes(
|
||||
{'url': 'http://www.youtube.com/watch/'},
|
||||
True
|
||||
result = MediaEmbedHandler.expand_db_attributes_for_editor(
|
||||
{'url': 'http://www.youtube.com/watch/'}
|
||||
)
|
||||
self.assertIn(
|
||||
(
|
||||
|
@ -578,9 +577,8 @@ 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 = MediaEmbedHandler.expand_db_attributes_for_editor(
|
||||
{'url': 'http://www.youtube.com/watch/'},
|
||||
True
|
||||
)
|
||||
|
||||
self.assertEqual(result, '')
|
||||
|
@ -601,8 +599,7 @@ class TestMediaEmbedHandler(TestCase):
|
|||
)
|
||||
|
||||
result = MediaEmbedHandler.expand_db_attributes(
|
||||
{'url': 'http://www.youtube.com/watch/'},
|
||||
False
|
||||
{'url': 'http://www.youtube.com/watch/'}
|
||||
)
|
||||
self.assertIn('test html', result)
|
||||
|
||||
|
@ -611,8 +608,7 @@ class TestMediaEmbedHandler(TestCase):
|
|||
get_embed.side_effect = EmbedNotFoundException
|
||||
|
||||
result = MediaEmbedHandler.expand_db_attributes(
|
||||
{'url': 'http://www.youtube.com/watch/'},
|
||||
False
|
||||
{'url': 'http://www.youtube.com/watch/'}
|
||||
)
|
||||
|
||||
self.assertEqual(result, '')
|
||||
|
|
|
@ -23,10 +23,25 @@ class ImageEmbedHandler:
|
|||
}
|
||||
|
||||
@staticmethod
|
||||
def expand_db_attributes(attrs, for_editor):
|
||||
def expand_db_attributes(attrs):
|
||||
"""
|
||||
Given a dict of attributes from the <embed> tag, return the real HTML
|
||||
representation.
|
||||
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', ''))
|
||||
|
||||
@staticmethod
|
||||
def expand_db_attributes_for_editor(attrs):
|
||||
"""
|
||||
Given a dict of attributes from the <embed> tag, return the real HTML
|
||||
representation for use within the editor.
|
||||
"""
|
||||
Image = get_image_model()
|
||||
try:
|
||||
|
@ -36,7 +51,4 @@ class ImageEmbedHandler:
|
|||
|
||||
image_format = get_image_format(attrs['format'])
|
||||
|
||||
if for_editor:
|
||||
return image_format.image_to_editor_html(image, attrs.get('alt', ''))
|
||||
else:
|
||||
return image_format.image_to_html(image, attrs.get('alt', ''))
|
||||
return image_format.image_to_editor_html(image, attrs.get('alt', ''))
|
||||
|
|
|
@ -19,11 +19,8 @@ class TestImageEmbedHandler(TestCase):
|
|||
'id': 'test-id',
|
||||
'format': 'test-format'})
|
||||
|
||||
def test_expand_db_attributes_page_does_not_exist(self):
|
||||
result = ImageEmbedHandler.expand_db_attributes(
|
||||
{'id': 0},
|
||||
False
|
||||
)
|
||||
def test_expand_db_attributes_image_does_not_exist(self):
|
||||
result = ImageEmbedHandler.expand_db_attributes({'id': 0})
|
||||
self.assertEqual(result, '<img>')
|
||||
|
||||
def test_expand_db_attributes_not_for_editor(self):
|
||||
|
@ -31,8 +28,7 @@ class TestImageEmbedHandler(TestCase):
|
|||
result = ImageEmbedHandler.expand_db_attributes(
|
||||
{'id': 1,
|
||||
'alt': 'test-alt',
|
||||
'format': 'left'},
|
||||
False
|
||||
'format': 'left'}
|
||||
)
|
||||
self.assertIn('<img class="richtext-image left"', result)
|
||||
|
||||
|
@ -42,7 +38,6 @@ class TestImageEmbedHandler(TestCase):
|
|||
{'id': 1,
|
||||
'alt': 'Arthur "two sheds" Jackson',
|
||||
'format': 'left'},
|
||||
False
|
||||
)
|
||||
self.assertIn('alt="Arthur "two sheds" Jackson"', result)
|
||||
|
||||
|
@ -51,18 +46,16 @@ class TestImageEmbedHandler(TestCase):
|
|||
result = ImageEmbedHandler.expand_db_attributes(
|
||||
{'id': 1,
|
||||
'format': 'left'},
|
||||
False
|
||||
)
|
||||
self.assertIn('<img class="richtext-image left"', result)
|
||||
self.assertIn('alt=""', result)
|
||||
|
||||
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 = ImageEmbedHandler.expand_db_attributes_for_editor(
|
||||
{'id': 1,
|
||||
'alt': 'test-alt',
|
||||
'format': 'left'},
|
||||
True
|
||||
)
|
||||
self.assertIn(
|
||||
'<img data-embedtype="image" data-id="1" data-format="left" '
|
||||
|
@ -71,11 +64,10 @@ class TestImageEmbedHandler(TestCase):
|
|||
|
||||
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 = ImageEmbedHandler.expand_db_attributes_for_editor(
|
||||
{'id': 1,
|
||||
'alt': 'Arthur "two sheds" Jackson',
|
||||
'format': 'left'},
|
||||
True
|
||||
)
|
||||
self.assertIn(
|
||||
'<img data-embedtype="image" data-id="1" data-format="left" '
|
||||
|
@ -85,10 +77,9 @@ class TestImageEmbedHandler(TestCase):
|
|||
|
||||
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 = ImageEmbedHandler.expand_db_attributes_for_editor(
|
||||
{'id': 1,
|
||||
'format': 'left'},
|
||||
True
|
||||
)
|
||||
self.assertIn(
|
||||
'<img data-embedtype="image" data-id="1" data-format="left" '
|
||||
|
|
Ładowanie…
Reference in New Issue