Created
February 28, 2013 13:12
-
-
Save jone/5056618 to your computer and use it in GitHub Desktop.
Plone: Setup dashboard on login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Products.Archetypes.interfaces.field import IImageField | |
from plone.app.blob.interfaces import IBlobImageField | |
from ftw.dashboard.portlets.postit.browser import postit | |
from ftw.dashboard.portlets.recentlymodified.browser import recentlymodified | |
from ftw.favorite.portlets import favorites | |
from plone.portlets.interfaces import IPortletManager | |
from zope.component import queryUtility | |
from zope.app.container.interfaces import INameChooser | |
from plone.portlets.constants import USER_CATEGORY | |
from plone.app.portlets.storage import UserPortletAssignmentMapping | |
from Products.Archetypes.interfaces import IObjectPostValidation | |
from zope.interface import implements | |
from zope.component import adapts | |
from Products.Archetypes.interfaces import IBaseObject | |
def user_create_dashboard(principal, event): | |
"""Checks if the dashboard is set up properly for the current user. | |
If it is not (this happens as example with AD-users), | |
setup the plone dashbboard | |
""" | |
if ValidateDashboardSetup(principal, event)(): | |
defautl_portlets = { | |
'plone.dashboard1': (postit.Assignment(), ), | |
'plone.dashboard2': (favorites.Assignment(), ), | |
'plone.dashboard3': [], | |
'plone.dashboard4': [], | |
} | |
userid = principal.getId() | |
list_dashboards = ('plone.dashboard1', 'plone.dashboard2', | |
'plone.dashboard3', 'plone.dashboard4') | |
for name in list_dashboards: | |
assignments = defautl_portlets[name] | |
column = queryUtility(IPortletManager, name=name) | |
if column is not None: | |
category = column.get(USER_CATEGORY, None) | |
if category is not None: | |
manager = category.get(userid, None) | |
if manager is None: | |
manager = UserPortletAssignmentMapping( | |
manager=name, | |
category=USER_CATEGORY, | |
name=userid) | |
category[userid] = manager | |
for portlet in manager.keys(): | |
del manager[portlet] | |
chooser = INameChooser(manager) | |
for assignment in assignments: | |
manager[chooser.chooseName(None, assignment)] = \ | |
assignment | |
class ValidateDashboardSetup(object): | |
""" Dashboard Validator | |
""" | |
def __init__(self, principal, event): | |
self.principal = principal | |
self.event = event | |
def __call__(self, *args, **kwargs): | |
setup_needed = False | |
list_dashboards = ('plone.dashboard1', 'plone.dashboard2', | |
'plone.dashboard3', 'plone.dashboard4') | |
for name in list_dashboards: | |
column = queryUtility(IPortletManager, name=name) | |
if column is not None: | |
category = column.get(USER_CATEGORY, None) | |
if category is not None: | |
manager = category.get(self.principal.getId(), None) | |
if manager is None: | |
setup_needed = True | |
break | |
return setup_needed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment