Skip to content

Commit

Permalink
Add versions attribute to image json view.
Browse files Browse the repository at this point in the history
  • Loading branch information
tisto committed Mar 27, 2015
1 parent 2823289 commit 9709a99
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion docs/source/_json/image.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@
"description": "An image",
"download": "http://localhost:55001/plone/image/@@download",
"portal_type": "Image",
"title": "Image"
"title": "Image",
"versions": [
"http://localhost:55001/plone/image/@@images/image/large",
"http://localhost:55001/plone/image/@@images/image/preview",
"http://localhost:55001/plone/image/@@images/image/mini",
"http://localhost:55001/plone/image/@@images/image/thumb",
"http://localhost:55001/plone/image/@@images/image/tile",
"http://localhost:55001/plone/image/@@images/image/icon",
"http://localhost:55001/plone/image/@@images/image/listing"
]
}
11 changes: 11 additions & 0 deletions src/plone/restapi/adapter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from Products.CMFCore.interfaces import IContentish
from Products.CMFCore.interfaces import IFolderish
from Products.CMFCore.interfaces import IPropertiesTool
from Products.CMFPlone.interfaces import IPloneSiteRoot

from plone.app.textfield import RichText
Expand All @@ -12,6 +13,7 @@
from zope.schema import Datetime
from zope.interface import implementer
from zope.component import adapter
from zope.component import getUtility

import json

Expand Down Expand Up @@ -104,6 +106,9 @@ def SerializeFileToJson(context):
@implementer(ISerializeToJson)
@adapter(IImage)
def SerializeImageToJson(context):
ptool = getUtility(IPropertiesTool)
image_properties = ptool.imaging_properties
allowed_sizes = image_properties.getProperty('allowed_sizes')
result = {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@id": context.absolute_url(),
Expand All @@ -112,5 +117,11 @@ def SerializeImageToJson(context):
'title': context.title,
'description': context.description,
'download': '{0}/@@download'.format(context.absolute_url()),
'versions': [
'{0}/@@images/image/{1}'.format(
context.absolute_url(),
x.split(' ')[0]
) for x in allowed_sizes
]
}
return json.dumps(result, indent=2, sort_keys=True)

0 comments on commit 9709a99

Please sign in to comment.