Back to index

plone3  3.1.7
common.py
Go to the documentation of this file.
00001 from zope.interface import implements, alsoProvides
00002 from zope.component import getMultiAdapter
00003 from zope.viewlet.interfaces import IViewlet
00004 from zope.deprecation.deprecation import deprecate
00005 
00006 from plone.app.layout.globals.interfaces import IViewView 
00007 
00008 from AccessControl import getSecurityManager
00009 from Acquisition import aq_base, aq_inner
00010 from Products.CMFPlone.utils import safe_unicode
00011 from Products.Five.browser import BrowserView
00012 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00013 from Products.CMFCore.utils import getToolByName
00014 from cgi import escape
00015 from urllib import quote_plus
00016 
00017 
00018 class ViewletBase(BrowserView):
00019     """ Base class with common functions for link viewlets.
00020     """
00021     implements(IViewlet)
00022 
00023     def __init__(self, context, request, view, manager):
00024         super(ViewletBase, self).__init__(context, request)
00025         self.__parent__ = view
00026         self.context = context
00027         self.request = request
00028         self.view = view
00029         self.manager = manager
00030 
00031     @property
00032     @deprecate("Use site_url instead. ViewletBase.portal_url will be removed in Plone 4")
00033     def portal_url(self):
00034         return self.site_url
00035 
00036 
00037     def update(self):
00038         self.portal_state = getMultiAdapter((self.context, self.request),
00039                                             name=u'plone_portal_state')
00040         self.site_url = self.portal_state.portal_url()
00041 
00042     def render(self):
00043         # defer to index method, because that's what gets overridden by the template ZCML attribute
00044         return self.index()
00045         
00046     def index(self):
00047         raise NotImplementedError(
00048             '`index` method must be implemented by subclass.')
00049 
00050 
00051 class TitleViewlet(ViewletBase):
00052 
00053     def update(self):
00054         self.portal_state = getMultiAdapter((self.context, self.request),
00055                                             name=u'plone_portal_state')
00056         self.context_state = getMultiAdapter((self.context, self.request),
00057                                              name=u'plone_context_state')
00058         self.page_title = self.context_state.object_title
00059         self.portal_title = self.portal_state.portal_title
00060 
00061     def index(self):
00062         portal_title = safe_unicode(self.portal_title())
00063         page_title = safe_unicode(self.page_title())
00064         if page_title == portal_title:
00065             return u"<title>%s</title>" % (escape(portal_title))
00066         else:
00067             return u"<title>%s &mdash; %s</title>" % (
00068                 escape(safe_unicode(page_title)),
00069                 escape(safe_unicode(portal_title)))
00070 
00071 
00072 class TableOfContentsViewlet(ViewletBase):
00073     index = ViewPageTemplateFile('toc.pt')
00074 
00075     def update(self):
00076         obj = aq_base(self.context)
00077         getTableContents = getattr(obj, 'getTableContents', None)
00078         self.enabled = False
00079         if getTableContents is not None:
00080             try:
00081                 self.enabled = getTableContents()
00082             except KeyError:
00083                 # schema not updated yet
00084                 self.enabled = False
00085 
00086 
00087 class SkipLinksViewlet(ViewletBase):
00088     index = ViewPageTemplateFile('skip_links.pt')
00089 
00090     def update(self):
00091         context_state = getMultiAdapter((self.context, self.request),
00092                                         name=u'plone_context_state')
00093         self.current_page_url = context_state.current_page_url
00094 
00095 
00096 class SiteActionsViewlet(ViewletBase):
00097     index = ViewPageTemplateFile('site_actions.pt')
00098 
00099     def update(self):
00100         context_state = getMultiAdapter((self.context, self.request),
00101                                         name=u'plone_context_state')
00102         self.site_actions = context_state.actions().get('site_actions', None)
00103 
00104 
00105 class SearchBoxViewlet(ViewletBase):
00106     index = ViewPageTemplateFile('searchbox.pt')
00107 
00108     def update(self):
00109         super(SearchBoxViewlet, self).update()
00110 
00111         context_state = getMultiAdapter((self.context, self.request),
00112                                         name=u'plone_context_state')
00113 
00114         props = getToolByName(self.context, 'portal_properties')
00115         livesearch = props.site_properties.getProperty('enable_livesearch', False)
00116         if livesearch:
00117             self.search_input_id = "searchGadget"
00118         else:
00119             self.search_input_id = ""
00120 
00121         folder = context_state.folder()
00122         self.folder_path = '/'.join(folder.getPhysicalPath())
00123 
00124 
00125 class LogoViewlet(ViewletBase):
00126     index = ViewPageTemplateFile('logo.pt')
00127 
00128     def update(self):
00129         super(LogoViewlet, self).update()
00130 
00131         self.navigation_root_url = self.portal_state.navigation_root_url()
00132 
00133         portal = self.portal_state.portal()
00134         logoName = portal.restrictedTraverse('base_properties').logoName
00135         self.logo_tag = portal.restrictedTraverse(logoName).tag()
00136 
00137         self.portal_title = self.portal_state.portal_title()
00138 
00139 
00140 class GlobalSectionsViewlet(ViewletBase):
00141     index = ViewPageTemplateFile('sections.pt')
00142 
00143     def update(self):
00144         context_state = getMultiAdapter((self.context, self.request),
00145                                         name=u'plone_context_state')
00146         actions = context_state.actions()
00147         portal_tabs_view = getMultiAdapter((self.context, self.request),
00148                                            name='portal_tabs_view')
00149         self.portal_tabs = portal_tabs_view.topLevelTabs(actions=actions)
00150 
00151         selectedTabs = self.context.restrictedTraverse('selectedTabs')
00152         self.selected_tabs = selectedTabs('index_html',
00153                                           self.context,
00154                                           self.portal_tabs)
00155         self.selected_portal_tab = self.selected_tabs['portal']
00156 
00157 
00158 class PersonalBarViewlet(ViewletBase):
00159     index = ViewPageTemplateFile('personal_bar.pt')
00160 
00161     def update(self):
00162         super(PersonalBarViewlet, self).update()
00163 
00164         context_state = getMultiAdapter((self.context, self.request),
00165                                         name=u'plone_context_state')
00166         tools = getMultiAdapter((self.context, self.request), name=u'plone_tools')
00167         
00168         sm = getSecurityManager()
00169 
00170         self.user_actions = context_state.actions().get('user', None)
00171 
00172         plone_utils = getToolByName(self.context, 'plone_utils')
00173         self.getIconFor = plone_utils.getIconFor
00174 
00175         self.anonymous = self.portal_state.anonymous()
00176 
00177         if not self.anonymous:
00178         
00179             member = self.portal_state.member()
00180             userid = member.getId()
00181             
00182             if sm.checkPermission('Portlets: Manage own portlets', self.context):
00183                 self.homelink_url = self.site_url + '/dashboard'
00184             else:
00185                 self.homelink_url = self.site_url + '/author/' + quote_plus(userid)
00186             
00187             member_info = tools.membership().getMemberInfo(member.getId())
00188             # member_info is None if there's no Plone user object, as when
00189             # using OpenID.
00190             if member_info:
00191                 fullname = member_info.get('fullname', '')
00192             else:
00193                 fullname = None
00194             if fullname:
00195                 self.user_name = fullname
00196             else:
00197                 self.user_name = userid
00198 
00199 
00200 class PathBarViewlet(ViewletBase):
00201     index = ViewPageTemplateFile('path_bar.pt')
00202 
00203     def update(self):
00204         super(PathBarViewlet, self).update()
00205 
00206         self.navigation_root_url = self.portal_state.navigation_root_url()
00207 
00208         self.is_rtl = self.portal_state.is_rtl()
00209 
00210         breadcrumbs_view = getMultiAdapter((self.context, self.request),
00211                                            name='breadcrumbs_view')
00212         self.breadcrumbs = breadcrumbs_view.breadcrumbs()
00213 
00214 class ContentActionsViewlet(ViewletBase):
00215     index = ViewPageTemplateFile('contentactions.pt')
00216     
00217     def update(self):
00218         context = aq_inner(self.context)
00219         context_state = getMultiAdapter((context, self.request),
00220                                         name=u'plone_context_state')
00221 
00222         self.object_actions = context_state.actions().get('object_actions', [])
00223 
00224         plone_utils = getToolByName(context, 'plone_utils')
00225         self.getIconFor = plone_utils.getIconFor
00226 
00227         self.portal_actionicons = getToolByName(context, 'portal_actionicons')
00228         
00229         # The drop-down menus are pulled in via a simple content provider
00230         # from plone.app.contentmenu. This behaves differently depending on
00231         # whether the view is marked with IViewView. If our parent view 
00232         # provides that marker, we should do it here as well.
00233         if IViewView.providedBy(self.__parent__):
00234             alsoProvides(self, IViewView)
00235         
00236     def icon(self, action):
00237         icon = action.get('icon', None)
00238         if icon is None:
00239             icon = self.getIconFor('content_actions', action['id'])
00240         return icon