Introducing image attachments

Before returning a response, check if the 'attachments' property is
defined. If yes, load the file (for the moment only images) in the
required variation, get the link and build a Markdown representation.
Search in the 'field' specified in the attachment and replace the
'slug' tag with the generated link.
This commit is contained in:
Francesco Siddi 2015-11-16 12:32:42 +01:00
parent a96526351b
commit 5b6feac154
2 changed files with 39 additions and 7 deletions

View File

@ -344,8 +344,33 @@ def before_inserting_nodes(items):
if project:
item['project'] = project['_id']
def parse_attachments(response):
"""Before returning a response, check if the 'attachments' property is
defined. If yes, load the file (for the moment only images) in the required
variation, get the link and build a Markdown representation. Search in the
'field' specified in the attachmentand replace the 'slug' tag with the
generated link.
"""
if 'properties' in response and 'attachments' in response['properties']:
files_collection = app.data.driver.db['files']
for field in response['properties']['attachments']:
for attachment in response['properties']['attachments']:
field_name = attachment['field']
field_content = response[field_name]
for f in attachment['files']:
slug = f['slug']
slug_tag = "[{0}]".format(slug)
f = files_collection.find_one({'_id': f['file']})
size = f['size'] if 'size' in f else 'l'
p = files_collection.find_one({'parent': f['_id'], 'size': size})
l = generate_link(p['backend'], p['file_path'], str(p['project']))
# Build Markdown img string
l = '![{0}]({1} "{2}")'.format(slug, l, f['name'])
response[field_name] = field_content.replace(slug_tag, l)
app.on_fetched_item_nodes += before_returning_item_permissions
app.on_fetched_item_nodes += parse_attachments
app.on_fetched_resource_nodes += before_returning_resource_permissions
app.on_fetched_item_node_types += before_returning_item_permissions
app.on_fetched_resource_node_types += before_returning_resource_permissions

View File

@ -26,15 +26,22 @@ node_type_asset = {
# We point to the original file (and use it to extract any relevant
# variation useful for our scope).
'file': _file_embedded_schema,
'description_attachments': {
'attachments': {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'field': {'type': 'string'},
'files' : {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'file': _file_embedded_schema,
'url': {
'type': 'string',
'minlength': 1
'slug': {'type': 'string', 'minlength': 1},
'size': {'type': 'string'}
}
}
}
}
}
@ -44,7 +51,7 @@ node_type_asset = {
'status': {},
'content_type': {'visible': False},
'file': {'visible': False},
'description_attachments': {'visible': False},
'attachments': {'visible': False},
},
'permissions': {
# 'groups': [{