Extend attachment shortcode rendering

The previous implementation only supported rendering
attachments within the context of a node or project document.
Now it also supports node.properties. This is a temporary
solution, as noted in the TODO comments.
This commit is contained in:
2018-09-15 19:01:58 +02:00
parent 263d68071e
commit f8d992400e
2 changed files with 51 additions and 27 deletions

View File

@@ -187,37 +187,48 @@ class AttachmentTest(AbstractPillarTest):
],
'filename': 'cute_kitten.jpg',
})
node_doc = {'properties': {
'attachments': {
'img': {'oid': oid},
}
node_properties = {'attachments': {
'img': {'oid': oid},
}}
node_doc = {'properties': node_properties}
# Collect the two possible context that can be provided for attachemt
# rendering. See pillar.shortcodes.sdk_file for more info.
possible_contexts = [node_properties, node_doc]
# We have to get the file document again, because retrieving it via the
# API (which is what the shortcode rendering is doing) will change its
# link URL.
db_file = self.get(f'/api/files/{oid}').get_json()
link = db_file['variations'][0]['link']
with self.app.test_request_context():
self_linked = f'<a class="expand-image-links" href="{link}">' \
f'<img src="{link}" alt="cute_kitten.jpg"/></a>'
self.assertEqual(
self_linked,
render('{attachment img link}', context=node_doc).strip()
)
self.assertEqual(
self_linked,
render('{attachment img link=self}', context=node_doc).strip()
)
self.assertEqual(
f'<img src="{link}" alt="cute_kitten.jpg"/>',
render('{attachment img}', context=node_doc).strip()
)
def do_render(context, link):
"""Utility to run attachment rendering in different contexts."""
with self.app.test_request_context():
self_linked = f'<a class="expand-image-links" href="{link}">' \
f'<img src="{link}" alt="cute_kitten.jpg"/></a>'
self.assertEqual(
self_linked,
render('{attachment img link}', context=context).strip()
)
self.assertEqual(
self_linked,
render('{attachment img link=self}', context=context).strip()
)
self.assertEqual(
f'<img src="{link}" alt="cute_kitten.jpg"/>',
render('{attachment img}', context=context).strip()
)
tag_link = 'https://i.imgur.com/FmbuPNe.jpg'
self.assertEqual(
f'<a href="{tag_link}" target="_blank">'
f'<img src="{link}" alt="cute_kitten.jpg"/></a>',
render('{attachment img link=%r}' % tag_link, context=node_doc).strip()
)
tag_link = 'https://i.imgur.com/FmbuPNe.jpg'
self.assertEqual(
f'<a href="{tag_link}" target="_blank">'
f'<img src="{link}" alt="cute_kitten.jpg"/></a>',
render('{attachment img link=%r}' % tag_link, context=context).strip()
)
# Test both possible contexts for rendering attachments
for context in possible_contexts:
do_render(context, link)