Back to index

plone3  3.1.7
editmanager.py
Go to the documentation of this file.
00001 from plone.memoize.view import memoize
00002 
00003 from plone.portlets.constants import CONTEXT_CATEGORY
00004 from plone.portlets.constants import GROUP_CATEGORY
00005 from plone.portlets.constants import CONTENT_TYPE_CATEGORY
00006 from plone.portlets.interfaces import IPortletManager
00007 from plone.portlets.interfaces import IPortletManagerRenderer
00008 from plone.portlets.interfaces import ILocalPortletAssignmentManager
00009 from plone.portlets.utils import hashPortletInfo
00010 
00011 from zope.interface import implements, Interface
00012 from zope.component import adapts, getMultiAdapter, queryMultiAdapter
00013 from zope.contentprovider.interfaces import UpdateNotCalled
00014 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
00015 
00016 from Acquisition import Explicit, aq_parent, aq_inner
00017 
00018 from Products.Five.browser import BrowserView 
00019 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00020 from Products.PythonScripts.standard import url_quote
00021 
00022 from plone.app.portlets.browser.interfaces import IManageColumnPortletsView
00023 from plone.app.portlets.browser.interfaces import IManageContextualPortletsView
00024 from plone.app.portlets.browser.interfaces import IManageDashboardPortletsView
00025 from plone.app.portlets.interfaces import IDashboard, IPortletPermissionChecker
00026 
00027 
00028 class EditPortletManagerRenderer(Explicit):
00029     """Render a portlet manager in edit mode.
00030     
00031     This is the generic renderer, which delegates to the view to determine
00032     which assignments to display.
00033     """
00034     implements(IPortletManagerRenderer)
00035     adapts(Interface, IDefaultBrowserLayer, IManageColumnPortletsView, IPortletManager)
00036 
00037     template = ViewPageTemplateFile('templates/edit-manager.pt')
00038 
00039     def __init__(self, context, request, view, manager):
00040         self.__parent__ = view
00041         self.manager = manager # part of interface
00042         self.context = context
00043         self.request = request
00044         self.__updated = False
00045         
00046     @property
00047     def visible(self):
00048         return True
00049 
00050     def filter(self, portlets):
00051         return portlets
00052 
00053     def update(self):
00054         self.__updated = True
00055 
00056     def render(self):
00057         if not self.__updated:
00058             raise UpdateNotCalled
00059         return self.template()
00060     
00061     # Used by the view template
00062 
00063     def normalized_manager_name(self):
00064         return self.manager.__name__.replace('.', '-')
00065 
00066     def baseUrl(self):
00067         return self.__parent__.getAssignmentMappingUrl(self.manager)
00068 
00069     def portlets(self):
00070         baseUrl = self.baseUrl()
00071         assignments = self._lazyLoadAssignments(self.manager)
00072         data = []
00073         
00074         manager_name = self.manager.__name__
00075         category = self.__parent__.category
00076         key = self.__parent__.key
00077         
00078         for idx in range(len(assignments)):
00079             name = assignments[idx].__name__
00080             
00081             editview = queryMultiAdapter((assignments[idx], self.request), name='edit', default=None)
00082             if editview is None:
00083                 editviewName = ''
00084             else:
00085                 editviewName = '%s/%s/edit' % (baseUrl, name)
00086             
00087             portlet_hash = hashPortletInfo(dict(manager=manager_name, category=category, 
00088                                                 key=key, name=name,))
00089             
00090             data.append( {'title'      : assignments[idx].title,
00091                           'editview'   : editviewName,
00092                           'hash'       : portlet_hash,
00093                           'up_url'     : '%s/@@move-portlet-up?name=%s' % (baseUrl, name),
00094                           'down_url'   : '%s/@@move-portlet-down?name=%s' % (baseUrl, name),
00095                           'delete_url' : '%s/@@delete-portlet?name=%s' % (baseUrl, name),
00096                           })
00097         if len(data) > 0:
00098             data[0]['up_url'] = data[-1]['down_url'] = None
00099         return data
00100         
00101     def addable_portlets(self):
00102         baseUrl = self.baseUrl()
00103         addviewbase = baseUrl.replace(self.context_url(), '')
00104         def sort_key(v):
00105             return v.get('title')
00106         portlets =  [{
00107             'title' : p.title,
00108             'description' : p.description,
00109             'addview' : '%s/+/%s' % (addviewbase, p.addview)
00110             } for p in self.manager.getAddablePortletTypes()]
00111         portlets.sort(key=sort_key)
00112         return portlets
00113 
00114     @memoize
00115     def referer(self):
00116         view_name = self.request.get('viewname', None)
00117         key = self.request.get('key', None)
00118         base_url = self.request['ACTUAL_URL']
00119         
00120         if view_name:
00121             base_url = self.context_url() + '/' + view_name
00122         
00123         if key:
00124             base_url += '?key=%s' % key
00125         
00126         return base_url
00127 
00128     @memoize
00129     def url_quote_referer(self):
00130         return url_quote(self.referer())
00131     
00132     # See note in plone.portlets.manager
00133     
00134     @memoize    
00135     def _lazyLoadAssignments(self, manager):
00136         return self.__parent__.getAssignmentsForManager(manager)
00137     
00138     @memoize
00139     def context_url(self):
00140         return str(getMultiAdapter((self.context, self.request), name='absolute_url'))
00141           
00142 class ContextualEditPortletManagerRenderer(EditPortletManagerRenderer):
00143     """Render a portlet manager in edit mode for contextual portlets
00144     """
00145     adapts(Interface, IDefaultBrowserLayer, IManageContextualPortletsView, IPortletManager)
00146 
00147     template = ViewPageTemplateFile('templates/edit-manager-contextual.pt')
00148 
00149     def __init__(self, context, request, view, manager):
00150         EditPortletManagerRenderer.__init__(self, context, request, view, manager)
00151         
00152     def blacklist_status_action(self):
00153         baseUrl = str(getMultiAdapter((self.context, self.request), name='absolute_url'))
00154         return baseUrl + '/@@set-portlet-blacklist-status'
00155     
00156     def manager_name(self):
00157         return self.manager.__name__
00158     
00159     def context_blacklist_status(self):
00160         assignable = getMultiAdapter((self.context, self.manager,), ILocalPortletAssignmentManager)
00161         return assignable.getBlacklistStatus(CONTEXT_CATEGORY)
00162 
00163     def group_blacklist_status(self):
00164         assignable = getMultiAdapter((self.context, self.manager,), ILocalPortletAssignmentManager)
00165         return assignable.getBlacklistStatus(GROUP_CATEGORY)
00166     
00167     def content_type_blacklist_status(self):
00168         assignable = getMultiAdapter((self.context, self.manager,), ILocalPortletAssignmentManager)
00169         return assignable.getBlacklistStatus(CONTENT_TYPE_CATEGORY)
00170   
00171 class DashboardEditPortletManagerRenderer(EditPortletManagerRenderer):
00172     """Render a portlet manager in edit mode for the dashboard
00173     """
00174     adapts(Interface, IDefaultBrowserLayer, IManageDashboardPortletsView, IDashboard)
00175         
00176 class ManagePortletAssignments(BrowserView):
00177     """Utility views for managing portlets for a particular column
00178     """
00179     
00180     # view @@move-portlet-up
00181     def move_portlet_up(self, name):
00182         assignments = aq_inner(self.context)
00183         IPortletPermissionChecker(assignments)()
00184         
00185         keys = list(assignments.keys())
00186         
00187         idx = keys.index(name)
00188         keys.remove(name)
00189         keys.insert(idx-1, name)
00190         assignments.updateOrder(keys)
00191         
00192         self.request.response.redirect(self._nextUrl())
00193         return ''
00194     
00195     # view @@move-portlet-down
00196     def move_portlet_down(self, name):
00197         assignments = aq_inner(self.context)
00198         IPortletPermissionChecker(assignments)()
00199         
00200         keys = list(assignments.keys())
00201         
00202         idx = keys.index(name)
00203         keys.remove(name)
00204         keys.insert(idx+1, name)
00205         assignments.updateOrder(keys)
00206         
00207         self.request.response.redirect(self._nextUrl())
00208         return ''
00209     
00210     # view @@delete-portlet
00211     def delete_portlet(self, name):
00212         assignments = aq_inner(self.context)
00213         IPortletPermissionChecker(assignments)()
00214         del assignments[name]
00215         self.request.response.redirect(self._nextUrl())
00216         return ''
00217         
00218     def _nextUrl(self):
00219         referer = self.request.get('referer')
00220         if not referer:
00221             context = aq_parent(aq_inner(self.context))
00222             url = str(getMultiAdapter((context, self.request), name=u"absolute_url"))    
00223             referer = '%s/@@manage-portlets' % (url,)
00224         return referer