Back to index

plone3  3.1.7
defaultpage.py
Go to the documentation of this file.
00001 import warnings
00002 from zope.component import queryUtility
00003 from zope.interface import implements
00004 
00005 from Acquisition import aq_inner, aq_base
00006 from Products.CMFCore.interfaces import ISiteRoot
00007 from Products.CMFPlone.interfaces import IBrowserDefault
00008 from Products.CMFPlone.interfaces import IDynamicViewTypeInformation
00009 from Products.CMFPlone import utils
00010 from Products.Five.browser import BrowserView
00011 
00012 from plone.app.layout.navigation.interfaces import IDefaultPage
00013 
00014 class DefaultPage(BrowserView):
00015     implements(IDefaultPage)
00016 
00017     def isDefaultPage(self, obj, context_=None):
00018         if context_ is not None:
00019             warnings.warn("The context_ parameter for isDefaultPage was never "
00020                           "implemented and will be removed in Plone 4.",
00021                           DeprecationWarning, 1)
00022 
00023         return isDefaultPage(aq_inner(self.context), obj)
00024 
00025 
00026     def getDefaultPage(self, context_=None):
00027         if context_ is not None:
00028             warnings.warn("The context_ parameter for getDefaultPage was "
00029                           "never implemented and will be removed in Plone 4.",
00030                           DeprecationWarning, 1)
00031 
00032         return getDefaultPage(aq_inner(self.context))
00033 
00034 
00035 def isDefaultPage(container, obj):
00036     """Finds out if the given obj is the default page in its parent folder.
00037 
00038     Only considers explicitly contained objects, either set as index_html,
00039     with the default_page property, or using IBrowserDefault.
00040     """
00041     parentDefaultPage = getDefaultPage(container)
00042     if parentDefaultPage is None or '/' in parentDefaultPage:
00043         return False
00044     return (parentDefaultPage == obj.getId())
00045 
00046 
00047 def getDefaultPage(context):
00048     """Given a folderish item, find out if it has a default-page using
00049     the following lookup rules:
00050 
00051         1. A content object called 'index_html' wins
00052         2. If the folder implements IBrowserDefault, query this
00053         3. Else, look up the property default_page on the object
00054             - Note that in this case, the returned id may *not* be of an
00055               object in the folder, since it could be acquired from a
00056               parent folder or skin layer
00057         4. Else, look up the property default_page in site_properties for
00058             magic ids and test these
00059 
00060     The id of the first matching item is then used to lookup a translation
00061     and if found, its id is returned. If no default page is set, None is
00062     returned. If a non-folderish item is passed in, return None always.
00063     """
00064     # The list of ids where we look for default
00065     ids = {}
00066 
00067     # For BTreeFolders we just use the has_key, otherwise build a dict
00068     if hasattr(aq_base(context), 'has_key'):
00069         ids = context
00070     else:
00071         for id in context.objectIds():
00072             ids[id] = 1
00073 
00074     # Inline function with default argument.
00075     def lookupTranslationId(obj, page):
00076         return utils.lookupTranslationId(obj, page, ids)
00077 
00078     # 1. test for contentish index_html
00079     if ids.has_key('index_html'):
00080         return lookupTranslationId(context, 'index_html')
00081 
00082     # 2. Test for IBrowserDefault
00083     browserDefault = IBrowserDefault(context, None)
00084     if browserDefault is not None:
00085         fti = context.getTypeInfo()
00086         if fti is not None:
00087             dynamicFTI = IDynamicViewTypeInformation(fti, None)
00088             if dynamicFTI is not None:
00089                 page = dynamicFTI.getDefaultPage(context, check_exists=True)
00090                 if page is not None:
00091                     return lookupTranslationId(context, page)
00092 
00093     # 3. Test for default_page property in folder, then skins
00094     pages = getattr(aq_base(context), 'default_page', [])
00095     if isinstance(pages, basestring):
00096         pages = [pages]
00097     for page in pages:
00098         if page and ids.has_key(page):
00099             return lookupTranslationId(context, page)
00100 
00101     portal = queryUtility(ISiteRoot)
00102     # Might happen during portal creation
00103     if portal is not None:
00104         for page in pages:
00105             if portal.unrestrictedTraverse(page, None):
00106                 return lookupTranslationId(context, page)
00107 
00108         # 4. Test for default sitewide default_page setting
00109         pp = getattr(portal, 'portal_properties', None)
00110         if pp is not None:
00111             site_properties = getattr(pp, 'site_properties', None)
00112             if site_properties is not None:
00113                 for page in site_properties.getProperty('default_page', []):
00114                     if ids.has_key(page):
00115                         return lookupTranslationId(context, page)
00116 
00117     return None