Assigning a Portlet With Python

All tutorials I found online deal with using either GenericSetup for portlets assignment, or explain only how to delete a portlet from Python. Below, I want to explain how to assign a portlet from Python, as is required when you want to have portlets that depend on dynamic structures.

Get the assignment_manager:

>>> manager = getUtility(IPortletManager, name=u"plone.leftcolumn", context=site)
>>> assignment_mapping = getMultiAdapter((context, manager), IPortletAssignmentMapping)

Get a portlet's assignment class, eg. that one from Events, and instantiate it:

>>> from plone.app.portlets.portlets import events
>>> myevents = events.Assignment()

Then assign the new portlet:

>>> assignment_mapping[u"my_events"] = myevents

If you're doing it manually, like me, then you also need to commit the change:

>>> assignment_mapping._p_changed = True
>>> transaction.commit()

Comments