Back to index

plone3  3.1.7
portal.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.i18n.interfaces import IUserPreferredLanguages
00003 from zope.i18n.locales import locales, LoadLocaleError
00004 
00005 from plone.memoize.view import memoize, memoize_contextless
00006 
00007 from Acquisition import aq_inner
00008 from Products.CMFCore.utils import getToolByName
00009 from Products.Five.browser import BrowserView
00010 
00011 from plone.app.layout.navigation.root import getNavigationRoot
00012 
00013 from interfaces import IPortalState
00014 
00015 class PortalState(BrowserView):
00016     """Information about the state of the portal
00017     """
00018     implements(IPortalState)
00019 
00020     @memoize_contextless
00021     def portal(self):
00022         context = aq_inner(self.context)
00023         return getToolByName(context, 'portal_url').getPortalObject()
00024     
00025     @memoize_contextless
00026     def portal_title(self):
00027         return self.portal().Title()
00028         
00029     @memoize_contextless
00030     def portal_url(self):
00031         return self.portal().absolute_url()
00032         
00033     @memoize_contextless
00034     def navigation_root_path(self):
00035         return getNavigationRoot(aq_inner(self.context))
00036     
00037     @memoize_contextless
00038     def navigation_root_url(self):
00039         rootPath = self.navigation_root_path()
00040         return self.request.physicalPathToURL(rootPath)
00041     
00042     @memoize_contextless
00043     def default_language(self):
00044         context = aq_inner(self.context)
00045         site_properties = getToolByName(context, "portal_properties").site_properties
00046         return site_properties.getProperty('default_language', None)
00047 
00048     @memoize
00049     def language(self):
00050         # TODO Looking for lower-case language is wrong, the negotiator
00051         # machinery uses uppercase LANGUAGE. We cannot change this as long
00052         # as we don't ship with a newer PloneLanguageTool which respects
00053         # the content language, though.
00054         return self.request.get('language', None) or \
00055                 aq_inner(self.context).Language() or self.default_language()
00056 
00057     @memoize_contextless
00058     def locale(self):
00059         # This code was adopted from zope.publisher.http.setupLocale
00060         envadapter = IUserPreferredLanguages(self.request, None)
00061         if envadapter is None:
00062             return None
00063 
00064         _locale = None
00065         langs = envadapter.getPreferredLanguages()
00066         for httplang in langs:
00067             parts = (httplang.split('-') + [None, None])[:3]
00068             try:
00069                 _locale = locales.getLocale(*parts)
00070                 break
00071             except LoadLocaleError:
00072                 # Just try the next combination
00073                 pass
00074         else:
00075             # No combination gave us an existing locale, so use the default,
00076             # which is guaranteed to exist
00077             _locale = locales.getLocale(None, None, None)
00078 
00079         return _locale
00080 
00081     @memoize_contextless
00082     def is_rtl(self):
00083         _locale = self.locale()
00084         if _locale is None:
00085             # We cannot determine the orientation
00086             return False
00087 
00088         char_orient = _locale.orientation.characters
00089         if char_orient == u'right-to-left':
00090             return True
00091 
00092         return False
00093 
00094     @memoize_contextless
00095     def member(self):
00096         context = aq_inner(self.context)
00097         tool = getToolByName(context, "portal_membership")
00098         return tool.getAuthenticatedMember()
00099 
00100     @memoize_contextless
00101     def anonymous(self):
00102         context = aq_inner(self.context)
00103         tool = getToolByName(context, "portal_membership")
00104         return bool(tool.isAnonymousUser())
00105 
00106     @memoize_contextless
00107     def friendly_types(self):
00108         context = aq_inner(self.context)
00109         site_properties = getToolByName(context, "portal_properties").site_properties
00110         not_searched = site_properties.getProperty('types_not_searched', [])
00111 
00112         types = getToolByName(context, "portal_types").listContentTypes()
00113         return [t for t in types if t not in not_searched]