-
Notifications
You must be signed in to change notification settings - Fork 6
/
JSONDetails.py
77 lines (61 loc) · 2.75 KB
/
JSONDetails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
try:
import json
except ImportError:
import simplejson as json
from zope.interface import implements
from zope.component import getUtility
from Products.CMFCore.utils import getToolByName
from Products.TinyMCE.adapters.interfaces.JSONDetails import IJSONDetails
from Products.TinyMCE.interfaces.utility import ITinyMCE
from plone.outputfilters.browser.resolveuid import uuidFor
class JSONDetails(object):
"""Return details of the current object in JSON"""
implements(IJSONDetails)
def __init__(self, context):
"""Constructor"""
self.context = context
def getDetails(self):
"""Builds a JSON object based on the details
of this object.
"""
utility = getUtility(ITinyMCE)
anchor_portal_types = utility.containsanchors.split('\n')
image_portal_types = utility.imageobjects.split('\n')
results = {}
results['title'] = self.context.title_or_id()
results['description'] = self.context.Description()
if self.context.portal_type in image_portal_types:
image_url = self._getPloneUrl() + '/resolveuid/' + uuidFor(self.context)
field_name = 'image'
images = self.context.restrictedTraverse('@@images')
results['thumb'] = '%s/@@images/%s/%s' % (image_url, field_name, 'thumb')
sizes = images.getAvailableSizes(field_name)
scales = [{'value': '@@images/%s/%s' % (field_name, key),
'size': size,
'title': key.capitalize()} for key, size in sizes.items()]
scales.sort(lambda x, y: cmp(x['size'][0], y['size'][0]))
original_size = images.getImageSize(field_name)
if original_size[0] < 0 or original_size[1] < 0:
original_size = (0, 0)
scales.insert(0, {'value': '',
'title': 'Original',
'size': original_size})
results['scales'] = scales
else:
results['thumb'] = ""
if self.context.portal_type in anchor_portal_types:
content_anchors = self.context.restrictedTraverse('@@content_anchors')
results['anchors'] = content_anchors.listAnchorNames()
else:
results['anchors'] = []
results.update(self.additionalDetails())
return json.dumps(results)
def additionalDetails(self):
"""Hook to allow subclasses to supplement or override the default set of results
"""
return {}
def _getPloneUrl(self):
"""Return the URL corresponding to the root of the Plone site."""
portal_url = getToolByName(self.context, 'portal_url')
portal = portal_url.getPortalObject()
return portal.absolute_url()