mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-25 07:13:46 +00:00
implement document links
This commit is contained in:
parent
10453ff58f
commit
95fda6b116
2 changed files with 62 additions and 0 deletions
|
|
@ -6,6 +6,7 @@ from wagtail.admin.rich_text.converters.contentstate_models import (
|
|||
)
|
||||
from wagtail.admin.rich_text.converters.html_ruleset import HTMLRuleset
|
||||
from wagtail.core.models import Page
|
||||
from wagtail.documents.models import get_document_model
|
||||
|
||||
|
||||
class HandlerState(object):
|
||||
|
|
@ -171,6 +172,20 @@ class PageLinkElementHandler(LinkElementHandler):
|
|||
return data
|
||||
|
||||
|
||||
class DocumentLinkElementHandler(LinkElementHandler):
|
||||
def get_attribute_data(self, attrs):
|
||||
Document = get_document_model()
|
||||
try:
|
||||
doc = Document.objects.get(id=attrs['id'])
|
||||
except Document.DoesNotExist:
|
||||
return {}
|
||||
|
||||
return {
|
||||
'id': doc.id,
|
||||
'url': doc.url,
|
||||
}
|
||||
|
||||
|
||||
class AtomicBlockEntityElementHandler(object):
|
||||
"""
|
||||
Handler for elements like <img> that exist as a single immutable item at the block level
|
||||
|
|
@ -237,6 +252,9 @@ ELEMENT_HANDLERS_BY_FEATURE = {
|
|||
'a[href]': ExternalLinkElementHandler('LINK'),
|
||||
'a[linktype="page"]': PageLinkElementHandler('LINK'),
|
||||
},
|
||||
'document-link': {
|
||||
'a[linktype="document"]': DocumentLinkElementHandler('DOCUMENT'),
|
||||
},
|
||||
# 'img': ImageElementHandler(),
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -278,3 +278,47 @@ class TestHtmlToContentState(TestCase):
|
|||
},
|
||||
]
|
||||
})
|
||||
|
||||
def test_document_link(self):
|
||||
converter = ContentstateConverter(features=['document-link'])
|
||||
result = json.loads(converter.from_database_format(
|
||||
'''
|
||||
<p>a <a linktype="document" id="1">document</a> link</p>
|
||||
'''
|
||||
))
|
||||
self.assertContentStateEqual(result, {
|
||||
'entityMap': {
|
||||
'0': {
|
||||
'mutability': 'MUTABLE', 'type': 'DOCUMENT',
|
||||
'data': {'id': 1, 'url': '/documents/1/test.pdf'}
|
||||
}
|
||||
},
|
||||
'blocks': [
|
||||
{
|
||||
'inlineStyleRanges': [], 'text': 'a document link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
|
||||
'entityRanges': [{'offset': 2, 'length': 8, 'key': 0}]
|
||||
},
|
||||
]
|
||||
})
|
||||
|
||||
def test_broken_document_link(self):
|
||||
converter = ContentstateConverter(features=['document-link'])
|
||||
result = json.loads(converter.from_database_format(
|
||||
'''
|
||||
<p>a <a linktype="document" id="9999">document</a> link</p>
|
||||
'''
|
||||
))
|
||||
self.assertContentStateEqual(result, {
|
||||
'entityMap': {
|
||||
'0': {
|
||||
'mutability': 'MUTABLE', 'type': 'DOCUMENT',
|
||||
'data': {}
|
||||
}
|
||||
},
|
||||
'blocks': [
|
||||
{
|
||||
'inlineStyleRanges': [], 'text': 'a document link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
|
||||
'entityRanges': [{'offset': 2, 'length': 8, 'key': 0}]
|
||||
},
|
||||
]
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue