Back to index

plone3  3.1.7
foldercontents.py
Go to the documentation of this file.
00001 from zope.component import getMultiAdapter
00002 from zope.interface import implements
00003 
00004 from AccessControl import Unauthorized
00005 from Acquisition import aq_parent, aq_inner
00006 from OFS.interfaces import IOrderedContainer
00007 from Products.ATContentTypes.interface import IATTopic
00008 from Products.CMFCore.utils import getToolByName
00009 from Products.Five import BrowserView
00010 
00011 from plone.memoize import instance
00012 from plone.app.content.browser.interfaces import IFolderContentsView
00013 from plone.app.content.browser.tableview import Table, TableKSSView
00014 from kss.core import KSSView
00015 
00016 from Products.CMFPlone.interfaces import IPloneSiteRoot
00017 
00018 import urllib
00019 
00020 NOT_ADDABLE_TYPES = ('Favorite',)
00021 
00022 class FolderContentsView(BrowserView):
00023     """
00024     """
00025     implements(IFolderContentsView)
00026     
00027     def contents_table(self):
00028         table = FolderContentsTable(self.context, self.request)
00029         return table.render()
00030 
00031     def title(self):
00032         """
00033         """
00034         return self.context.pretty_title_or_id()
00035 
00036     def icon(self):
00037         """
00038         """
00039         ploneview = getMultiAdapter((self.context, self.request), name="plone")
00040         icon = ploneview.getIcon(self.context)
00041         return icon.html_tag()
00042 
00043     def parent_url(self):
00044         """
00045         """
00046         portal_url = getToolByName(self.context, 'portal_url')
00047         plone_utils = getToolByName(self.context, 'plone_utils')
00048         portal_membership = getToolByName(self.context, 'portal_membership')
00049 
00050         obj = self.context
00051 
00052         checkPermission = portal_membership.checkPermission
00053 
00054         # Abort if we are at the root of the portal
00055         if IPloneSiteRoot.providedBy(self.context):
00056             return None
00057         
00058 
00059         # Get the parent. If we can't get it (unauthorized), use the portal
00060         parent = aq_parent(aq_inner(obj))
00061         
00062         # # We may get an unauthorized exception if we're not allowed to access#
00063         # the parent. In this case, return None
00064         try:
00065             if getattr(parent, 'getId', None) is None or \
00066                    parent.getId() == 'talkback':
00067                 # Skip any Z3 views that may be in the acq tree;
00068                 # Skip past the talkback container if that's where we are
00069                 parent = aq_parent(aq_inner(parent))
00070 
00071             if not checkPermission('List folder contents', parent):
00072                 return None
00073     
00074             return parent.absolute_url()
00075 
00076         except Unauthorized:
00077             return None        
00078 
00079 class FolderContentsTable(object):
00080     """   
00081     The foldercontents table renders the table and its actions.
00082     """                
00083 
00084     def __init__(self, context, request, contentFilter={}):
00085         self.context = context
00086         self.request = request
00087         self.contentFilter = contentFilter
00088 
00089         url = self.context.absolute_url()
00090         view_url = url + '/@@folder_contents'
00091         self.table = Table(request, url, view_url, self.items,
00092                            show_sort_column=self.show_sort_column,
00093                            buttons=self.buttons)
00094 
00095     def render(self):
00096         return self.table.render()
00097 
00098     @property
00099     @instance.memoize
00100     def items(self):
00101         """
00102         """
00103         plone_utils = getToolByName(self.context, 'plone_utils')
00104         plone_view = getMultiAdapter((self.context, self.request), name=u'plone')
00105         portal_workflow = getToolByName(self.context, 'portal_workflow')
00106         portal_properties = getToolByName(self.context, 'portal_properties')
00107         site_properties = portal_properties.site_properties
00108         
00109         use_view_action = site_properties.getProperty('typesUseViewActionInListings', ())
00110         browser_default = self.context.browserDefault()
00111                 
00112         if IATTopic.providedBy(self.context):
00113             contentsMethod = self.context.queryCatalog
00114         else:
00115             contentsMethod = self.context.getFolderContents
00116         
00117         results = []
00118         for i, obj in enumerate(contentsMethod(self.contentFilter)):
00119             if (i + 1) % 2 == 0:
00120                 table_row_class = "draggable even"
00121             else:
00122                 table_row_class = "draggable odd"
00123 
00124             url = obj.getURL()
00125             path = obj.getPath or "/".join(obj.getPhysicalPath())
00126             icon = plone_view.getIcon(obj);
00127             
00128             type_class = 'contenttype-' + plone_utils.normalizeString(
00129                 obj.portal_type)
00130 
00131             review_state = obj.review_state
00132             state_class = 'state-' + plone_utils.normalizeString(review_state)
00133             relative_url = obj.getURL(relative=True)
00134             obj_type = obj.portal_type
00135 
00136             modified = plone_view.toLocalizedTime(
00137                 obj.ModificationDate, long_format=1)
00138             
00139             if obj_type in use_view_action:
00140                 view_url = url + '/view'
00141             elif obj.is_folderish:
00142                 view_url = url + "/folder_contents"              
00143             else:
00144                 view_url = url
00145 
00146             is_browser_default = len(browser_default[1]) == 1 and (
00147                 obj.id == browser_default[1][0])
00148                                  
00149             results.append(dict(
00150                 url = url,
00151                 id  = obj.getId,
00152                 quoted_id = urllib.quote_plus(obj.getId),
00153                 path = path,
00154                 title_or_id = obj.pretty_title_or_id(),
00155                 description = obj.Description,
00156                 obj_type = obj_type,
00157                 size = obj.getObjSize,
00158                 modified = modified,
00159                 icon = icon.html_tag(),
00160                 type_class = type_class,
00161                 wf_state = review_state,
00162                 state_title = portal_workflow.getTitleForStateOnType(review_state,
00163                                                            obj_type),
00164                 state_class = state_class,
00165                 is_browser_default = is_browser_default,
00166                 folderish = obj.is_folderish,
00167                 relative_url = relative_url,
00168                 view_url = view_url,
00169                 table_row_class = table_row_class,
00170                 is_expired = self.context.isExpired(obj),
00171             ))
00172         return results
00173 
00174     @property
00175     def orderable(self):
00176         """
00177         """        
00178         return IOrderedContainer.providedBy(self.context)
00179 
00180     @property
00181     def show_sort_column(self):
00182         return self.orderable and self.editable
00183         
00184     @property
00185     def editable(self):
00186         """
00187         """
00188         context_state = getMultiAdapter((self.context, self.request),
00189                                         name=u'plone_context_state')
00190         return context_state.is_editable()
00191 
00192     @property
00193     def buttons(self):
00194         buttons = []
00195         portal_actions = getToolByName(self.context, 'portal_actions')
00196         button_actions = portal_actions.listActionInfos(object=aq_inner(self.context), categories=('folder_buttons', ))
00197 
00198         # Do not show buttons if there is no data, unless there is data to be
00199         # pasted
00200         if not len(self.items):
00201             if self.context.cb_dataValid():
00202                 for button in button_actions:
00203                     if button['id'] == 'paste':
00204                         return [self.setbuttonclass(button)]
00205             else:
00206                 return []
00207 
00208         for button in button_actions:
00209             # Make proper classes for our buttons
00210             if button['id'] != 'paste' or self.context.cb_dataValid():
00211                 buttons.append(self.setbuttonclass(button))
00212         return buttons
00213 
00214     def setbuttonclass(self, button):
00215         if button['id'] == 'paste':
00216             button['cssclass'] = 'standalone'
00217         else:
00218             button['cssclass'] = 'context'
00219         return button
00220 
00221 class FolderContentsKSSView(TableKSSView):
00222     table = FolderContentsTable
00223