Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 from zope.component import getUtility, getMultiAdapter
00002 from zope.app.container.interfaces import INameChooser
00003 
00004 from plone.portlets.interfaces import IPortletManager
00005 from plone.portlets.interfaces import IPortletAssignmentMapping
00006 
00007 from plone.portlets.constants import CONTEXT_CATEGORY, USER_CATEGORY
00008 
00009 from plone.app.portlets.interfaces import IPortletPermissionChecker
00010 
00011 from plone.app.portlets.portlets import calendar
00012 from plone.app.portlets.portlets import classic
00013 from plone.app.portlets.portlets import login
00014 from plone.app.portlets.portlets import news
00015 from plone.app.portlets.portlets import events
00016 from plone.app.portlets.portlets import navigation
00017 from plone.app.portlets.portlets import recent
00018 from plone.app.portlets.portlets import review
00019 
00020 from plone.app.portlets.storage import PortletAssignmentMapping
00021 from plone.app.portlets.storage import UserPortletAssignmentMapping
00022 
00023 from Acquisition import aq_base
00024 from Products.CMFCore.utils import getToolByName
00025 
00026 def assignment_mapping_from_key(context, manager_name, category, key, create=False):
00027     """Given the name of a portlet manager, the name of a category, and a
00028     key in that category, return the IPortletAssignmentMapping. 
00029     Raise a KeyError if it cannot be found.
00030     """
00031     
00032     manager = getUtility(IPortletManager, manager_name)
00033     
00034     if category == CONTEXT_CATEGORY:
00035         path = key
00036         portal = getToolByName(context, 'portal_url').getPortalObject()
00037         portal_path = '/'.join(portal.getPhysicalPath())
00038         path = path[len(portal_path)+1:]
00039         obj = portal.restrictedTraverse(path, None)
00040         if obj is None:
00041             raise KeyError, "Cannot find object at path %s" % path
00042         return getMultiAdapter((obj, manager), IPortletAssignmentMapping)
00043     else:
00044         mapping = manager[category]
00045         if key not in mapping and create:
00046             if category == USER_CATEGORY:
00047                 mapping[key] = UserPortletAssignmentMapping()
00048             else:
00049                 mapping[key] = PortletAssignmentMapping()
00050         return mapping[key]
00051 
00052 def assignment_from_key(context, manager_name, category, key, name):
00053     """Given the name of a portlet manager, the name of a category, a
00054     key in that category and the name of a particular assignment, return
00055     the IPortletAssignment. Raise a KeyError if it cannot be found.
00056     """
00057     return assignment_mapping_from_key(context, manager_name, category, key)[name]
00058 
00059 DONT_MIGRATE = object()
00060 
00061 portletsMapping = { 'portlet_login'      : login.Assignment(),
00062                     'portlet_news'       : news.Assignment(count=5),
00063                     'portlet_events'     : events.Assignment(count=5),
00064                     'portlet_navigation' : navigation.Assignment(),
00065                     'portlet_calendar'   : calendar.Assignment(),
00066                     'portlet_review'     : review.Assignment(),
00067                     'portlet_recent'     : recent.Assignment(count=5),
00068                     'portlet_related'    : DONT_MIGRATE,
00069                     'portlet_languages'  : DONT_MIGRATE,
00070                   }
00071                   
00072 def convert_legacy_portlets(context):
00073     """Convert legacy portlets (left_slots, right_slots) in the given
00074     context to new-style portlets.
00075     """
00076         
00077     # Convert left_slots and right_slots to portlets
00078     
00079     left = getUtility(IPortletManager, name='plone.leftcolumn')
00080     right = getUtility(IPortletManager, name='plone.rightcolumn')
00081     
00082     leftAssignable = getMultiAdapter((context, left), IPortletAssignmentMapping).__of__(context)
00083     rightAssignable = getMultiAdapter((context, right), IPortletAssignmentMapping).__of__(context)
00084     
00085     IPortletPermissionChecker(leftAssignable)()
00086     IPortletPermissionChecker(rightAssignable)()
00087     
00088     leftChooser = INameChooser(leftAssignable)
00089     rightChooser = INameChooser(rightAssignable)
00090     
00091     left_slots = getattr(aq_base(context), 'left_slots', [])
00092     right_slots = getattr(aq_base(context), 'right_slots', [])
00093             
00094     for item in left_slots:
00095         path = item.split('/')
00096         if len(path) == 4:
00097             newPortlet = portletsMapping.get(path[1], None)
00098             if newPortlet is None and path[0] in ('context', 'here',) and path[2] == 'macros':
00099                 newPortlet = classic.Assignment(path[1], path[3])
00100             if newPortlet is not None and newPortlet is not DONT_MIGRATE:
00101                 leftAssignable[leftChooser.chooseName(None, newPortlet)] = newPortlet
00102                 
00103     for item in right_slots:
00104         path = item.split('/')
00105         if len(path) == 4:
00106             newPortlet = portletsMapping.get(path[1], None)
00107             if newPortlet is None and path[0] in ('context', 'here',) and path[2] == 'macros':
00108                 newPortlet = classic.Assignment(path[1], path[3])
00109             if newPortlet is not None and newPortlet is not DONT_MIGRATE:
00110                 rightAssignable[rightChooser.chooseName(None, newPortlet)] = newPortlet
00111 
00112     context.left_slots = []
00113     context.right_slots = []