Remove the default portlet assignments using GenericSetup

Here is an upgrade step from one of our projects for removing a specific portlet. Removing the condition on the id and just doing del mapping[id] should remove portlets set anywhere other than the site root.

def remove_mostviewed_portlet_assignments(context):
    """ We are no longer using these and the code has been removed
    """
    portal = getSite()
    all_content = portal.portal_catalog(
        show_inactive=True,
        language="ALL",
        object_provides=ILocalPortletAssignable.__identifier__
    )
    # Load the real object instead of index stub
    all_content = [content.getObject() for content in all_content]
    # portal itself does not show up in the query above,
    # though it might contain portlet assignments
    all_content = list(all_content) + [portal]
    for content in all_content:
        for manager_name in ["plone.leftcolumn", "plone.rightcolumn"]:
            manager = getUtility(
                IPortletManager, name=manager_name, context=content)
            mapping = getMultiAdapter(
                (content, manager), IPortletAssignmentMapping)
            # id is portlet assignment id
            # and automatically generated
            for id, assignment in mapping.items():
                if id.startswith('most-viewed'):
                    # set fixing_up to True to let zope.container.contained
                    # know that our object doesn't have __name__ and __parent__
                    fixing_up = contained.fixing_up
                    contained.fixing_up = True
                    del mapping[id]
                    contained.fixing_up = fixing_up
                    logger.info(
                        "Removed `most-viewed` assignment from: {0}".format(
                            '/'.join(content.getPhysicalPath()))
                    )
3 Likes