Back to index

plone3  3.1.7
portletcontext.py
Go to the documentation of this file.
00001 from types import StringTypes
00002 
00003 from zope.interface import implements, Interface
00004 from zope.component import adapts
00005 
00006 from Acquisition import aq_parent, aq_inner, aq_base
00007 
00008 from Products.CMFCore.interfaces import ISiteRoot
00009 from Products.CMFCore.utils import getToolByName
00010 
00011 from plone.portlets.interfaces import IPortletContext
00012 from plone.portlets.constants import USER_CATEGORY
00013 from plone.portlets.constants import GROUP_CATEGORY
00014 from plone.portlets.constants import CONTENT_TYPE_CATEGORY
00015 
00016 class ContentContext(object):
00017     """A portlet context for regular content items.
00018 
00019     Note - we register this for Interface so that it can also work for
00020     tools and other non-content items. This may hijack the context in non-CMF
00021     contexts, but that is doubtfully going to be an issue.
00022     """
00023     implements(IPortletContext)
00024     adapts(Interface)
00025 
00026     def __init__(self, context):
00027         self.context = context
00028 
00029     @property
00030     def uid(self):
00031         return '/'.join(self.context.getPhysicalPath())
00032 
00033     def getParent(self):
00034         return aq_parent(aq_inner(self.context))
00035 
00036     def globalPortletCategories(self, placeless=False):
00037         cats = []
00038         if not placeless:
00039             pt = self._getContentType()
00040             if pt is not None:
00041                 cats.append((CONTENT_TYPE_CATEGORY, pt,))
00042         u = self._getUserId()
00043         if u is not None:
00044             cats.append((USER_CATEGORY, u,))
00045         for g in self._getGroupIds():
00046             cats.append((GROUP_CATEGORY, g,))
00047         return cats
00048 
00049     def _getUserId(self):
00050         membership = getToolByName(self.context, 'portal_membership', None)
00051         if membership is None:
00052             return None
00053         
00054         member = membership.getAuthenticatedMember()
00055         if not member:
00056             return None
00057 
00058         memberId = member.getId()
00059         if memberId is None:
00060             # Basic users such as the special Anonymous users have no
00061             # id, but we can use their username instead.
00062             try:
00063                 memberId = member.getUserName()
00064             except AttributeError:
00065                 pass
00066 
00067         if not memberId:
00068             return None
00069 
00070         return memberId
00071 
00072     def _getGroupIds(self):
00073         membership = getToolByName(self.context, 'portal_membership', None)
00074         if membership is None or membership.isAnonymousUser():
00075             return ()
00076 
00077         member = membership.getAuthenticatedMember()
00078         if not member:
00079             return ()
00080 
00081         groups = member.getGroups()
00082 
00083         # Ensure we get the list of ids - getGroups() suffers some acquision
00084         # ambiguity - the Plone member-data version returns ids.
00085 
00086         for group in groups:
00087             if type(group) not in StringTypes:
00088                 return ()
00089 
00090         return sorted(groups)
00091 
00092     def _getContentType(self):
00093         typeInfo = getattr(aq_base(self.context), 'getTypeInfo', None)
00094         if typeInfo is not None:
00095             fti = typeInfo()
00096             if fti is not None:
00097                 return fti.getId()
00098         portal_type = getattr(aq_base(self.context), 'portal_type', None)
00099         if portal_type is not None:
00100             return portal_type
00101         return None
00102 
00103 class PortalRootContext(ContentContext):
00104     """A portlet context for the site root.
00105     """
00106 
00107     implements(IPortletContext)
00108     adapts(ISiteRoot)
00109 
00110     def __init__(self, context):
00111         self.context = context
00112 
00113     def getParent(self):
00114         return None