Test coverage for vnccollab.theme.browser.wizardjson
1: import simplejson
1: from Acquisition import aq_parent
1: from AccessControl import getSecurityManager
1: from zope.component import getMultiAdapter
1: from Products import AdvancedQuery
1: from Products.AdvancedQuery import MatchGlob, Eq, In
1: from Products.Five.browser import BrowserView
1: from Products.CMFPlone.utils import safe_unicode
1: from Products.CMFCore import permissions
1: from Products.CMFCore.utils import getToolByName
1: from Products.CMFCore.interfaces import ISiteRoot, IFolderish
1: from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
1: from plone import api
1: from plone.uuid.interfaces import IUUID
1: from plone.app.search.browser import Search
1: from plone.app.contentlisting.interfaces import IContentListing
2: class GetTreeJson(BrowserView):
'''Returns a JSON representation of the directory structure
1: to be used by jquery.dynatree library.'''
1: def getSearchDestinationList(self, type_=None):
""" Return list of destinatons """
>>>>>> catalog = getToolByName(self.context, 'portal_catalog')
>>>>>> portal = api.portal.get()
>>>>>> container_path = '/'.join(portal.getPhysicalPath())
query = (MatchGlob('Title',
self.request.get('SearchableText', '') + '*') | \
MatchGlob('Description',
self.request.get('SearchableText', '') + '*')) & \
>>>>>> Eq('path', container_path) & \
>>>>>> In('portal_type', self._get_container_types())
>>>>>> obj = lambda o: o if ISiteRoot.providedBy(o) else o.getObject()
>>>>>> results = IContentListing(catalog.evalAdvancedQuery(query))
>>>>>> results = [self._info_from_content(x) for x in results
>>>>>> if self._is_container_selectable(obj(x), type_)]
>>>>>> results.sort(lambda x, y: cmp(x['title'], y['title']))
>>>>>> return simplejson.dumps(results)
1: def getInitialTree(self):
'''Returns the initial tree for the dynatree.'''
2: context = self.getFolderishParent(self.context)
2: child_tree = None
2: child_uid = None
3: while True:
3: is_root = ISiteRoot.providedBy(context)
3: if is_root:
2: uid = None
else:
1: uid = IUUID(context)
3: tree = self.get_tree(uid)
3: if child_tree is not None:
1: self._insert_child_tree(tree, child_tree, child_uid)
3: if is_root:
2: break
1: context = aq_parent(context)
1: child_tree = tree
1: child_uid = uid
2: root_node = self.getRootNode()
2: root_node['children'] = tree
2: return simplejson.dumps(root_node)
1: def getRootNode(self):
3: portal = api.portal.get()
3: tree = self._info_from_content(portal)
3: return tree
1: def getFolderishParent(self, obj):
'''Returns obj or its nearest parent that is folderish.'''
24: while True:
24: if IFolderish.providedBy(obj):
24: return obj
>>>>>> obj = aq_parent(obj)
1: def _insert_child_tree(self, tree, child_tree, child_uid):
5: for branch in tree:
4: if branch.get('key', False) == child_uid:
1: branch['children'] = child_tree
1: branch['isLazy'] = False
1: branch['expand'] = True
1: def getTree(self, uid=None, type_=None):
'''Returns the (lazy) tree for a given node.
params:
uid: UUID of the container to get its tree.
'''
1: results = self.get_tree(uid=uid)
1: return simplejson.dumps(results)
1: def get_tree(self, uid=None, type_=None):
6: if type_ is None:
6: type_ = self.request.get('type_', None)
6: catalog = getToolByName(self.context, 'portal_catalog')
6: container_path = ''
6: if uid is not None:
2: container = catalog(UID=uid)
2: if len(container) == 1:
1: container = container[0]
1: container_path = container.getPath()
6: if not container_path:
5: portal = api.portal.get()
5: container_path = '/'.join(portal.getPhysicalPath())
6: query = {'portal_type': self._get_container_types(),
6: 'path': {'query': container_path, 'depth': 1}}
6: results = IContentListing(catalog(**query))
23: results = [self._info_from_content(x, type_) for x in results]
26: results.sort(lambda x, y: cmp(x['title'], y['title']))
6: return results
1: def _info_from_content(self, content, type_=None, search_html=False):
20: content_is_root = ISiteRoot.providedBy(content)
20: if content_is_root:
3: content_uid = '0'
3: obj = content
3: path = content.absolute_url_path()
3: url = content.absolute_url()
else:
17: obj = content.getObject()
17: path = content.getPath()
17: url = content.getURL()
17: content_uid = content.uuid()
20: selectable = self._is_container_selectable(obj, type_)
20: context = self.getFolderishParent(self.context)
20: context_is_root = ISiteRoot.providedBy(context)
20: if context_is_root:
15: context_uid = '0'
else:
5: context_uid = IUUID(context)
20: i_am_context = context_uid == content_uid
20: result = {
20: 'key': content_uid,
20: 'id': content.getId(),
20: 'title': safe_unicode(content.Title()).encode('utf-8'),
20: 'tooltip': safe_unicode(content.Description()).encode('utf-8'),
20: 'icon': content.getIcon(),
20: 'noLink': True,
20: 'isFolder': True,
20: 'isLazy': True,
20: 'path': path,
20: 'url': url,
20: 'unselectable': not(selectable),
20: 'activate': selectable and i_am_context,
20: 'children': [],
}
20: return result
1: def _get_container_types(self):
6: return ['Folder', 'CastsContainer']
1: def _is_container_selectable(self, container, type_):
20: writable = self._is_container_writable(container)
20: if not writable:
>>>>>> return False
20: return self._is_type_allowed_in_container(container, type_)
1: def _is_type_allowed_in_container(self, obj, type_):
20: if type_ is None:
20: return True
>>>>>> allowed_types = list(obj.getLocallyAllowedTypes())
>>>>>> return type_ in allowed_types
1: def _is_container_writable(self, obj):
'''True if the current user can write in the object container.
NOTE: We need to access to the associate object, and this could
be time consuming. In case of degradation of speed, check here.
'''
20: perm = getSecurityManager().checkPermission(
20: permissions.AddPortalContent, obj)
20: return perm == 1