Back to index

plone3  3.1.7
manage.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.component import getMultiAdapter, getUtility
00003 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
00004 
00005 from AccessControl import Unauthorized
00006 from Acquisition import aq_inner
00007 from Acquisition import aq_base
00008 from Products.Five import BrowserView
00009 
00010 from Products.CMFCore.utils import getToolByName
00011 
00012 from plone.portlets.interfaces import IPortletManager
00013 from plone.portlets.interfaces import IPortletAssignmentMapping
00014 from plone.portlets.interfaces import ILocalPortletAssignmentManager
00015 
00016 from plone.portlets.constants import USER_CATEGORY
00017 from plone.portlets.constants import GROUP_CATEGORY
00018 from plone.portlets.constants import CONTENT_TYPE_CATEGORY
00019 from plone.portlets.constants import CONTEXT_CATEGORY
00020 
00021 from plone.app.portlets.storage import PortletAssignmentMapping
00022 from plone.app.portlets.storage import UserPortletAssignmentMapping
00023 
00024 from plone.app.portlets.interfaces import IPortletPermissionChecker
00025 
00026 from plone.app.portlets.browser.interfaces import IManagePortletsView
00027 from plone.app.portlets.browser.interfaces import IManageContextualPortletsView
00028 from plone.app.portlets.browser.interfaces import IManageDashboardPortletsView
00029 from plone.app.portlets.browser.interfaces import IManageGroupPortletsView
00030 from plone.app.portlets.browser.interfaces import IManageContentTypePortletsView
00031 
00032 from plone.app.portlets import utils
00033 from plone.memoize.view import memoize
00034 
00035 class ManageContextualPortlets(BrowserView):
00036     implements(IManageContextualPortletsView)
00037     
00038     def __init__(self, context, request):
00039         super(ManageContextualPortlets, self).__init__(context, request)
00040         self.request.set('disable_border', True)
00041         
00042     # IManagePortletsView implementation
00043     
00044     @property
00045     def category(self):
00046         return CONTEXT_CATEGORY
00047         
00048     @property
00049     def key(self):
00050         return '/'.join(self.context.getPhysicalPath())
00051     
00052     def getAssignmentMappingUrl(self, manager):
00053         baseUrl = str(getMultiAdapter((self.context, self.request), name='absolute_url'))
00054         return '%s/++contextportlets++%s' % (baseUrl, manager.__name__)
00055     
00056     def getAssignmentsForManager(self, manager):
00057         assignments = getMultiAdapter((self.context, manager), IPortletAssignmentMapping)
00058         return assignments.values()
00059     
00060     # view @@manage-portlets
00061     
00062     def has_legacy_portlets(self):
00063         left_slots = getattr(aq_base(self.context), 'left_slots', [])
00064         right_slots = getattr(aq_base(self.context), 'right_slots', [])
00065         
00066         return (left_slots or right_slots)
00067 
00068     # view @@set-portlet-blacklist-status
00069     def set_blacklist_status(self, manager, group_status, content_type_status, context_status):
00070         portletManager = getUtility(IPortletManager, name=manager)
00071         assignable = getMultiAdapter((self.context, portletManager,), ILocalPortletAssignmentManager)
00072         assignments = getMultiAdapter((self.context, portletManager), IPortletAssignmentMapping)
00073         
00074         IPortletPermissionChecker(assignments.__of__(aq_inner(self.context)))()
00075         
00076         def int2status(status):
00077             if status == 0:
00078                 return None
00079             elif status > 0:
00080                 return True
00081             else:
00082                 return False
00083         
00084         assignable.setBlacklistStatus(GROUP_CATEGORY, int2status(group_status))
00085         assignable.setBlacklistStatus(CONTENT_TYPE_CATEGORY, int2status(content_type_status))
00086         assignable.setBlacklistStatus(CONTEXT_CATEGORY, int2status(context_status))
00087         
00088         baseUrl = str(getMultiAdapter((self.context, self.request), name='absolute_url'))
00089         self.request.response.redirect(baseUrl + '/@@manage-portlets')
00090         return ''
00091     
00092     # view @@convert-legacy-portlets
00093     
00094     def convert_legacy_portlets(self):
00095         utils.convert_legacy_portlets(self.context)
00096         self.context.request.response.redirect(self.context.absolute_url() + '/@@manage-portlets')
00097 
00098 class ManageDashboardPortlets(BrowserView):
00099     implements(IManageDashboardPortletsView)
00100         
00101     # IManagePortletsView implementation
00102     
00103     @property
00104     def category(self):
00105         return USER_CATEGORY
00106         
00107     @property
00108     def key(self):
00109         return self._getUserId()
00110     
00111     def getAssignmentMappingUrl(self, manager):
00112         baseUrl = str(getMultiAdapter((self.context, self.request), name='absolute_url'))
00113         userId = self._getUserId()
00114         return '%s/++dashboard++%s+%s' % (baseUrl, manager.__name__, userId)
00115 
00116     def getAssignmentsForManager(self, manager):
00117         userId = self._getUserId()
00118         column = getUtility(IPortletManager, name=manager.__name__)
00119         category = column[USER_CATEGORY]
00120         mapping = category.get(userId, None)
00121         if mapping is None:
00122             mapping = category[userId] = UserPortletAssignmentMapping(manager=manager.__name__,
00123                                                                       category=USER_CATEGORY,
00124                                                                       name=userId)
00125         return mapping.values()
00126     
00127     def _getUserId(self):
00128         membership = getToolByName(aq_inner(self.context), 'portal_membership', None)
00129         if membership.isAnonymousUser():
00130             raise Unauthorized, "Cannot get portlet assignments for anonymous through this view"
00131         
00132         member = membership.getAuthenticatedMember()
00133         memberId = member.getId()
00134         
00135         if memberId is None:
00136             raise KeyError, "Cannot find user id of current user" 
00137         
00138         return memberId
00139 
00140 class ManageGroupPortlets(BrowserView):
00141     implements(IManageGroupPortletsView)
00142         
00143     # IManagePortletsView implementation
00144     
00145     @property
00146     def category(self):
00147         return GROUP_CATEGORY
00148         
00149     @property
00150     def key(self):
00151         return self.request['key']
00152     
00153     def __init__(self, context, request):
00154         super(ManageGroupPortlets, self).__init__(context, request)
00155         self.request.set('disable_border', True)
00156     
00157     def getAssignmentMappingUrl(self, manager):
00158         baseUrl = str(getMultiAdapter((self.context, self.request), name='absolute_url'))
00159         key = self.request['key']
00160         return '%s/++groupportlets++%s+%s' % (baseUrl, manager.__name__, key)
00161 
00162     def getAssignmentsForManager(self, manager):
00163         key = self.request['key']
00164         column = getUtility(IPortletManager, name=manager.__name__)
00165         category = column[GROUP_CATEGORY]
00166         mapping = category.get(key, None)
00167         if mapping is None:
00168             mapping = category[key] = PortletAssignmentMapping(manager=manager.__name__,
00169                                                                category=GROUP_CATEGORY,
00170                                                                name=key)
00171         return mapping.values()
00172     
00173     # View attributes
00174     
00175     def group(self):
00176         return self.request['key']
00177 
00178 class ManageContentTypePortlets(BrowserView):
00179     implements(IManageContentTypePortletsView)
00180     
00181     def __init__(self, context, request):
00182         super(ManageContentTypePortlets, self).__init__(context, request)
00183         self.request.set('disable_border', True)
00184         
00185     # IManagePortletsView implementation
00186     
00187     @property
00188     def category(self):
00189         return CONTENT_TYPE_CATEGORY
00190         
00191     @property
00192     def key(self):
00193         return self.request['key']
00194     
00195     def getAssignmentMappingUrl(self, manager):
00196         baseUrl = str(getMultiAdapter((self.context, self.request), name='absolute_url'))
00197         pt = self.request['key']
00198         return '%s/++contenttypeportlets++%s+%s' % (baseUrl, manager.__name__, pt)
00199 
00200     def getAssignmentsForManager(self, manager):
00201         pt = self.request['key']
00202         column = getUtility(IPortletManager, name=manager.__name__)
00203         category = column[CONTENT_TYPE_CATEGORY]
00204         mapping = category.get(pt, None)
00205         if mapping is None:
00206             mapping = category[pt] = PortletAssignmentMapping(manager=manager.__name__,
00207                                                               category=CONTENT_TYPE_CATEGORY,
00208                                                               name=pt)
00209         return mapping.values()
00210     
00211     # View attributes
00212     
00213     def portal_type(self):
00214         return self.fti().Title()
00215                 
00216     def portal_type_icon(self):
00217         ploneview = getMultiAdapter((self.context, self.request), name=u"plone")
00218         return ploneview.getIcon(self.fti())
00219            
00220     @memoize
00221     def fti(self):
00222         portal_types = getToolByName(aq_inner(self.context), 'portal_types')
00223         portal_type = self.request['key']
00224         for fti in portal_types.listTypeInfo():
00225             if fti.getId() == portal_type:
00226                 return fti
00227                 
00228 class ManagePortletsViewlet(BrowserView):
00229     """A general base class for viewlets that want to be rendered on the
00230     manage portlets view. This makes it possible to have a viewlet that 
00231     renders a portlet manager, and still have the generic edit manager
00232     renderer work (it doesn't work otherwise, because the edit manager
00233     renderer is registered on IManagePortletsView, but inside a viewlet,
00234     the __parent__ is the viewlet, not the ultimate parent).
00235     """
00236     implements(IManagePortletsView)
00237     
00238     def __init__(self, context, request, view, manager):
00239         super(ManagePortletsViewlet, self).__init__(context, request)
00240         self.__parent__ = view
00241         self.context = context
00242         self.request = request
00243         self.view = view
00244         self.manager = manager
00245     
00246     @property
00247     def category(self):
00248         return self.ultimate_parent().category
00249     
00250     @property
00251     def key(self):
00252         return self.ultimate_parent().key
00253     
00254     def getAssignmentMappingUrl(self, manager):
00255         return self.ultimate_parent().getAssignmentMappingUrl(manager)
00256         
00257     def getAssignmentsForManager(self, manager):
00258         return self.ultimate_parent().getAssignmentsForManager(manager)
00259         
00260     @memoize
00261     def ultimate_parent(self):
00262         parent = self.__parent__
00263         while parent is not None and hasattr(parent, '__parent__') and parent.__parent__ is not None:
00264             parent = parent.__parent__
00265         return parent
00266         
00267     # Subclasses need to implement update() and render() - or
00268     # we can use ZCML with the template attribute (hence we don't
00269     # put these here)
00270     
00271 class ManageContextualPortletsViewlet(ManagePortletsViewlet):
00272     """A viewlet base class for viewlets that need to render on the
00273     manage contextual portlets screen.
00274     """
00275     implements(IManageContextualPortletsView)
00276         
00277 class ManageGroupPortletsViewlet(ManagePortletsViewlet):
00278     """A viewlet base class for viewlets that need to render on the
00279     manage group portlets screen.
00280     """
00281     implements(IManageGroupPortletsView)
00282     
00283 class ManageContentTypePortletsViewlet(ManagePortletsViewlet):
00284     """A viewlet base class for viewlets that need to render on the
00285     manage content type portlets screen.
00286     """
00287     implements(IManageContentTypePortletsView)