Skip to content

Instantly share code, notes, and snippets.

@cdw9
Created September 19, 2022 19:53
Show Gist options
  • Save cdw9/a221da284f3dcb7eb295f210b1a3b395 to your computer and use it in GitHub Desktop.
Save cdw9/a221da284f3dcb7eb295f210b1a3b395 to your computer and use it in GitHub Desktop.
Get a listing (type, path) of where all the portlets are assigned within a Plone site
# bin/instance1 -O plonesiteid run scripts/portlet_assignments.py
import logging
import sys
from AccessControl.SecurityManagement import newSecurityManager
from plone import api
from plone.portlets.interfaces import IPortletAssignmentMapping, IPortletManager
from zope.component import queryMultiAdapter, queryUtility
logger = logging.getLogger("[script]")
class ScriptRunner(object):
"""Find all the portlets in the site
"""
def __init__(self, portal, app=None):
self.portal = portal
self.app = app
def su(self, user_id='admin'):
user = self.portal.acl_users.getUserById(user_id)
if user is None and self.app is not None:
user = self.app.acl_users.getUserById(user_id)
if user is None:
print("User '%s' could not be found".format(user_id))
sys.exit(1)
newSecurityManager(None, user)
def run(self):
all_content = api.content.find(context=api.portal.get())
for item in all_content:
path = item.getPath()
obj = item.getObject()
for manager_name in ['plone.leftcolumn', 'plone.rightcolumn']:
manager = queryUtility(IPortletManager, name=manager_name, context=obj)
if not manager:
continue
mappings = queryMultiAdapter((obj, manager), IPortletAssignmentMapping)
if not mappings:
continue
for key, assignment in mappings.items():
print(f'{assignment.__class__.__module__} @ {path}')
def main(app):
try:
portal = api.portal.get()
except api.exc.CannotGetPortalError:
print("ERROR plone.api.exc.CannotGetPortalError")
print("Plone site ID needs to be passed:")
print("$ bin/instance -O [plonesiteid] run scripts/portlet_assignments.py")
sys.exit()
runner = ScriptRunner(portal, app)
runner.su()
runner.run()
if __name__ == '__main__':
main(app) # NOQA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment