Annotation of kupuMPIWG/plone/kupu_plone_layer/kupuInfoForBrains.py, revision 1.1.1.1

1.1       dwinter     1: ## Script (Python) "kupuInfoForBrains"
                      2: ##title=Provide dictionaries with information about a list of catalog brains
                      3: ##bind container=container
                      4: ##bind context=context
                      5: ##bind namespace=
                      6: ##bind script=script
                      7: ##bind subpath=traverse_subpath
                      8: ##parameters=values, linkhere=False, linkparent=False, showimagesize=False
                      9: from Products.CMFCore.utils import getToolByName
                     10: import AccessControl
                     11: from AccessControl import Unauthorized
                     12: 
                     13: request = context.REQUEST
                     14: response = request.RESPONSE
                     15: response.setHeader('Cache-Control', 'no-cache')
                     16: 
                     17: types_tool = getToolByName(context, 'portal_types')
                     18: kupu_tool = getToolByName(context, 'kupu_library_tool')
                     19: url_tool = getToolByName(context, 'portal_url')
                     20: uid_catalog = getToolByName(context, 'uid_catalog')
                     21: linkbyuid = kupu_tool.getLinkbyuid()
                     22: coll_types = kupu_tool.queryPortalTypesForResourceType('collection', ())
                     23: preview_action = 'kupupreview'
                     24: portal_base = url_tool.getPortalPath()
                     25: prefix_length = len(portal_base)+1
                     26: 
                     27: # The redirecting url must be absolute otherwise it won't work for
                     28: # preview when the page is using portal_factory
                     29: # The absolute to relative conversion when the document is saved
                     30: # should strip the url right back down to resolveuid/whatever.
                     31: base = context.absolute_url()
                     32: security = AccessControl.getSecurityManager()
                     33: 
                     34: def info_object(obj, allowCollection=True):
                     35:     '''Get information from a content object'''
                     36: 
                     37:     # Parent folder might not be accessible if we came here from a
                     38:     # search.
                     39:     if not security.checkPermission('View', obj):
                     40:         return None
                     41: 
                     42:     try:
                     43:         id = obj.getId()
                     44:         portal_type = getattr(obj, 'portal_type','')
                     45:         collection = allowCollection and portal_type in coll_types
                     46: 
                     47:         if linkbyuid and not collection and hasattr(obj, 'UID'):
                     48:             url = base+'/resolveuid/%s' % obj.UID()
                     49:         else:
                     50:             url = obj.absolute_url()
                     51: 
                     52:         icon = "%s/%s" % (context.portal_url(), obj.getIcon())
                     53:         width = height = size = None
                     54:         preview = obj.getTypeInfo().getActionById(preview_action, None)
                     55: 
                     56:         try:
                     57:                 size = context.getObjSize(obj)
                     58:         except:
                     59:             size = None
                     60: 
                     61:         if showimagesize:
                     62:             width = getattr(obj, 'width', None)
                     63:             height = getattr(obj, 'height', None)
                     64:             if callable(width): width = width()
                     65:             if callable(height): height = height()
                     66: 
                     67:         title = obj.Title() or obj.getId()
                     68:         description = obj.Description()
                     69: 
                     70:         return {'id': id, 'url': url, 'portal_type': portal_type,
                     71:               'collection':  collection, 'icon': icon, 'size': size,
                     72:               'width': width, 'height': height,
                     73:               'preview': preview, 'title': title, 'description': description,
                     74:               }
                     75:     except Unauthorized:
                     76:         return None
                     77: 
                     78: def info(brain, allowCollection=True):
                     79:     '''Get information from a brain'''
                     80:     id = brain.getId
                     81: 
                     82:     url = brain.getURL()
                     83:     portal_type = brain.portal_type
                     84:     collection = portal_type in coll_types
                     85: 
                     86:     # Path for the uid catalog doesn't have the leading '/'
                     87:     path = brain.getPath()
                     88:     UID = None
                     89:     if path:
                     90:         metadata = uid_catalog.getMetadataForUID(path[prefix_length:])
                     91:         if metadata:
                     92:             UID = metadata.get('UID', None)
                     93: 
                     94:     if linkbyuid and not collection and UID:
                     95:         url = base+'/resolveuid/%s' % UID
                     96:     else:
                     97:         url = brain.getURL()
                     98: 
                     99:     icon = "%s/%s" % (context.portal_url(), brain.getIcon)
                    100:     width = height = size = None
                    101:     preview = types_tool.getTypeInfo(brain.portal_type).getActionById(preview_action, None)
                    102: 
                    103:     # It would be nice to do everything from the brain, but
                    104:     # unfortunately we need to get the object for the preview size.
                    105:     # XXX Figure out some way to get the image size client side
                    106:     # instead of inserting it here.
                    107:     if showimagesize:
                    108:         obj = brain.getObject()
                    109:         if hasattr(obj, 'get_size'):
                    110:             size = context.getObjSize(obj)
                    111:         width = getattr(obj, 'width', None)
                    112:         height = getattr(obj, 'height', None)
                    113:         if callable(width): width = width()
                    114:         if callable(height): height = height()
                    115:         
                    116:     title = brain.Title or brain.getId
                    117:     description = brain.Description
                    118: 
                    119:     return {'id': id, 'url': url, 'portal_type': portal_type,
                    120:           'collection':  collection, 'icon': icon, 'size': size,
                    121:           'width': width, 'height': height,
                    122:           'preview': preview, 'title': title, 'description': description,
                    123:           }
                    124:           
                    125: # For Plone 2.0.5 compatability, if getId is callable we assume
                    126: # we have an object rather than a brains.
                    127: if values and callable(values[0].getId):
                    128:     info = info_object
                    129: 
                    130: # return [info(brain) for brain in values]
                    131: res = []
                    132: 
                    133: portal = url_tool.getPortalObject()
                    134: if linkhere and portal is not context:
                    135:     data = info_object(context, False)
                    136:     if data:
                    137:         data['label'] = '. (%s)' % context.title_or_id()
                    138:         res.append(data)
                    139: 
                    140: if linkparent:
                    141:     if portal is not context:
                    142:         data = info_object(context.aq_parent, True)
                    143:         if data:
                    144:             data['label'] = '.. (Parent folder)'
                    145:             res.append(data)
                    146:             
                    147: for obj in values:
                    148:     data = info(obj, True)
                    149:     if data:
                    150:         res.append(data)
                    151: return res

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>