Remove the default set of portlets in user dashboard

In Plone 5. The user dashboard is initialized with two user portlets, a portlets.News and a portlets.Recent.
How to change this behavior ?

In my use case, I never use user portlet. The dashboard contains only group portlets.

Hope that helps.

from zope.component import getUtility
from plone.portlets.interfaces import IPortletManager

from plone.portlets.constants import GROUP_CATEGORY
from plone.app.portlets.storage import GroupDashboardPortletAssignmentMapping

from plone.portlets.constants import USER_CATEGORY
from plone.app.portlets.storage import UserPortletAssignmentMapping



list_dashboards = ('plone.dashboard1', 'plone.dashboard2','plone.dashboard3', 'plone.dashboard4')
group_id = group.id
user_id = member.id

for name in list_dashboards:

    column = getUtility(IPortletManager, name=name)
    
    if column is not None:
    
        # Begin Delete Group Portlets on the Dashboard
    
        category = column.get(GROUP_CATEGORY, None)
    
        if category is not None:
            
            manager = category.get(group_id, None)
            
            if manager is None:
                manager = GroupDashboardPortletAssignmentMapping(
                    manager=name,
                    category=GROUP_CATEGORY,
                    name=group_id)
            
            for portlet in manager.keys():
                del manager[portlet]
                
        # End Delete Group Portlets on the Dashboard
            
        # Begin Delete User Portlets on the Dashboard
        category = column.get(USER_CATEGORY, None)
        
        if category is not None:
            
            manager = category.get(user_id, None)
            
            if manager is None:
                manager = UserPortletAssignmentMapping(
                    manager=name,
                    category=USER_CATEGORY,
                    name=user_id)
                    
            for portlet in manager.keys():
                del manager[portlet]
        # End Delete User Portlets on the Dashboard                

Full Example, it works in my Project

<!-- subscriber.zcml -->

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:five="http://namespaces.zope.org/five"
    xmlns:i18n="http://namespaces.zope.org/i18n"
    xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
    i18n_domain="my.addon">
    
    <!-- Register Event Handlers -->
    
    <!-- Handler for `User Initial Login Event`. Remove Portlets  -->
    <subscriber
        for="Products.PlonePAS.interfaces.events.IUserInitialLoginInEvent"
        handler="my.addon.subscriber.onUserInitialLoginEvent"  />
        
</configure>

# -*- coding: utf-8 -*-
# subscriber.py

from my.addon.utils import removeDashboardPortlets

def onUserInitialLoginEvent(event):
    """
        Subscriber for `User Initial Login Event`.
        reset the Portlets on the User Dashboard    
    """
    removeDashboardPortlets()
# -*- coding: utf-8 -*-
# utils.py
import logging
from zope.component import getUtility
from plone import api
from plone.app.portlets.storage import UserPortletAssignmentMapping
from plone.portlets.interfaces import IPortletManager
from plone.portlets.constants import USER_CATEGORY

logger = logging.getLogger('my.addon')

def removeDashboardPortlets():
    
    logger.info('Updating Dashboard Portlet - Remove Group/User Portlets')    
    
    current = api.user.get_current()
        
    list_dashboards = ('plone.dashboard1', 'plone.dashboard2','plone.dashboard3', 'plone.dashboard4')
        
    for name in list_dashboards:
        
        column = getUtility(IPortletManager, name=name)
            
        if column is not None:
            
            category = column.get(USER_CATEGORY, None)
            
            if category is not None:
                
                manager = category.get(current.id, None)
                
            if manager is None:
                    
                manager = UserPortletAssignmentMapping(
                    manager=name,
                    category=USER_CATEGORY,
                    name=current.id)
                    
                category[current.id] = manager
                        
            for portlet in manager.keys():
                del manager[portlet]

Thank you @1letter

It is possible to override the set of default dashboard portlets (at least in plone 5.1) by overriding the DefaultDashboard adapter from plone.app.portlets/plone/app/portlets/dashboard.py

example:

@implementer(IDefaultDashboard)
class DefaultDashboard(object):
    """The default default dashboard.
    """
    adapts(IPropertiedUser)

    def __init__(self, principal):
        self.principal = principal

    def __call__(self):
        return {
            'plone.dashboard1': (favorites.Assignment(), ),
            'plone.dashboard2': (),
            #'plone.dashboard2': (portlets.recent.Assignment(), ),
            'plone.dashboard3': (),
            'plone.dashboard4': (),
        }

and in overrides.zcml:

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:plone="http://namespaces.plone.org/plone"
    xmlns:i18n="http://namespaces.zope.org/i18n"
    xmlns:zcml="http://namespaces.zope.org/zcml"
    i18n_domain="ufm.intranet">

    <adapter factory=".dashboard.DefaultDashboard" />

</configure>
1 Like