Move PageLinkHandler to a submodule wagtail.core.rich_text.pages

pull/4079/merge
Matt Westcott 2017-11-27 20:49:57 +00:00
rodzic 443a046eb1
commit a6f5514941
3 zmienionych plików z 40 dodań i 37 usunięć

Wyświetl plik

@ -1,11 +1,10 @@
import re # parsing HTML with regexes LIKE A BOSS.
from django.utils.functional import cached_property
from django.utils.html import escape
from django.utils.safestring import mark_safe
from wagtail.core import hooks
from wagtail.core.models import Page
from wagtail.core.rich_text.pages import PageLinkHandler
from wagtail.core.whitelist import allow_without_attributes, Whitelister, DEFAULT_ELEMENT_RULES
@ -16,40 +15,6 @@ from wagtail.core.whitelist import allow_without_attributes, Whitelister, DEFAUL
# to DB representation and back again.
class PageLinkHandler:
"""
PageLinkHandler will be invoked whenever we encounter an <a> element in HTML content
with an attribute of data-linktype="page". The resulting element in the database
representation will be:
<a linktype="page" id="42">hello world</a>
"""
@staticmethod
def get_db_attributes(tag):
"""
Given an <a> tag that we've identified as a page link embed (because it has a
data-linktype="page" attribute), return a dict of the attributes we should
have on the resulting <a linktype="page"> element.
"""
return {'id': tag['data-id']}
@staticmethod
def expand_db_attributes(attrs, for_editor):
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 = ''
return '<a %shref="%s">' % (editor_attrs, escape(page.specific.url))
except Page.DoesNotExist:
return "<a>"
EMBED_HANDLERS = {}
LINK_HANDLERS = {
'page': PageLinkHandler,

Wyświetl plik

@ -0,0 +1,37 @@
from django.utils.html import escape
from wagtail.core.models import Page
class PageLinkHandler:
"""
PageLinkHandler will be invoked whenever we encounter an <a> element in HTML content
with an attribute of data-linktype="page". The resulting element in the database
representation will be:
<a linktype="page" id="42">hello world</a>
"""
@staticmethod
def get_db_attributes(tag):
"""
Given an <a> tag that we've identified as a page link embed (because it has a
data-linktype="page" attribute), return a dict of the attributes we should
have on the resulting <a linktype="page"> element.
"""
return {'id': tag['data-id']}
@staticmethod
def expand_db_attributes(attrs, for_editor):
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 = ''
return '<a %shref="%s">' % (editor_attrs, escape(page.specific.url))
except Page.DoesNotExist:
return "<a>"

Wyświetl plik

@ -4,7 +4,8 @@ from mock import patch
from wagtail.core.models import Page
from wagtail.core.rich_text import (
DbWhitelister, FeatureRegistry, PageLinkHandler, RichText, expand_db_html, extract_attrs)
DbWhitelister, FeatureRegistry, RichText, expand_db_html, extract_attrs)
from wagtail.core.rich_text.pages import PageLinkHandler
class TestPageLinkHandler(TestCase):